Using variables for exclusion in tar


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using variables for exclusion in tar
# 1  
Old 07-18-2010
Using variables for exclusion in tar

Hello.

I am writing a backup script which uses a function to create a tar archive for a specified directory. For some reason though I cannot get tar to exclude a specified path when passed as a variable. The problem does not seem to be with the command though as it works when entered in the script directly.

Here are the relevant portions of the script:

Code:
BUP_ROOT=$HOME/backup
bup_files () {
   cd $2
   [[ "$3" ]] && BUP_EXCLUDE="--exclude='$3'" || BUP_EXCLUDE=""
   tar $BUP_EXCLUDE -czf $BUP_ROOT/$1.tgz ./
}

bup_files name_of_archive /path/to/archive "download/*"

This works except the specified directory is not excluded. If I put an 'echo' in front of the tar command it shows the $BUP_EXCLUDE variable is being set and with what I would expect. e.g.

Code:
tar --exclude='download/*' -czf /home/username/backup/name_of_archive.tgz ./

Wondering if the use of putting the whole exclusion option in a variable was a problem, as a test I rewrote the command as

Code:
tar --exclude='$3' -czf $BUP_ROOT/$1.tgz ./

This did not make any different though. As a final test I put the exclusion directly in the tar command

Code:
tar --exclude='download/*' -czf $BUP_ROOT/$1.tgz ./

This worked, the archive was created with the required path excluded. This leads me to think there is some problem with the shell passing the variable correctly rather than with the tar command. The wildcard is supposed to be a literal for tar to do its own pattern matching, and by testing with echo in front of the tar command it does seem to be passed as one, there is no globbing. I have even tried putting quotes around the $BUP_EXCLUDE" variable in the tar command to see if that helps with no effect.

The shell is BASH version 3.00.15 and I have tried searching for a solution with no luck so I hope I am missing something obvious here.

Thanks,

Michael.
# 2  
Old 07-19-2010
'$3' does not allow the shell to expand the variable use double quotes
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 07-19-2010
Would the single quotes still have that effect inside double quotes?

It fixed the problem though, in hindsight I was being totally dumb using them to prevent globbing when this never would have happened anyway being inside the double quotes.

So I am wondering whether rather than preventing the variable being expanded, the single quotes were just being passed to tar as part of the exclude pattern, and so it was failing for that reason?

Either way, it has fixed the problem.

Many thanks,

Michael.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern exclusion between two files

I have a large file (file1) that has 96770154 lines and a smaller file (file2) that has 3 lines. I want to remove all of the strings from file1 that occur in file2. file1 looks like this: DOGDOGNODOGTESTCAT CATHELLOBYEEBYEFAT CATCATDOGDOGCATYESGOOD file2 looks like this: YES... (10 Replies)
Discussion started by: verse123
10 Replies

2. Shell Programming and Scripting

Mutual exclusion in getopts

Can i use mutual exclusion of particular args and force script to exit if both are specified? while getopts l:d: OPTS; do case $OPTS in l) VALUE1=$OPTARG;; d) VALUE2=$OPTARG;; *) echo "$USAGE" && exit 2;; # Here i want to exit if both -l and -d are specified ... (1 Reply)
Discussion started by: urello
1 Replies

3. Shell Programming and Scripting

tar command to explore multiple layers of tar and tar.gz files

Hi all, I have a tar file and inside that tar file is a folder with additional tar.gz files. What I want to do is look inside the first tar file and then find the second tar file I'm looking for, look inside that tar.gz file to find a certain directory. I'm encountering issues by trying to... (1 Reply)
Discussion started by: bashnewbee
1 Replies

4. Shell Programming and Scripting

Ftp mget exclusion question

In the script I am doing a mget for multiple files. How can I exclude just one file which I do not need to ftp ? (3 Replies)
Discussion started by: jville
3 Replies

5. Shell Programming and Scripting

Separating delimited file by pattern with exclusion list

I have a file with the contents below jan_t=jan;feb_t=feb;mar_t=mar;year=2010 jan_t=null;feb_t=feb;mar_t=mar;year=2010 jan_t=jan;feb_t=feb;mar_t=mar;year=2010 I want to extract out all the fields values ending with "_t" , however, i want to exclude feb_t and mar_t from the results In... (6 Replies)
Discussion started by: alienated
6 Replies

6. Shell Programming and Scripting

Delete old files but with exclusion with file list

Hello Can you please help and check what im missing on script below the goal is to delete the old files more than 7 days old but not the excluded file list inside excluded.dat file #!/bin/sh EXCLUDE=/path/to/exclude/exclude.dat FIND=/bin/find for xfile in '(read $EXCLUDE)' do $FIND... (9 Replies)
Discussion started by: angst_nu
9 Replies

7. UNIX for Dummies Questions & Answers

tar -cvf test.tar `find . -mtime -1 -type f` only tar 1 file

Hi all, 4 files are returned when i issue 'find . -mtime -1 -type f -ls'. ./ora_475244.aud ./ora_671958.aud ./ora_934052.aud ./ora_934050.aud However, when I issued the below command: tar -cvf test.tar `find . -mtime -1 -type f`, the tar file only contains the 1st file -... (2 Replies)
Discussion started by: ahSher
2 Replies

8. Solaris

Search for string in a directory with binaries exclusion

Hi all, My need is to search for a given string "toto" within a directory, but the search should ignore binary and db files. It is in fact, a combination of xargs grep that I didn't succeed to manage. Let we say that we are under /etc/ and we want to search for all included files (except... (9 Replies)
Discussion started by: mdjebbi
9 Replies

9. UNIX for Dummies Questions & Answers

Searching a file with exclusion in the search

Hello, I'm looking for a bit of help. Im trying to search a file for lines that contain white spaces at the end of the lines. This is what I'm using where $param is the path and file name and it redirects the output to a txt file : echo | grep -n ' $' $param >> $2 Is it possible to have... (8 Replies)
Discussion started by: gintreach
8 Replies

10. Shell Programming and Scripting

Exclusion List of file and directory

dear all i trying to list all files within a directory. I want to exclude all subdirectory and some files, with using below statement, but it not exclude the files which start with "&" and end with "SL" , is there any things wrong with the below statement ? TIA cd /myaccount/mydirectory... (6 Replies)
Discussion started by: ayang
6 Replies
Login or Register to Ask a Question