I’m using a custom script to create the backups of file data on the Linux servers which I administrate.
The script does backup a complete directory with it’s subdirectory structures, however sometimes on one of the servers there was a requirement that some of the directories which are used as cache directories and grow really high in Gigabytes to be excluded from the tar archive.
The server where I have already set up my data backup solution is running Debian Linux and therefore I needed to modify my site_backups.sh file data backup script to exclude a number of directories from my target backup directory /home/mydata/ .
I haven’t recently used the tar –exclude option, so I had to experiment a bit with the option, check some info online and check the tar manual.
After my little experiments finally I found out the proper way to exclude files from my tar archives and decided to put it on my blog in hope that it might be of use to some other Linux / Mac hobbyists out there.
Here is how I was able to properly exclude my directories from my tar archive:
debian:~# /bin/tar -czvf my_tar_archive_name.tar.gz /home/mydata
--exclude="{"/home/mydata/dirtoexclude1/*",
"/home/mydata/dirtoexclude2/*","/home/mydata/dirtoexcludeN/*"}
Note that it is possible to put each of the excluded directory under a separate –exclude like for instance:
debian:~# /bin/tar -czvf my_tar_archive_name.tar.gz /home/mydata
--exclude='/home/mydata/exampledirtoexclude1/*'
--exclude='/home/mydata/exampledirtoexclude2/*'
Note that I experienced problems with setting the directories to be exclude if I try to place the tar –exclude option before the specified target directory to be placed in the archive.
Just to give you an example if you try to set the tar exclude like:
debian:~# /bin/tar -czvf my_tar_archive_name.tar.gz
--exclude="{"/home/mydata/dirtoexclude1/*",
"/home/mydata/dirtoexclude2/*","/home/mydata/dirtoexcludeN/*"} /home/mydata
You will get errors indicating the excluded directories as missing, if you try to invoke the exclude as shown in the 3rd command example you will get errors like:
/bin/tar: --exclude=/home/mydata/dirtoexclude1/*: Cannot open: No such file or directory
/bin/tar: Error is not recoverable: exiting now
Just in case if you’re experiencing this tar error whether trying to exlude directory this is probably caused by the wrong placement of the –exclude tar command directive, just change the –exclude option placement as shown in the first two commands in my post and the exclude will be fine.
Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16 GTB7.1
tar –exclude=”/engineer/misc/Controls/*” -czvf /tars/misc.tgz misc/*
This is the command I am trying to run and it isn’t working.
View CommentView CommentI took the = out, tried the –exclude at the end no luck, any ideas?
Mozilla/5.0 (X11; U; Linux x86_64; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Version/5.0 Safari/531.2+ Debian/squeeze (2.30.6-1) Epiphany/2.30.6
What Linux distro, are you running. I notice you’re trying “-exclude” try with “–exclude”. If you’re on Debian the exclude in the article should definitely be working.
View CommentView CommentMozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16 GTB7.1
Checked this morning, did not work again. Here is the entire command line I am using.
tar -czvf /tars/ArchivesII.$TD.tgz Archives/ –exclude “Archives/Controls/” –exclude “/Archives/Sales/” –exclude “/UPS System Backup/”
Using Fedora 9.
View CommentView Commentsorry, typo, I do have the double dash in front of exclude.
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2
This worked well for me:
View CommentView Commenttar -cvf home.tar /home/want_this/ –exclude '/home/dont_want_this/*'