Dustin Boswell - Brain Dumps about Computers, Programming, and Everything Else
dustwell.com About Me Past Academic Work
Blog Posts How to hash passwords Snapshotting with Rsync "An hour" vs. "A hour" Yes/No proposition bets div, span, and CSS "display:" Pair programming + screen SSH keys in 2 easy steps Fixing Your Flaky Internet How X-over-SSH works Drinking Distilled Water djb-dns installation Vim Cheat Sheet Oil ETFs leak money
Using Rsync to do local snapshotting/backups January 9, 2010
Jeff Atwood's recent data-loss story is a good reminder of why you should do off-site backups. But what if you just accidentally rm'ed a file, or saved over it's contents with bad data? It's a lot of work to do a full backup recovery of your entire system just to get back one file. Isn't there an easier way?

Local Snapshotting: not perfect, but super usefull

Here's what I do: run rsync with cron to copy all your important files to a local /backup directory.
First, create a new file called crontab.txt with contents like:
@hourly  rsync -a --inplace --max-size=1MB /www /etc /backup/hourly/
@daily   rsync -a --inplace --max-size=1MB /www /etc /backup/daily/
@weekly  rsync -a --inplace --max-size=1MB /www /etc /backup/weekly/
@monthly rsync -a --inplace /www /etc /backup/monthly/

Let me explain:

Now you install this crontab by doing:

crontab crontab.txt
I do this as the root user, but you can do this as any user that can read all the directories you need to backup, and can write to /backup. (Warning: the above command will overwrite any other crontabs you already have installed. Do a crontab -l first, to see what's installed.) Now you can just sit back and relax -- copies of your local files are being copied every hour, day, week, and month to the /backup directory. If you want to make sure you have a full (monthly) backup right away, then you should execute:
rsync -a --inplace /www /etc /backup/monthly/
right now.

How does this help me?

Let's say you just accidentally removed a local file
rm /etc/lighttpd/lighttpd.conf
# Oh shit, I didn't mean to do that!
Not to fear, you can recover it by doing
cp /backup/hourly/etc/lighttpd/lighttpd.conf /etc/lighttpd/lighttpd.conf

Why do hourly, daily, etc...?

For mistakes that you catch right away, the /backup/hourly directory is what you'll goto most often to get a recent version. But sometimes you don't realize something is wrong until days (or weeks) later. In that case, the hourly backup is of no use, since it has already mirrored the mistake. If you were really paranoid, you could add a @yearly line to the crontab above.
blog comments powered by Disqus