Painless Automatic Backups
As a student I understand the consequences of having a hard drive fail or listening to that agonizing crunch your laptop makes when bumped off a desk and trampled on by other cogs in the machine. In fact, nearly every college class syllabus I've had handed to me has a "make backups - you are responsible. No exceptions" clause.
But also, as a student, I don't need a complex bi-hourly, onsite raid 10, and offsite rotating tape-backup solution. All I really want is for my files to exist in multiple places: my laptop, my desktop, my external HDD. This isn't difficult to do manually, and that is the way I did it for about a week; until my inner hacker cried out in despair. I shouldn't have to think about this! It should just happen.
The following is a collection of scripts/utilites I pulled together from various resources (all found with our reliable friend. It is all provided as-is.. and if you somehow end up with an empty hard drive instead of backups.. I've washed my hands.
Note: This is a "How I did it" not a "How you should."
The Tools
I chose these, because they are lightweight, common, and simple!
- rsync - a nifty utility "that provides fast incremental file transfer."
- flexbackup - "a flexible backup tool" ;)
- ssh - because I am a freak about security and encryption.
I also have some little scripts I wrote or got from an article similar to this one.
- validate-rsync - validates rsync connections (from Troy johnson
- rsync-hostname-backups - a script designed to be used in cron and easily copied.
The Setup
My setup here in my room is simple. I have a laptop, which I do pretty much all my work on when I am out and about. Back at the HQ is my desktop; I prefer to use my desktop when I'm near it, so beside simply backing things up I like to keep certain folders synchronized. As you will see this is cake with the tools I listed above.
Ok! First I installed everything - on both machines. That was as easy as installing flexbacku - rsync and ssh are pretty standard.
I have a ~/.backups/ directory on both computers (it is a symlink on the desktop to my external). I want my school files, documents and other things on my laptop to be backed up automatically and mirrored on my desktop.
The backing up bit is cake with flexbackup. Here is the _important_ bits of my /etc/flexbackup.conf
$type = 'tar';
$set{'school'} = "/share/School/";
$set{'docs'} = "/home/user/docs/";
$compress = 'gzip';
$compr_level = '6';
$device = '/home/user/.backups/';
Then I specified a cronjob. I use fcron, because it it can run a job after restarting if it was missed. This is what went in my crontab.
# A full backup every saturday at 5 AM 0 5 * * 6 flexbackup -set all -full # A differential backup every wednesday at 5 am 0 5 * * 3 flexbackup -set all -differential # An incremental backup every day except wednesday and saturday at 5 am 0 5 * * 0-2,4,5 flexbackup -set all -incremental
Voila, automatic archiving of my files is done.. once I test it all of course. Now lets mirror the directories I want mirrored.
As I mentioned I am an encryption nutso - I prefer all my traffic to anywhere, whether it be local or not, to be encrypted. So we are going to use a little script to plop into the crontab that will rsync through ssh. Of course, since everything is automatic I can't be around to enter a password for authentication. Following are the commands I used to create a trusted key/pub key.
user@localhost ~ $ ssh-keygen -t dsa Generating public/private dsa key pair. Enter file in which to save the key (/home/user/.ssh/id_dsa): /home/user/.ssh/rsync-key Enter passphrase (empty for no passphrase): [Press Enter Here] Enter same passphrase again: Your identification has been saved in /home/user/.ssh/rsync-key. Your public key has been saved in /home/user/.ssh/rsync-key.pub. The key fingerprint is: 1b:e2:a3:0a:7e:43:3d:37:50:b6:da:74:04:03:74:33 user@localhost ~ $ scp ~/.ssh/rsync-key.pub remote-hostname: Password: rsync-key.pub 100% 607 0.6KB/s 00:00
I created a keypair then copied the _public_ portion to the other computer. Now on the remote computer I ran this command:
cat ~/rsync-key.pub >> ~/.ssh/authorized_keys
Now the key can be used to make connections to this host, but these connections can be from anywhere and they can do anything that the user can do. Not a good thing ;) Here are the changes I made to ~/.ssh/authorized_keys
ssh-dss AAAAB3NzaC1kc3MAAACBANS3udL/Oq6yWUf430hN1srQNNsIAe0tF1YuqLchAG5aH4M9gHZyiG5p ZQ5UH+XzlHtbjI5x+yBT0zDeEqVhniF3M7JOz3H5EitHGK4AOF7GkUEEO/zV8ZDATd+XYfl1dmyQTzURwou3 kNnvGw9/rKo+BSiavw7H09Ax4LvVG5qxAAAAFQCLzw/V/vgtmF23DKIiJXueno6NcQAAAIEAtIDQyHh2pgvR k7qRZUGkpfu1PuaklPxTnMxd3Z4i8hE0lznFANBQzSNYXe18DVSJNL2T6STMzlEZZPzSzjQgQwpmZiev+9MO 90U6QGqP1kQKmFU/u8D2Wj2s67bZvVKHotilKgMW75tiBCfkNrIP7ohBBymY+ktpL69Mxe0KTVcAAACAJdc0 jNpIaOsb0ZGeVT/zYUkJ31DaIjJndFYaaofpL2yeapr0qATaCcXNBUh0hYkJOZP+uSlDJZXdNdfcdrJHillp o6UCqxHc9uuXC2GLd4P4Pg2kEcKIxYF3DMv54HaD2CG5jieUAYIkFKgOqfGlTKTDaTqJ73huVCTDvzBK5E8 = user@localhost
TO:
from="192.168.0.237",command="/home/user/path/to/validate-rsync" ssh-dss AAAAB3NzaC1kc3MAAACBANS3udL/Oq6yWUf430hN1srQNNsIAe0tF1YuqLchAG5aH4M9gHZyiG5p ZQ5UH+XzlHtbjI5x+yBT0zDeEqVhniF3M7JOz3H5EitHGK4AOF7GkUEEO/zV8ZDATd+XYfl1dmyQTzURwou3 kNnvGw9/rKo+BSiavw7H09Ax4LvVG5qxAAAAFQCLzw/V/vgtmF23DKIiJXueno6NcQAAAIEAtIDQyHh2pgvR k7qRZUGkpfu1PuaklPxTnMxd3Z4i8hE0lznFANBQzSNYXe18DVSJNL2T6STMzlEZZPzSzjQgQwpmZiev+9MO 90U6QGqP1kQKmFU/u8D2Wj2s67bZvVKHotilKgMW75tiBCfkNrIP7ohBBymY+ktpL69Mxe0KTVcAAACAJdc0 jNpIaOsb0ZGeVT/zYUkJ31DaIjJndFYaaofpL2yeapr0qATaCcXNBUh0hYkJOZP+uSlDJZXdNdfcdrJHillp o6UCqxHc9uuXC2GLd4P4Pg2kEcKIxYF3DMv54HaD2CG5jieUAYIkFKgOqfGlTKTDaTqJ73huVCTDvzBK5E8
That is somewhat more secure, but still allows rsync access to and from any file on the machine (that the user can access). don't forget about the validate-rsync file. Now all I did was edit the rsync-hostname-backups script, made it executable, placed in somwhere unobstrusive and edited the crontab again:
0 4 * * * /home/user/path/to/rsync-hostname-backups
And thats that. The cool stuff happens in the validate-rsync and rsync-hostname-backups scripts... check them out. If I forgot something, or if something doesn't make sense let me know.
happy backup times by KC