Complex includes/excludes with rsync

Solution 1:

You need to include all of the parent directories down to the desired directory before using the exclude rule.

For instance, I use the following in a backup script:

rsync -av \ 
--filter='+ /var/' \
--filter='+ /var/backups/' \
--filter='- /var/*' \
/ \
$DEST

So in your case you would need something like the following:

rsync -av \ 
--filter='+ /home/' \
--filter='+ /home/user1/' \
--filter='+ /home/user2/' \
--filter='+ /home/user2/subdir/' \
--filter='- /home/user2/*' \
--filter='- /home/*' \
/ \
$DEST

Solution 2:

On the command line:

rsync --dry-run --verbose --recursive --include=/home/user1 --exclude=/home/* / DEST

Remove --dry-run to make it functional, replace "DEST" with your destination and add user and host to the source "/" if needed.

From a rule file:

rsync --dry-run --verbose --recursive --filter='merge /etc/rsync/somerules.rules' / DEST

where the contents of /etc/rsync/somerules.rules might be:

+/ /home/user1
-/ /home/*

You should test these and you may need to make some adjustments, but this should get you started.