Thursday, March 27, 2008

Auto back up your data

Losing your computer data is a bad thing. One good habit is to regularly back up your data. But manually doing this is too cumbersome and so we need automated scripts to back up the data. Here is a small script that I use to back up my own desktop daily. It uses rsync protocol over ssh. You need to have ssh, rsync and ssh public-private key pairs for your back up machine. Setting up SSH key authentication is easy and here is one of the link http://www.ece.uci.edu/~chou/ssh-key.html

Add this script to your cron settings. For linux copy this file to /etc/cron.daily/ directory.

The benefit of using rsync is that only changes over the last week will be exported and it will save network bandwidth. SSH provides the encryption layer so that others cannot snoop on your data when you are backing up. This code is under Public Domain License. Customize it to your needs and auto back up your home directory from now onwards.

#!/usr/local/bin/bash

RSYNC=rsync
SSH=ssh
KEY=/home/sushant/.ssh/id_rsa
RUSER=sushant
RHOST=mybackupmachine.com
RPATH=backup/daily/`date +%u`
LPATH=/home/sushant/

$RSYNC -avxz --exclude=".*" --force --delete --delete-excluded --ignore-errors -e "$SSH -i $KEY" $LPATH $RUSER@$RHOST:$RPATH
 
Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 2.5 India License.