Automated Backups With rdiff-backup
Automated Backups With rdiff-backup
This tutorial describes how to do automated server backups with the tool rdiff-backup. rdiff-backup lets you make backups over a network using SSH so that the data transfer is encrypted. The use of SSH makes rdiff-backup very secure because noone can read the data that is being transferred. rdiff-backup makes incremental backups, thus saving bandwidth.
Please find out more about rdiff-backup's features here: http://www.nongnu.org/rdiff-backup/index.html
The problem is that SSH requires a password for logging in which is not good if you want to run rdiff-backup as a cron job. The need for a password requires human interaction which is not what we want. For example, to backup the directory /boot of server1.example.com, you would type rdiff-backup email@example.com::/boot boot on your backup server (in this tutorial we name it backup.example.com) which would try to save server1.example.com's directory /boot in backup.example.com's directory boot. Now this is what happens:
You see, in line 2 you are asked for the root password of server1.example.com.
But fortunately there is a solution: the use of public keys. We create a pair of keys (on our backup server backup.example.com), one of which is saved in a file on the remote system (server1.example.com). Afterwards we will not be prompted for a password anymore when we run rdiff-backup. This also includes cron jobs which is exactly what we want.
Oh, as you might have guessed already from what I have written so far, the concept is that we initiate the backups of server1.example.com directly from backup.example.com; server1.example.com does not have to do anything to get backed up.
This howto is meant as a practical guide; it does not cover the theoretical backgrounds. They are treated in a lot of other documents in the web.
This document comes without warranty of any kind! I want to say that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!
Step 1: Install rdiff-backup On server1.example.com And backup.example.com
First we have to install rdiff-backup on both server1.example.com and backup.example.com. On Debian systems you can simply do that by running
apt-get install rdiff-backup
On other distributions the installation is different (on Fedora it might be something like yum install rdiff-backup, on Mandriva urpmi rdiff-backup, and on SuSE you should use yast to install rdiff-backup).
Step 2: Create The Keys On backup.example.com
On backup.example.com, we create a group and an unprivileged user called rdiff-backup. This user rdiff-backup will run the backups. We do not want root to run the backups for security reasons!
groupadd -g 3500 rdiff-backup
The second command creates the user rdiff-backup with the home directory /backup (which is created automatically by this command if it does not exist already) who is not allowed to login on the shell (again for security reasons). If the group ID and user ID 3500 are already in use on your system, replace them by another (free) ID.
su -m rdiff-backup
With this command you become the user rdiff-backup on the shell. All the following commands must be run as user rdiff-backup!
Create the keys:
You will see something like this:
It is ok to save the key in /backup/.ssh/id_rsa so you can simply hit enter. It is important that you do not enter a passphrase otherwise the backup will not work without human interaction so again hit enter. In the end two files are created: /backup/.ssh/id_rsa and /backup/.ssh/id_rsa.pub.
Next create the file /backup/.ssh/config with the following contents:
The value of host is what we use later on to start the backup. You can use any name the you like (e.g. server1_backup, this_is_the_machine_i_want_to_backup, etc.) (but it should not contain whitespace; underscores are ok).
Change the permissions of that file:
chmod -R go-rwx /backup/.ssh
Now we copy over our public key to server1.example.com:
ssh-copy-id -i ~/.ssh/id_rsa.pub firstname.lastname@example.org
This will look like this:
Once again you have to type in the root password of server1.example.com. What this command does is it copies the public key of the user rdiff-backup to the file /root/.ssh/authorized_keys on the remote server server1.example.com.