Simple backups with rsync on Ubuntu

Backing up is something we all know we should do, but it always seems like such a hassle. Recently I decided to set up a simple way of backing up the main directories on my main Ubuntu machine. All I want is to copy the most important files to an encrypted external hard drive, with minimal work.

Originally I looked into using Ubuntu’s built-in Backups tool, but it wasn’t quite fit for what I wanted. Its main drawback is that, although it allows you to ignore specific directories, it doesn’t allow you to specify a pattern. As a front-end developer, I have a lot of node_modules directories, which contain thousands of files that do not need backing up. It turns out that the command-line tool rsync offers this, and everything else I need for backing up.

If you have heard of rsync before, you might think it is only for transferring between local and remote machines, but it is just as good at transferring between hard drives. There are a few flags to pass which make it work well for backing up. Below is the command I use. Note the backslashes \ are just there so that the command can be written across multiple lines for better readability.

rsync --archive --delete --progress --human-readable \
  --exclude node_modules --exclude tmp \
  ~/dev ~/Documents ~/Pictures \
  /media/gearoid/external-drive/Backup 

The --archive flag is the most important here. It actually sets several other flags behind the scenes, including making the rsync move recursively through directories, as well as maintaining ownership and permissions. The --delete flag removes files and directories at the destination that are no longer at the source. Essentially the whole command will create a mirror of the ~/dev, ~/Documents and ~/Pictures directories in a directory called Backup on my external hard drive.

If you’re looking for a very simple backup solution, have a look at rsync. It could be all you need.