Executing a tar command with the --exclude option in a Debian shell script.

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Executing a tar command with the --exclude option in a Debian shell script.
# 1  
Executing a tar command with the --exclude option in a Debian shell script.

Hi All,

I am trying to execute the following tar command with two --exclude options to suppress extract of the two directories specified.

Do I need to single quote the directory paths ??
Many thanks for your help.

The relevant code excerpt from the script is:

cd /var/www/${SITE}

 tar -cvf ${WORKING_BACKUP_ROOT}/${TAR_ARCHIVE_FILE} --exclude='/var/www/abcCompany/cache' \
                     --exclude='/var/www/abcCompany/administrator/cache' . >/${BACKUP_ROOT}/${ARCHIVE_LOG}

Last edited by daveu7; 03-14-2012 at 01:12 PM..
# 2  
If your directories contained spaces in their names, the quotes would prevent them from splitting and messing up your commandline. So I would keep them in any case, but they neither help nor hurt here.

For instance, try this:

$ echo exclude='whatever'


The shell itself uses the single quotes before they are fed into tar.

Oh, one more thing you should add to your excludes -- the archive itself, of course.

Last edited by Corona688; 03-14-2012 at 02:02 PM..
# 3  
Hi Corona688,

Please can you explain your last two sentences at little further.
Do you mean I have to exclude the archive I am building as well ?

My reasons for doing all this are that I am getting this error:

tar: .: file changed as we read it

and then my shell script called from Cron exits because its checking the exit status > 0 and doesn't do any backups.

So I am trying to exclude the dynamically changing directories i.e. cache directories.

My investigations have revealed that the tar command is return an exit status of > 0 for the above error i.e fatal rather than a warning which I suppose should have a 0 exit status.

Many thanks
# 4  
I suspect your archive is trying to archive itself to itself.

tar ... --exclude=${WORKING_BACKUP_ROOT}/${TAR_ARCHIVE_FILE} ...

tar will return nonzero for nonfatal errors, too. I suppose it depends what you consider "fatal". A functioning tarball that contains everything but the file you really needed could be considered a failure. So, only if everything goes perfectly will tar return success.
# 5  
Further questions after testing the revised tar structure

Hi All

I have modified the tar command as discussed in this thread.
But now I am getting a

tar: : Cannot stat: No such file or directory
tar: Error exit delayed from previous errors
./backup_websites: line 67: --exclude=/var/www/abcCompany/cache: No such file or directory

against the second exclude. The directory definitely exists. I am using absolute paths here are relative paths required ?
Do you think the backslashes to split the long command maybe and issue ?

Many thanks for your help

cd /var/www/${SITE}

                      --exclude='/var/www/abcCompany/cache' \
                     --exclude='/var/www/abcCompany/administrator/cache' . >/${BACKUP_ROOT}/${ARCHIVE_LOG}

# 6  
The backslashes are an issue, in a sense -- you missed one Smilie So it was trying to find a file named --exclude=... to execute.

If any of those folders contain spaces in their name, they need to be quoted. You might as well just quote all of them, to be safe. In double-quotes this time, since variables don't work inside single-quotes.
cd /var/www/"{SITE}"

        --exclude="${WORKING_BACKUP_ROOT}/${TAR_ARCHIVE_FILE}" \
                      --exclude='/var/www/abcCompany/cache' \
                     --exclude='/var/www/abcCompany/administrator/cache' . >"/${BACKUP_ROOT}/${ARCHIVE_LOG}"

# 7  
Hi Corona688,

I have implemented the changes you suggested and I am still getting the same file or directory not found error. I am running the script as CRON (which always runs as root) so you don't think its permissions on the directories do you ?

If I just run the original code it works sometimes and not others ? but then originally I was getting:

tar: .: file changed as we read it.

Hence the reason for switching to new code structure.

Please can you offer any further advise.
Many thanks for your help.

This was my original code:

cd /var/www/"{SITE}"



Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #921
Difficulty: Medium
Unix Epoch Time cannot be extended backwards from the epoch date.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Tar Ball command to exclude directories

Hi, uname -a SunOS mymac 5.11 11.2 sun4u sparc SUNW,SPARC-Enterprise I need to tar a folder /tmp/moht but do not want these three folders to be included in the tar file -> savejpg, bmpsave and imgsave I tried --exclude, -path, -not options but it says bad option Can you help me with... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Executing 'exit' command from shell script

Hi, I am writing shell script to automate few use cases for CLI interface. We have CLI interface which has bunch of commands. I am trying to execute one of the commands 'exit' as part of automation to exit from CLI object (not from shell script) in my shell script. My intension is to execute... (4 Replies)
Discussion started by: Mahesh Desai
4 Replies

3. AIX

Exclude Directories in my tar command

Hi, im having some issues after i execute the next command: tar -cvf /varios/restore/test.tar -X /jfma/test1/excludefile /jfma | gzip -c > /varios/restore/test.tar.gz this creates the desired "test.tar.gz" file, but whe i try to open it it says "tar: 0511-164 There is a media read or write... (6 Replies)
Discussion started by: blacksteel1988
6 Replies

4. UNIX for Dummies Questions & Answers

Cron shell script not executing diskutil command

I'm trying to learn how to use cron for repetative tasks. I have an external disk that needs to be unmounted and remounted every hour due to some problems that a backup utility (specifically, TimeMachine) is having repeatedly accessing the device. I've created a shell script that will find the... (3 Replies)
Discussion started by: illuminate
3 Replies

5. Shell Programming and Scripting

working with tar exclude command

i have issue with tar, let me explain when i run below command it works perfectly as usual. tar -cvf /tmp/temp.tar --exclude="exclusion expression" dir my requirement is --exclude="exclusion expression" will come from another variable. so when i execute below command: tar -cvf... (2 Replies)
Discussion started by: ajayyadavmca
2 Replies

6. Shell Programming and Scripting

Executing a batch of files within a shell script with option to refire the individual files in batch

Hello everyone. I am new to shell scripting and i am required to create a shell script, the purpose of which i will explain below. I am on a solaris server btw. Before delving into the requirements, i will give youse an overview of what is currently in place and its purpose. ... (2 Replies)
Discussion started by: goddevil
2 Replies

7. Shell Programming and Scripting

complicated exclude option in find command

Hi all, In a directory, I have many video files. Example : As you can see, some of the video files come with a .aspx file (wich means the video is actually being uploaded and not entirely written on the FS) I try to write a bash script that would find all video files in the ... (1 Reply)
Discussion started by: gniagnia
1 Replies

8. Shell Programming and Scripting

Problem Executing Firmware Command using Shell Script

Guys, I have a script that should change one of the configuration Parameter in a http accelerator, this config change which will halt http traffic into device. So I have designed a script which should do these changes. But after executing this script, found that one of the input variable is not... (8 Replies)
Discussion started by: raghunsi
8 Replies

9. UNIX for Dummies Questions & Answers

TAR : option to exclude absolute path

Dear All , :D I have a question ... I need to exclude the absolute path in the TAR process. For example : system("tar cvf /root/BACKUPS_$fecha.tar /root/BKP/"); system("gzip /root/BACKUPS_$fecha.tar"); I need to exclude de path " /root/BKP/ " in the file.tar.gz What is the parameter to... (1 Reply)
Discussion started by: telco
1 Replies

10. UNIX for Dummies Questions & Answers

tar command with compress option...

Hi ! i have to write a script that archivs homes not used since 3 years. First, my script gathers the users that are concerned, using the following command : ll -lt /home/*/.sh_history | egrep '2000|1999|1998|1997' | awk '{print $3}' i obtain a list like this : user_1 user_2 ...... (3 Replies)
Discussion started by: tomapam
3 Replies

Featured Tech Videos