How to exclude files and directories from tar
archive on Debian Linux / (Cannot open: No such file or directory)
cause and solution
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.