I have the following directory structure:
/test_dir/logs:
admin-access.log
admin-access.log00001
admin-access.log00002
admin-access.log00003
admin-access.log00004
admin-access.log00005
/test_dir/installer_logs:
AITimeListener.log
installer.log
/test_dir/servers/clust1/logs/:
clust1.out
clust1.out00001
clust1.out00002
clust1.out00003
clust1.out00004
I'm using the following set of commands to archive the folder with exclusions:
cd /test_dir/
tar -czhf /backup/test_dir_node01_2016-02-19_initial.tgz --anchored --exclude "logs/*" --exclude "servers/*/logs/*" --exclude "installer_logs/*" *
And as result I've got the following directory structure after archive untar:
/backup/logs:
no files here
/backup/installer_logs:
no files here
/backup/servers/clust1/logs/:
no files here
I'm trying to code the bash script which is supposed to do the same and with no luck, here's my code:
backup.parameters file:
NC_HOME=/test_dir
BACKUP_HOME=/backup
EXCLUDE_LIST="
--exclude \"logs/*\"
--exclude \"servers/*/logs/*\"
--exclude \"installer_logs/*\"
"
backup_initial.sh file
#!/bin/bash
if [ ! -f backup.parameters ]; then echo "backup.parameters file not found! exiting...."; exit 1; fi
. ./backup.parameters
TAR_ARGS="czf"
DATE=`date +%F`
HOSTNAME=`hostname`
cd $NC_HOME
echo "Initial backup started on $HOSTNAME at `date \"+%F %T\"`"
tar -czhf $BACKUP_HOME/$(basename $NC_HOME)_"$HOSTNAME"_"$DATE"_initial.tgz --anchored $EXCLUDE_LIST *
echo "Initial backup finished on $HOSTNAME at `date \"+%F %T\"`"
echo "Initial backup location: $BACKUP_HOME/$(basename $NC_HOME)_"$HOSTNAME"_"$DATE"_initial.tgz"
After untar of archive which is created by the script I see there're still files in exclusion directories:
/backup/logs:
admin-access.log
admin-access.log00001
admin-access.log00002
admin-access.log00003
admin-access.log00004
admin-access.log00005
/backup/installer_logs:
AITimeListener.log
installer.log
/backup/servers/clust1/logs/:
clust1.out
clust1.out00001
clust1.out00002
clust1.out00003
clust1.out00004
Please help how can I code this out, also please don't advise me to use -X option. I really need to do it with --exclude.
Try changing EXCLUDE_LIST to:
EXCLUDE_LIST=(
--exclude 'logs/*'
--exclude 'servers/*/logs/*'
--exclude 'installer_logs/*'
)
And then expand it like this:
tar -czhf ... --anchored "${EXCLUDE_LIST[@]}" *
The reason why what you're doing now doesn't work is because EXCLUDE_LIST is being expaned to --exclude "logs/*" .... The double quotes aren't removed and remain part of the argument, and so the pattern doesn't match anything since you don't have any files or directories that have double quotes in their names.