Backup using bash script


Recommended Posts

I'm trying to backup some of the folders in my home directory, and (of course) am having some problems with folders containing spaces.

This is the current script I have:

#!/bin/sh
DATE=`date +'%m_%d_%Y'`
FILELIST='/home/name/somedirectory /home/name/anotherdirectory /home/name/a directory with spaces'
EXCLUDELIST='/home/name/somedirectory/dontbackupthis /home/name/somedirectory/dont backup this'
echo "STARTING BACKUP"
tar --exclude=$EXCLUDELIST -cvvzf /media/externalharddriver/linuxbackup_${DATE}.tar.gz $FILELIST

I think tar is recognizing the directories with spaces as separate directories (ex. It's thinking that '/home/name/a directory with spaces' is in fact '/home/name/a' and 'directory' and 'with' and 'spaces', so 4 directories).

Any ideas on how I can pass the directories to tar correctly? Wrapping the directory in ''s didn't work, and using \ to escape the spaces didn't work either (I probably did something wrong though :p )

Link to comment
Share on other sites

Edit:

Try changing the first few lines to something looking like this:

IFS='|'
FILELIST='/home/david/test/here|/home/david/test/a directory with spaces|/some/other/directory'
EXCLUDELIST='/home/david/test/here/exclude this'

What the IFS='|' does is changes the separator used by bash to the | symbol (or whatever else you want), meaning spaces in the filenames will be left intact.

Edited by David Scaife
Link to comment
Share on other sites

As a workaround to escaping characters in variables you can list the directories to backup (and those to be excluded) in a separate text file, one directory per line, and pass that to tar:

tar -X excludelist.txt -cvvzf /media/externalharddriver/linuxbackup_${DATE}.tar.gz -T filelist.txt

Tar wont get confused with spaces as it will read each line as a different path.

Link to comment
Share on other sites

I currently have a version as follows (with only one directory listed in FILELIST and one in EXCLUDELIST):

#!/bin/sh
DATE=`date +'%m_%d_%Y'`
IFS='|'
FILELIST='/home/name/some directory with spaces'
EXCLUDELIST='/home/name/some directory with spaces'
echo "STARTING BACKUP"
tar --exclude=$EXCLUDELIST -cvvzf /media/externalharddriver/linuxbackup_${DATE}.tar.gz $FILELIST

Basically, this should create an empty archive, since the file in FILELIST is the same as the file in EXCLUDELIST.

However, it seems to ignore the EXCLUDELIST!

Link to comment
Share on other sites

Basically, this should create an empty archive, since the file in FILELIST is the same as the file in EXCLUDELIST.

However, it seems to ignore the EXCLUDELIST!

That's odd, are you sure about that? What output does the script give you?

I know it's not very helpful saying this, but I ran the exact same script you pasted above (with a change of folder path) and it created an empty archive when FILELIST = EXCLUDELIST.

Link to comment
Share on other sites

Ah, I had a '/' at the end of every item in the EXCLUDELIST. I fixed that now.

One more bug is left: I think the EXCLUDELIST-pattern is wrong when I have multiple items in there. The first directory is excluded, as it should, but the second one is added.

I think that I should specify the EXCLUDELIST differently, because the second (and third, and ...) items are indexed, instead of ignored (tar --exclude=BLAH includefirst includesecond).

I'm not sure if this is clear enough, but I hope my current problem is understandable.

Link to comment
Share on other sites

How about this?

#!/bin/sh
DATE=`date +'%m_%d_%Y'`
IFS='|'
FILELIST='/home/name/some directory with spaces'
EXCLUDELIST='/home/name/backup.tmp'
echo -e "/home/name/some dir with spaces\n/home/name/another dir with spaces\n...." > $EXCLUDELIST
echo "STARTING BACKUP"
tar --exclude-from=$EXCLUDELIST -cvvzf /media/externalharddriver/linuxbackup_${DATE}.tar.gz $FILELIST

I cannot test at the moment, but you should get the idea from this example.

Link to comment
Share on other sites

I think you may need a separate exclude for every path. Otherwise, your script will look like:

tar --exclude=/path/to/file1 /path/to/file2 -cvvzf /media/externalharddriver/linuxbackup_9182008.tar.gz /path/to/include1 /path/to/include2

So I think this would be confusing to the program because you didn't explicitly say to exclude both... maybe not...

edit: you got it working so nvm =p

Edited by Robgig1088
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.