That rsyncing feeling

Q I'm highly interested in running backups - ideally decentralised - regularly. To do that, I've found the nice utility called rsync. Here's the command I run to get a local copy of my whole home directory in another folder:

rsync -avz --delete-after /home/white/ /home/white/Backup/

However, I would like to filter out specific files (typically source files and not data files) so that I come up with a subset of the filesystem. In that way, I would be able to copy the most sensitive files on to a remote hard drive and a local USB pen drive.

A There are a couple of arguments to rsync that will do what you want. First though, is /home/white/Backup on a separate filesystem to /home/white? If so, you should add the -x or --one-file-system option, otherwise you'll find yourself trying to back up the backup directory on to itself, which will quickly fill it. This option is also useful when backing up the root partition, to stop it trying to back up virtual filesystems such as /dev, sys and /proc. To exclude particular files or directories, you can use the --exclude option:

rsync -avxz --exclude '*.c' --exclude '*.h' --exclude .thumbnails ...

Note that the first two exclude patterns are quoted to stop the shell interpreting the * characters, while the third excludes an entire directory. Your command line is going to get very long if you have a lot of files to exclude, but you can put the patterns (no need for quotes this time) in a file one per line and then call:

rsync -avxz --exclude-from ~/myexcludes ...

The exclude options are fine for filtering out simple patterns or single directories, but what if you want more sophisticated filtering? The --filter option provides this and it's comprehensive enough to have its own section in the man page, which you should read carefully before trusting the security of your data to the rules you create. However, you can simplify the use of filters by putting the exclusion and inclusion rules in a file called .rsync-filter and adding -F to rsync's options. This argument tells rsync to look for .rsync-filter files in every directory it visits, applying the rules it finds to that directory and its children. The format of .rsync-filter would be

exclude *.c
exclude *.h
exclude .thumbnails

You can use include rules as well as exclude. Each file is tested against each of the rules until one matches, when it is included or excluded according to the rule (subsequent rules are not checked). Files that match no rules are included. This can be used to include some files that would match an exclude rule by placing an include rule before it. You can also use this to only backup specified directories with something like this:

include /mail
include /documents
include /photos
exclude *

The leading / makes the directory match against the beginning of the path in the directory you are backing up, not the root filesystem. If these rules are in /home/white/.rsync-filter, the first one matches /home/white/mail. This should be enough to get you started, but you must read the man page to be sure you understand what you are doing. Remember that the second rule of backups is that you must verify them after making them (the first rule is to do them in the first place!).

Back to the list