My laptop (a Macbook uni body that I picked up at the end of 2009) goes everywhere with me. Now that I am working on some serious stuff, it would be a damn shame if my laptop were to get damaged, stolen, or whatever. Thus recently I bought a cheap desktop computer from Futureshop, dumped Slackware on it, and set up rsync to run as a daemon. This setup allows me to remotely sync my laptop (certain directories, at least) with my desktop.
In case you don’t know about it, rsync is one of the coolest programs around. In a nutshell it syncs files and directories across networks. This blog post won’t go through the nitty gritty details involved with setting up rsync to run as a daemon (meaning it runs constantly, listening for connections), but I will briefly go over my personal setup that keeps my machines in sync.
I started out by picking a few directories on my laptop that I wanted to keep in sync: my documents, my pictures, and, of course, my code
. I then set up rsync modules corresponding to each directory. An rsync module is essentially a name that is attached to a directory on the server. So for example the module “bryan_code” corresponds to the directory /home/bryan/Code.
Next I wrote a shell script to automatically call rsync over a list of directories to sync. It responds to two commands: “push”, which pushes the laptop files onto the server; and “pull”, which yanks the server files down to the laptop. I have released the full script under the MIT License, and it can be found here. Hopefully it will be useful to you.
Next I started looking for a way in which I could tell my laptop to push its files up to my server daily, so that I don’t have to constantly run the script manually. I looked into using cron jobs, but when my laptop is asleep (which is often) it may miss its scheduled uploading. Luckily OSX has a program called “periodic“, which works just like cron, but if the machine for some reason misses its scheduled job, it runs the job as soon as it possibly can.
Therefore all I had to do was dump a small script into /etc/periodic/daily in order to have daily automatic backups! The script looks like this:
#!/bin/bash
script=/Users/bryan/bin/sync
log_file=/var/log/sync.log
if [ -e $log_file ]; then
sizeof_file = `du -k $log_file | cut -f1`
if [[ $sizeof_file -gt 1024 ]]; then
rm -f $log_file
fi
fi
date >> $log_file
$script push >> $log_file
This dumps all output to /var/log/sync.log, in case something goes wrong and you need to see what screwed up. And voila! Automatic backups!