|
Personal Backups with rdiff-backupcomment [] trackback []What is rdiff-backupQuoting their web page:
What We Are Going To DoIn this article I will not give a tutorial on rdiff-backup, I will just use it as a simple personal backup tool. For this, I could use a mirroring tool, which would be OK, but rdiff-backup is both easy to get and easy to use, and most important, I know how it works ;-) 1 So, here's the general plan:
What This Is NotThis is not a real, or serious backup solution!
So, why bother? Because this is easy, and not having any backups is worse. And tell me the truth... do you have any backups of your personal folders? I am of the opinion that if the real solution is hard, it's often a good idea to spend the effort it takes to really solve the problem. But I am also of the opinion that sometimes, if the real solution is too hard, most people is not going to care enough about the problem to actually solve it. And in those cases, there is room for an almost good enough non-solution. The Real StuffIf you want to perform real, serious backups, I recommend you look at Amanda or Bacula or some other of the plethora of backup solutions available, define a policy, and start doing offsite (or at least off-system) backups. Some are good, some are very good, almost all of them are better than this article's suggestion (in some way at least ;-) Getting rdiff-backupIn their homepage you can find Fedora RPMs, and binaries for some other OSs or distributions. I am using Red Hat 9, and compiled it from the .src.rpm without problems, after getting librsync from the same page, also in .src.rpm format. Backing Up Your Stuff With rdiff-backupStep 1As root, create a folder somewhere, where your backups will go. I will use /home/backups in this example, you can use whatever you want, but:
The space thing is like this: The backup will be just as large as whatever you want to backup. Then it will get larger with time, until it becomes, maybe, many times as large, depending on how many older versions you want to keep of your data. If you are not root, then you can still do it, but the backup will lose some of its properties (for example, a trojan or a mistake would be able to delete it!) Step 2Decide what to backup. For example, I want to backup /home/ralsina/projects, where all my in-progress stuff is stored, and /home/ralsina/.kde, where the configuration (and more) of KDE is saved. Step 3Let's try it. Here's a shell script for my example:
#!/bin/sh
# Where the backups go
target=/home/backups
mkdir -p "$target"
# Just put all the folders you want backed here. If they
# have strange characters in the name, just put them in
# single quotes. If you are not sure, quote them anyway.
for folder in /home/ralsina/projects /home/ralsina/.kde
do
# If the folder being backed up contains ".."
# something bad is going on
if echo "$folder" | grep ..
then
continue
fi
mkdir -p $target/$folder
rdiff-backup $folder $target/$folder
done
chown root.root -R $target
chmod o-w -R $target
The purpose of the chown and chmod at the end is to make sure no one can modify the backups. Think of it as storing them in a vault. If you are backing up stuff from various users, that will be wrong since it's making root own everything. Here's an alternative:
#!/bin/sh
# Where the backups go
target=/home/backups
mkdir -p "$target"
# Just put all the folders you want backed here. If they
# have strange characters in the name, just put them in
# single quotes. If you are not sure, quote them anyway.
for folder in /home/ralsina/projects /home/ralsina/.kde
do
# If the folder being backed up contains ".."
# something bad is going on
if echo "$folder" | grep \\.\\.
then
continue
fi
mkdir -p $target/$folder
rdiff-backup $folder $target/$folder
done
chown root.root $target
chmod 700 $target
This version preserves the owners of everything, but it "locks" the entrance to the backup folder. This way, the backups are safe from accidents, but only root can restore stuff... which may be too inconvenient. You can just remove the chown/chmod commands, and that version will keep backups, but not protect them against the user himself. So, take your backup script, save it somewhere with a reasonable name like, in my case, /usr/local/bin/ralsina-backup, make it executable 2 RestoringTo get back the last version of a file from the backup, you could just copy it from the backup directory. You can restore both recent and old versions using the --restore-as-of option of rdiff-backup. For example: rdiff-backup --restore-as-of (time) /home/backups/home/ralsina/projects/doc.txt Will restore the /home/ralsina/projects/doc.txt file from moment (time), where (time) has this format (quoting the riff-backup docs):
Limiting the Age of BackupsThe scripts given above would keep old versions of files forever. That is not practical, since it would require a huge amount of disk space. To limit that, you can use the --remove-older-than option. For example, if we used rdiff-backup --remove-older-than 2W $folder $target/$folder It would remove files older than two weeks. The format of the date for removal is the same as the one given above for restoring. Automating The ProcessYou can take advantage of cron to make this all run automatically. Just create an entry in root's crontab to do this every night, or whatever. Here's an example daily backup at midnight: 0 0 * * * /usr/local/bin/ralsina-backup 2>&1 | mail ralsina This sends a mail to account ralsina in the local box containing the output of the command. If you are not familiar with cron, you can do it by hand. I suggest you get familiar with it, or use a graphical cron management tool. Offering Automatic Backups For All UsersA more advanced backup script could read a certain file (say "backupthis") from each user's folder, and get from there the information on what to backup. Here's a quick and dirty version:
#!/bin/sh
for homefolder in /home/*
do
if [ -f $homefolder/backupthis ]
then
while read $homefolder/backupthis
do
# If the folder being backed up contains ".."
# something bad is going on
if echo "$folder" | grep \\.\\.
then
continue
fi
mkdir -p $target/$homefolder/$folder
rdiff-backup $homefolder/$folder $target/$homefolder/$folder
done < $homefolder/backupthis
fi
done
Notice that for this script, if I wanted to backup /home/ralsina/projects, in my backupthis file I would have to put only projects. This is intentional, and the goal is that users can only backup stuff from their own home folders. This script is not meant for arbitrary input. It's not really well written. If a user really really wanted, he could find ways to make it do weird and dangerous stuff ( It's quick and dirty as I said ;-) Other Possible UsesYou could use almost this exact procedure for keeping two home folders sync'd over the Internet, thus letting you use both computers and have all your data at hand. To do that, read rdiff-backup's information about using it over ssh. However, Unison is probably a better idea. Final WordsI Hope you find this useful. rdiff-backup is a very cool tool, and has helped me well in a couple of situations. It's very efficient, and simple to use. However, if you start using these scripts, I strongly suggest you learn more about rdiff-backup from its docs, and its wiki
last change 2004-03-22 10:13:36 |
Presenting rdiff-backup, and a few simple scripts for backing your personal data safely. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
© 2004-2005, Roberto Alsina