filter question

Matt McCutchen matt at mattmccutchen.net
Fri Feb 22 01:14:41 GMT 2008


On Thu, 2008-02-21 at 17:41 -0500, Igor Serebryany wrote:
> I've been trying to come up with a way to do the following
> operation.  I have a /home folder and I want to transfer only the
> /mail/ folders inside the home folder along with their parent
> folders.  So, if I have the following files:
> 
> /home/bob/document.doc
> /home/bob/pr0n/pr0nFile
> /home/bob/mail/file1
> /home/bob/mail/spam/viagra
> /home/mike/kitten.jpg
> /home/mike/misc/mail/inbox
> 
> I want to do "rsync ***MAGIC*** /home /destination" and have
> /destination look like this:
> 
> /destination/bob/mail/file1
> /destination/bob/mail/spam/viagra
> /destination/mike/misc/mail
> /destination/mike/misc/mail/inbox
> 
> I can't figure out what combination of options would allow me to do
> this, even after bashing my head against various filter sets for
> about 1.5 hours.  Can anyone help me?

Rsync doesn't make this easy!  With -r, excluding a directory stops
rsync from traversing the contents to notice any included items inside.
Thus, any directory that potentially could directly or indirectly
contain a "mail" folder needs to be included in order for rsync to find
the "mail" folder.  However, you don't want to actually copy any
directories except for "mail" folders and their ancestors, so use
--prune-empty-dirs.  However, this will prune empty directories even
inside of "mail" folders.  The convention to stop this is to use a
protect filter, but unfortunately, that will interfere with deletion of
extraneous destination directories if you are using --delete.  Here's
the command:

rsync -r --filter='P mail/***/' --include='mail/***' --include='*/' \
--exclude='*' --prune-empty-dirs /home/ /destination/

An alternative would be to use "find" instead of rsync's filters to find
the desired "mail" folders:

find /home -type d -name mail -printf '%P\n' | \
rsync -r --files-from=- /home/ /destination/

This will correctly delete extraneous stuff from the destination mail
folders if you pass --delete, but it won't delete anything elsewhere in
the destination.

Wayne, I think there ought to be a way to prevent directories from being
pruned from the file-list without preventing them from being deleted.

Matt



More information about the rsync mailing list