How to setup an unattended back-up process.

Jan 23, 2007 11:24 GMT  ·  By

rdiff-backup is an utility capable of maintaining a back-up mirror of a file or directory over the network, on another server. It stores incremental rdiff deltas with the back-up, which allows you to recreate any back-up point. The back-up is encrypted through the SSH protocol, thus no one will be able to read the data being transferred. Moreover, rdiff-backup is saving bandwidth by making incremental back-ups.

Prerequisites

As for all back-ups, you should be planning what you want to do, before actually doing it. Next, you should make sure that both computers are reachable from the Internet (or at least the computer you want to back-up) and whether both computers have SSH (client and daemon) installed and configured properly. As an example for this guide, I'll back-up a directory on my work computer, to a directory on my home computer.

Installation

- First of all, you'll need to install rdiff-backup on both computers. This is a very popular utility so it's most likely to find it in your distribution's repository so use your package manager to install it. Eg:

Fedora:

code
# yum install rdiff-backup
Debian/Ubuntu:
code
$ sudo apt-get install rdiff-backup
- Now for the tricky part. Because rdiff-backup is using SSH, which asks for a password upon logging in, it will require human interaction during the actual back-up process. And because we are trying to setup an automated process, this is not what we want. Fortunately, this problem can be easily skipped by using SSH public keys. So, we'll need to create a pair of keys on the home computer, one of which will be saved on the work computer. Basically, these keys will tell the work machine that the home machine is allowed to login through SSH.

Generate a DSA key pair on the home computer:

code
$ ssh-keygen -t dsa
Hit enter when you are asked for the target directory and for the passphrase.

Send the public key to the work computer: (The work computer has to have used the ssh client, otherwise, the .ssh directory won't exist)

code
$ cd $HOME
$ scp .ssh/id_dsa.pub [email protected]:~/.ssh
Log in to work PC and add the key to the list of authorized keys:
code
$ ssh [email protected]
$ cd .ssh
$ cat id_dsa.pub >> authorized_keys2
$ rm -rf id_dsa.pub
- Test if everything is ok: Log out and log back in, this time, you shouldn't be asked for a password.

Actual back-up process

- What we want to do here is back-up the scripts/ directory on the work PC to the home PC. To do this, type the following command on the home machine:

code
$ rdiff-backup -v4 --print-statistics [email protected]:://home/work-user/scripts/ scripts/
Automating the process with crontab

- On the home machine, type the following command to open the crontab editor:

code
$ crontab -e
It will open vi. If you don't know how to use it, I?ll try to explain shortly. Once it started, press INSERT key to start writing into it. Now, add the following line:
code
30 4 * * * /usr/bin/rdiff-backup [email protected]:://home/work-user/scripts/ scripts/
Now press ESCAPE key, then SHIFT + ; and finally, wq

As of now, every night at 4:30, the scripts directory from the work PC will be saved to /home/home-user/scripts directory and will be updated if any scripts are added to the work directory.