Backing up data with rsync

I recently discovered a tool which turned out to be useful for backing up my local folders, namely: rsync. It’s a straightforward command-line utility that makes it really easy to maintain a mirror of local data on any distant support. Basic usage for this purpose is:

$ rsync -auvz --delete ./local-folder/ /mnt/remote-folder/

Where:

  • -a stands for "archive" mode (preserve permissions, dates, etc.),
  • -u stands for "update" only, i.e. don’t re-send or overwrite newer files,
  • -v stands for "verbose", so we can see a list of files being processed,
  • -z enables file compression for faster transfers,
  • --delete means we delete files that don’t exist any more in the local folder.

The verbose mode enabled by -v is not necessary depending on your use case. These other parameters can also come in handy:

  • --exclude can be used to specify files or filename patterns to ignore,
  • --progress displays a progression bar, e.g. to check that a big file transfer is not hanging.

Check out man rsync for more details and options.

Trailing slashes

The only thing to be careful about with rsync is the addition, or not, of a trailing slash. If there is no trailing slash, the corresponding object is treated as a target, while if there is a trailing slash, the corresponding object is treated as a directory containing the target files. It's clearer with an example: suppose we have a directory foo containing the file README, and we want to sync it to a directory bar. If we do:

$ rsync -auvz ./foo ./bar/

Then foo will be copied to the target location bar/, and we will end up with the following file tree:

./foo
./foo/README
./bar/
./bar/foo
./bar/foo/README

On the contrary, if we do:

$ rsync -auvz ./foo/ ./bar/

Then the content of foo is synced with the content of bar, and we get:

./foo
./foo/README
./bar/
./bar/README

So, make sure that the source and destination folders are consistent trailing-slash-wise.

Discussion

Feel free to post a comment by e-mail using the form below. Your e-mail address will not be disclosed.

📝 You can use Markdown with $\LaTeX$ formulas in your comment.

By clicking the button below, you agree to the publication of your comment on this page.

Opens your e-mail client.