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}
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'
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.
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.
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.
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}
tar -cvf ${WORKING_BACKUP_ROOT}/${TAR_ARCHIVE_FILE}
--exclude=${WORKING_BACKUP_ROOT}/${TAR_ARCHIVE_FILE} \
--exclude='/var/www/abcCompany/cache' \
--exclude='/var/www/abcCompany/administrator/cache' . >/${BACKUP_ROOT}/${ARCHIVE_LOG}
The backslashes are an issue, in a sense -- you missed one 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}"
tar -cvf "${WORKING_BACKUP_ROOT}/${TAR_ARCHIVE_FILE}" \
--exclude="${WORKING_BACKUP_ROOT}/${TAR_ARCHIVE_FILE}" \
--exclude='/var/www/abcCompany/cache' \
--exclude='/var/www/abcCompany/administrator/cache' . >"/${BACKUP_ROOT}/${ARCHIVE_LOG}"
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}"
tar -cvf ${WORKING_BACKUP_ROOT}/${TAR_ARCHIVE_FILE} . >/${BACKUP_ROOT}/${ARCHIVE_LOG}