Thursday, May 6, 2010

Setting up automatic back-ups

To set up automatic back-ups from your machine to a remoteserver use rsync and cron/anacron commands that now come pre-installed on your Linux machine.

Here is the reference from the Linux gazette that I used to set up my backups. Since my machine is not necessarily turned on 24 hours a day, I used anacron in stead of cron to schedule my backups.

To set-up a new job for anacron, edit the /etc/anacrontab file
1 20 backup rsync -r -e ssh --delete /home/username/thesis username@remoteserver:backups/thesis/thesis/

For the above command to run you should have the public/private key for password-less login to the remoteserver set-up before hand.

On the remoteserver, you must set up cron-jobs to store timely snapshots of the backups. This would help if your current versions are corrupted and you would like to roll back to a previous version.

Here is the code that must be added to your cron file (edit it using crontab -e ) to do that (root access not required):

# Back up mail files with snapshots of 6, 4, 3, 2, 1 months and 3, 2, 1 weeks
# Order 4m->6m, 3m->4m, 2m->3m, 1m->2m, 3w->1m, 2w->3w, 1w->2w, mirror->1w

# At 3am on the 1st of Jan,Mar,May,Jul,Sep,Nov copy the 4m to the 6m
00 03 1 1,3,5,7,9,11 * cp -f /backups/thesis/backup/4month.tar.gz /backups/thesis/backup/6month.tar.gz

# At 3.02am on the 1st of every month move the 3m to the 4m (and continue for other months)
02 03 1 * * cp -f /backups/thesis/backup/3month.tar.gz /backups/thesis/backup/4month.tar.gz
04 03 1 * * cp -f /backups/thesis/backup/2month.tar.gz /backups/thesis/backup/3month.tar.gz
06 03 1 * * cp -f /backups/thesis/backup/1month.tar.gz /backups/thesis/backup/2month.tar.gz
08 03 1 * * cp -f /backups/thesis/backup/3week.tar.gz /backups/thesis/backup/1month.tar.gz

# And then every Sunday take care of the weekly snapshots and the archiving of the mirror
10 03 * * 0 cp -f /backups/thesis/backup/2week.tar.gz /backups/thesis/backup/3week.tar.gz
12 03 * * 0 cp -f /backups/thesis/backup/1week.tar.gz /backups/thesis/backup/2week.tar.gz
14 03 * * 0 rm -f /backups/thesis/backup/1week.tar.gz
16 03 * * 0 tar zcf /backups/thesis/backup/1week.tar.gz /backups/thesis/thesis/*