Is it possible to exclude empty directories when creating a tar?
I have a backup command which looks like this:
tar cvpzf /backup/incremental-$now.tar.gz --after-date='1 day ago' -C / etc/ home/ root/ --exclude-from=/root/exclusion_list.txt --warning=no-file-unchanged > /backup/log-$now.txt 2>&1
The problem is that it also includes empty folder, when all the files in them are unchanged so not included in the incremental backup. I'm looking for a way to avoid this, but the only solutions I've found (piping the results of find to tar as arguments with xargs) just works when you've one folder, I've got three of them.
By default tar does not provide a means of skipping empty directories. That being said, it is not too much work to pull a simple shell-script out of thin air to do what needs to be done.
Here is a forum discussion where I found the solution to the exact same problem, when the need arose.
MAJOR UPDATE: First I need to seriously apologize for my sloppy copy-pasting. It was a late-at-night post and I was not at the peak of my intellect...
The correct forum post is here. Actually it is the same person asking the same question in a different forum, and getting a totally different answer. I had bookmarked both when I encountered the problem and gave the link to the wrong one.
Actually, what it shows here is the way to "pipe" the output of the find command into tar. You have to ignore all the stuff about "newer than" the guy is asking about.
Instead the find part you need is like this:
find . -type d ! -empty | ...
Of course you have to replace the dot (".") with the path where you need the find/tar to start. After the pipe continue with the tar command as per the second link (the correct one).
Once again I am deeply sorry if I caused any trouble with my lack of attention to detail.
Another way which only works if you have only a small set of files changed is:
tar cvzf [...] `find . -mtime -1 -type f`
If you have a large set of changes, you can dump the changed file list to a file and use --files-from parameter on your tar command:
find . -mtime -1 -type f > /tmp/changed.txt
tar cvzf [...] --files-from=/tmp/changed.txt
or in one line
find . -mtime -1 -type f | tar cvzf [...] --files-from=/dev/stdin