09.12.2023, 19:27 UhrDeutsch | English
Hallo Gast [ Registrierung | Anmelden ]

kanotix.com FAQ (häufig gestellte Fragen)

Kategorie: Index ->



Backing-Up the System with rdiff-backup

rdiff-backup is a tool used to backup your files. It can be run on a variety of *nix ports, but this howto will assume debian based linux. Note: run the commands in this howto as root unless told otherwise.


  • is great for recovering messed up dist-upgrades, kernel upgrades etc (and also just for restoring individual files).

  • only backs up what's changed like rsync does (so each backup doesn't take long).

  • keeps a history of changes (which means that you can restore a file you deleted three weeks ago!).

  • does secure backups over a network (using ssh).

  • backups up partitions while they are mounted (so it's easy to automate a daily backup ... no unmounting necessary).

  • can restore everything if your hard drive goes belly-up and you have to buy a new one.

  • scales to backup large networks (linux is fine, windows is more difficult) and is used by businesses.

  • is a command-line-driven app, so it's great for those who like to do things like automate backups in a powerful way (ie a bash script which is called by cron).

  • remembers and deals with file ownerships and permissions, and also deals with symbolic links (and all that sort of stuff) so when you restore, you get things exactly as they were.

What you'll need

rdiff-backup keeps an entire (uncompressed) copy of the files you are backing up, and it also keeps a history (incremental backups), so this means that your backup space needs to be larger than what you're backing up. If you are backing up 100 gigs of space, you may need 120 gigs of space (on a separate hard drive preferably!).

How to set it up

Let's say that your pc has the following:

  • a 100 gig hard drive (hda) which is in use, with mounts hda1 used for the root partition, and hda5 used for storing music and other files, and hda6 for swap.

  • a spare 200 gig hard drive (hdb) which is not in use, with one mount hdb1 ... we'll use this one for our backups.

  • IP address

The first thing to do is to install rdiff-backup:

# apt-get install rdiff-backup

Now although we can backup any directory, lets go the whole hog and back up entire partitions ... we want to back up hda1 and hda5 (we don't want to backup hda6), and so we make some directories to store the data:

# mkdir /media/hdb1/rdiff-backups/
# mkdir /media/hdb1/rdiff-backups/

(or /mnt if that's what's in your /etc/fstab specially KANOTIX version till 2005-04)

We want the IP address distinguished because we want this computer to also backup another one (covered later on).

Backing up

rdiff-backup uses the syntax "rdiff-backup <source-dir> <dest-dir>" ... note that we always specify directory names, not file names.

To back up hda5, we can run:

# rdiff-backup /media/hda5 /media/hdb1/rdiff-backups/

And to back up the root partition, we run:

# rdiff-backup --exclude '/tmp/*' --exclude '/proc/*' --exclude '/sys/*' \
> --exclude '/media/*/*' / /media/hdb1/rdiff-backups/

Any "AF_UNIX path too long" errors can be ignored. This may take a while as it's the first time the partition has been backed up, so rdiff-backup will have to back up the entire partition (not just the differences). Notice that we don't want to back up /tmp as this always changes, nor /proc or /sys as these don't contain real files, and also we don't want to back up mounts. Note that if we did back up mounts, then we would be backing up hdb1, and could get into an infinite loop! One way around this is to backup the mounts separately. Now the reason that I've put '/proc/*' instead of just '/proc' is that this will cunningly still backup the directory name /proc, but will ignore everything inside. The same is true to /tmp, /sys, and even more cunningly all the mount point names. This way, if we destroy our root partition and do a full restore, /tmp, /proc, /sysand the mount points names will be created (just as they should). If /tmp doesn't exist when X starts up, it may complain (something I learned the hard way ... for more hard lessons learnt, keep reading). See the man page for more on --exclude and --include.

Restoring directories from backups

rdiff-backup uses the syntax:
rdiff-backup -r <from-when> <source-dir> <dest-dir>

Now if we were to accidentally delete the directory /media/hda7/photos, then we can restore it by doing:

# rdiff-backup -r now /media/hdb1/rdiff-backups/\
> /media/hda5/photos

The "-r now" option means restore from the latest backup. If we have been backing everything up periodically (via crontab, say), and we didn't realize that the photos directory was missing until a few days later, we would need to restore from a backup from a few days ago (and not "now", as the latest backup says that the photos directory doesn't exist). Or perhaps we just want to get back to a previous version of something.
If we want to restore from three days ago, then we would use the "-r 3D" ... but, as the man page says, note:

"3D" refers to the instant 72 hours before the present, and if there was no backup made at that time, rdiff-backup restores the state recorded for the previous backup. For instance, in the above case, if "3D" is used, and there are only backups from 2 days and 4 days ago, the directory would be restored as it was 4 days ago (so you have to think about it before you restore).

Using the following will list the date and times of when the backups were made for hda5:

# rdiff-backup -l /media/hdb1/rdiff-backups/

Restoring partitions

You can also restore whole partitions (mounts), after all, a mount point is really just a directory.

WARNING: Don't restore the root partition while you are booted up into it!

While it may seem obvious that to ignore the above warning is not a good idea, I couldn't help experimenting ... I tried it because I wanted to see if the root partition could be restored while I was running out of that same root mount ... but and with one command I lost all the files on all my partitions, including all the backups on a separate hard drive!! rdiff-backup did exactly what it was instructed to do ... the backup for the root partition had empty mount points, and so in order to restore to how the backup is, it deleted everything in the mounts to make everything like the backup. It was a hard lesson, but it wasn't rdiff-backup's fault, it just shows the power of Linux.

To restore hda5 from the latest backup, we simply do:

# rdiff-backup -r now /media/hdb1/rdiff-backups/ /media/hda5

Restoring the root partition

But to restore the root partition, it's not so simple. Don't restore the root partition while it's mounted (see the above warning). It's really useful to be able to restore the root partition, as then if you mess up things when installing/upgrading, or mess up installing a new kernel (etc), you have piece of mind that you can roll things back to how you had them, and it'll only take 20 minutes.

One way to restore the root partition is to boot into a spare linux partition if you have one on your hard disk. Then since you will be able to restore the partition you want, as it won't be mounted as root. After you restore the partition, boot up into it and it will be exactly has it was when it was backed up ... exactly! This is by far the easiest method.

Another way to restore the root partition is to boot up a live cd (like "KANOTIX" http://kanotix.com/) and do the restore from there. rdiff-backup is included in KANOTIX since version 2005-04.rc18! For older versions of KANOTIX, you can type in the grub (Bootoptions, (Cheatcodes)) the cheatcode "unionfs" and that will mean you can install stuff on the Kanotix live cd!! Just boot up and do the following commands:

$ sudo su
# wget -O/etc/apt/sources.list http://kanotix.com/files/fix/sources.list.k32
# apt-get update
# apt-get install rdiff-backup

Now let's do the restore:

# mount /dev/hda1 /media/hda1
# mount /dev/hdb1 /media/hdb1
# rdiff-backup -r now /media/hdb1/rdiff-backups/ /media/hda1

If you don't have a KANOTIX CD and your Live-CD is supported by klik you can install rdiff-backup using the Klik and calling:

$ sudo ~/.zAppRun ~/Desktop/rdiff-backup_0.13.4-5.cmg rdiff-backup \
> -r now /media/hdb1/rdiff-backups/ /media/hda1

I recommend that anyone backup up their root partition (with the intention of restoring from it if they have to) should test the restoring process. Nothing worse than thinking everything will be fine, and then coming across something unexpected during an emergency.

Backing up other pc's

You can back up other pc's onto the local pc, as long as our local pc can ssh to the other pc's (and as long as you have room on your hard drive). The ssh server (sshd) needs to be running on remote pc. The other pc's don't have to be in your local lan, they could be anywhere in the world.

Let's assume that our remote pc has the following:
a 100 gig hard drive (hda) which is in use, with only mounts, hda1 used for the root partition, hda5 that we are storing some temporary files that we don't want backed up, and hda6 for swap
IP address

Note both 100 gig drives cannot usually be backed up on the 200 gig drive using rdiff-backup (since there would be no room for incremental files), but since we aren't backing up hda5 on the remote pc (and since hard drives aren't usually 100% full anyway, although don't rely on this) then we may calculate that we have enough room. Every time rdiff-backup performs another backup, more incremental files are created, and this takes up more and more room. We can tell rdiff-backup only to keep a max of 1 months backups (this command is shown later on), and this would take up less room than telling rdiff-backup to keep a years worth of data. And of course if we want a years worth of data backed up, then we have to have the hard drive space to store a years worth of incremental files.

The first thing we have to do is to install rdiff-backup on the remote pc as well (every computer that you want to backup including the backup server must have rdiff-backup installed).

To back the remote pc up onto the local pc, run on the local pc (ie

# mkdir /media/hdb1/rdiff-backups/
# rdiff-backup --exclude '/tmp/*' --exclude '/proc/*' --exclude '/sys/*'\
> --exclude '/media/*/*' /media/hdb1/rdiff-backups/

(Note the use of the double colons).

Now if we want to restore a directory on the remote pc, we can initiate the restore on either the local pc, or the remote pc.

This is how we would restore the directory /usr/local/games on the remote pc, by initiating it from the remote pc:

# rdiff-backup -r now\
> /usr/local/games

This is how we would restore the directory /usr/local/games on the remote pc, by initiating it from the local pc:

# rdiff-backup -r now /media/hdb1/rdiff-backups/\

Use the same sort of syntax when restoring your root partition from a live cd (where the remote pc has been booted up via live cd ... see above).

Automating backups:

If we are backing up other pc's onto our local pc first thing to do is enable password-less ssh logins using ssh keys. Note that we are talking about password-less ssh logins as root. This can be tied down so that only rdiff-backup commands are performed, but this is outside the scope if this howto (see the links at the end of this howto for more info, or google for howto's to do this). We are going to assume complete trust, and we are going to set up the simplest way of achieving password-less logins.

From the local pc, do the following:

# [ -f /root/.ssh/id_rsa ] || ssh-keygen -t rsa -f /root/.ssh/id_rsa

And press enter twice for blank passwords.

Then do:

# cat /root/.ssh/id_rsa.pub | ssh 'mkdir -p /root/.ssh;\
> cat - >>/root/.ssh/authorized_keys2'

(We'll be prompted for our root password, and this hopefully will be the last time we are asked for it).

Now we can ssh to the remote pc as root without having to type a password (go on, try it!), and rdiff-backup can be automated.

Next, create a bash script that contains all the rdiff-backup commands. Our bash script may look something like this:

echo "=======Backing up root======="
${RDIFF} --ssh-no-compression --exclude '/tmp/*' --exclude '/proc/*'\
--exclude '/sys/*' --exclude '/media/*/*'\
/ /media/hdb1/rdiff-backups/
echo "(and purge increments older than 1 month)"
${RDIFF} --remove-older-than 1M\
--force /media/hdb1/rdiff-backups/
echo "=======Backing up mount hda5======="
${RDIFF} --ssh-no-compression --exclude /media/hda5/myjunk /media/hda5\
echo "(and purge increments older than 1 months)"
${RDIFF} --remove-older-than 1M\
--force /media/hdb1/rdiff-backups/
echo "=======Backing up root======="
${RDIFF} --ssh-no-compression --exclude '/tmp/*' --exclude '/proc/*'\
--exclude '/sys/*' --exclude '/media/*/*' --exclude '/mnt/*/*'\ /media/hdb1/rdiff-backups/
echo "(and purge increments older than 1 months)"
${RDIFF} --remove-older-than 1M\
--force /media/hdb1/rdiff-backups/

Now we can name this bash script "myrdiff-backups.bash" and put it in /usr/local/bin on our local (backup server) machine, and change it to an executable. Run the bash script and make sure it works.

And lastly we can get cron to call it every night at 8pm. The following line in root's crontab will do the trick, call

# crontab -e

and insert the following line
0 20 * * * /usr/local/bin/myrdiff-backups.bash

And that should be it.

More info

man rdiff-backup

Thanks to Swynndla

nach oben
Deutsch | English
Logos and trademarks are the property of their respective owners, comments are property of their posters, the rest is © 2004 - 2006 by Jörg Schirottke (Kano).
Consult Impressum and Legal Terms for details. Kanotix is Free Software released under the GNU/GPL license.
This CMS is powered by PostNuke, all themes used at this site are released under the GNU/GPL license. designed and hosted by w3you. Our web server is running on Kanotix64-2006.