Quick and Dirty Backups with rsync

2 minute read

One simple way to back up files is to copy them to Dropbox. However, manually copying files does not constitute a good backup, you’re going to forget.

Also, you want to be smart about it and just copy things that have changed or are new. My favorite tool for smart copies is rsync.

If you aren’t familiar with rsync, it’s a program for intelligently syncing one directory tree with another. It looks for additions, changes, and, optionally, deletions in a source directory (and it’s subdirectories) and make the destination an exact copy. After an initial copy is created, rsync is very efficient as further syncs only copy changes. The form is simple:

rsync -a /source/path/ /copy/path

One slightly confusing and very important thing to understand is the role of the trailing “/” in the source path.

If you end the source path with a “/” then the contents of the source directory with be copied in to the destination directory, i.e if the /source/path/README.md exists, it will be copied to /copy/path/README.md.

However, if you leave the “/” off of the source path then source directory is created in the destination directory, i.e /source/path/README.md ends up in /copy/path/path/README.md.

Given that, these two commands are identical:

rsync -a /source/path/ /copy/path
rsync -a /source/path /copy

rsync can take multiple sources:

rsync -a /path/thing1 /path/thing2 /copy

will backup to /copy/thing1 and /copy/thing2

Give all that, creating a backup is as simple as:

mkdir ~/Dropbox/Backups
rsync -a /path/to/important ~/Dropbox/Backups

For automation, you can setup that command as a cron job. Run crontab -e and paste in:

@hourly /usr/bin/rsync -a /path/to/important ~/Dropbox/Backups

You can also use @daily or @weekly or read man 5 crontab for a range of other options. Always use the full path of the executable in your cron job, you can not count on PATH being set as it does not run you dotfiles.

An additional option is to add the --delete flag to the rsync command:

rsync -a --delete /path/to/important ~/Dropbox/Backups

This will remove any files from the backup that have been deleted in the source. I typically do this manually as I don’t want something I accidentally deleted to get automatically delete from the backup before I notice.

This is just a quick and dirty option and you should seriously look at more feature rich options. However, with very little fuss, it does the job of making sure you have copies of critical files.