Using rdiff-backup to keep your files safe and secure!

Rdiff-backup is a small nifty python-based incremental backup software. Rdiff is similar to rsync, but adds support for incremental and revision-based backup. This way you can keep, in this example, up to 14 days of changes. Rdiff-backup also differs (!) from rsnapshot as it only stores the changes over time of a certain file, and this saves you a lot of storage. It’s extremely usable for database-backups, as they tend to change little but grow huge.

You might have to do small adjustments to stuff like users and parent directory. In this guide i use /root/ as parent directory for most actions, and all commands should work then.

Upgrading and Installing

It’s important that you do this on your target server and the server you are backuping, or else rdiff-backup will not be able to use ssh as it starts an rdiff-server on your target machine.

apt-get update
apt-get install rdiff-backup

Setting up SSH with keys

After installing rdiff we need to generate ssh-keys so that you can have automated rdiff-backups. I’ll only cover this briefly, but if you are unsure try to consult another guide i made about setting up key-based ssh authentication.

ssh-keygen
scp .ssh/id_rsa user@host:.ssh/id_rsa

This generates the keys you need and send them to the appropriate user on your remote machine for storing backups. If you are setting up more servers to use the same backup location copy id_rsa.pub over to them.

Using rdiff-backup

rdiff-backup -v5 –print-statistics /local-dir user@host::/remote-dir
rdiff-backup –remove-older-than 2W user@host::/remote-dir

Now we have created the first and initial backup, this might take some time, depending on your connection speed, disks and CPU. You should be able to watch what’s stores since we issued the -v5 comand (verbos 5).

Automating it

echo “#!/bin/sh” >> rdiff-backup.sh
echo “rdiff-backup -v5 –print-statistics /local-dir user@host::/remote-dir” >> rdiff-backup.sh
echo “rdiff-backup –remove-older-than 2W user@host::/remote-dir” >> rdiff-backup.sh

Running it

crontab -e

And insert the following line

* 0 * * * /root/rdiff-backup.sh

Save it! Now you can just sit back and relax and let ‘em backups flow, each night at twelve o’ clock.

Restoring

Maybe the most boring part but also the most important part, restoring. When you restore with rdiff-backup you can restore to any date you want, as long as it’s still being kept. In this guide we used 2W, or two weeks, so we should be able to restore a file 14 days back. Nifty!

rdiff-backup -r 14D user@host::/remote-dir/file /local-dir/file

This will restore the 14 days old file (-r 14D) of “file” to “/local-dir/file”. Pretty nifty, ey? If you are doing disaster recovery you can also recover an entire directory.

Final words

Do some simple tests, like backing up and restoring. This way you don’t get caught with your pants down when you actually need to restore something, and you ensure that backups actually are working – as this guide is not fully tested nor foolproof.

Leave a Reply