Wednesday, May 16, 2012

Incremental backup using RSYNC


This backup script was done in one of our machines where an external usb hard drive is attached locally. My former officemate Ian Dexter helped me get this script done, many thanks to him. So without further adieu:
Open up your favorite editor, I used vi for this.
#!/bin/sh
SRCDIR=/home
BAKHOME=/backup
BAKDIR=`date +%A`
LOGDIR=/backup/log
TMPDIR=/backup/tmp
OPTS="-av --force --ignore-errors --delete --backup --backup-dir=$BAKHOME/$BAKDIR"
RSYNC=/usr/bin/rsync
# Start
 date > $LOGDIR/backup.$BAKDIR.log
# Cleanup
 [ -d $TMPDIR ] || mkdir $TMPDIR
 $RSYNC --delete -a $TMPDIR $BAKHOME/$BAKDIR
 rm -rf $TMPDIR
# Backup
 $RSYNC $OPTS $SRCDIR $BAKHOME/current >> $LOGDIR/backup.$BAKDIR.log
exit 0
# Save the file as backup.sh or whatever name you prefer.

Now edit root’s crontab and run this script daily, choose an off-peak hour for this.
You now have a backup copy of all the files/directories under the /home from Monday to Sunday.

No comments:

Post a Comment