Rsync Backup Script
From XMission Wiki
Introduction
This is a sample backup script, utilizing rsync, which will backup a Linux or Mac OS X system to XMission's Remote Drive service. It can be adapted easily for other servers.
After the initial copy, it only backs up incremental changes, saving you time and storage.
Script
#!/bin/bash HOST=`hostname -s` EXCLUDE='--exclude=**/[Cc]ache/**' RSYNC_ARGS="-vaz --progress --delete --delete-excluded --delete-after $EXCLUDE" BACKUP_DIRS="/var/www /home /etc /usr/local /var/spool/cron /var/lib" SERVER="shell.xmission.com" SERVERDIR="backup" DATE=`date +%Y%m%d-%H%M%S` ssh $SERVER "if [ ! -f $SERVERDIR/log ]; then mkdir $SERVERDIR/log; \ fi; if [ ! -f $SERVERDIR/$HOST/daily.0 ]; then mkdir $SERVERDIR/$HOST/daily.0; \ fi" ssh $SERVER "rm -rf $SERVERDIR/$HOST/daily.8; \ mv $SERVERDIR/$HOST/daily.7 $SERVERDIR/$HOST/daily.8; \ mv $SERVERDIR/$HOST/daily.6 $SERVERDIR/$HOST/daily.7; \ mv $SERVERDIR/$HOST/daily.5 $SERVERDIR/$HOST/daily.6; \ mv $SERVERDIR/$HOST/daily.4 $SERVERDIR/$HOST/daily.5; \ mv $SERVERDIR/$HOST/daily.3 $SERVERDIR/$HOST/daily.4; \ mv $SERVERDIR/$HOST/daily.2 $SERVERDIR/$HOST/daily.3; \ mv $SERVERDIR/$HOST/daily.1 $SERVERDIR/$HOST/daily.2; \ cp -al $SERVERDIR/$HOST/daily.0 $SERVERDIR/$HOST/daily.1" for DIR in $BACKUP_DIRS; do rsync $RSYNC_ARGS $DIR ${SERVER}:$SERVERDIR/$HOST/daily.0/ >>/tmp/$HOST-$DATE done gzip --best /tmp/$HOST-$DATE scp /tmp/$HOST-$DATE.gz $SERVER:$SERVERDIR/log rm /tmp/$HOST-$DATE.gz
Cron Entry
You will need a cron entry to rotate out the old weekly directories and prune the log files, depending on how long you want to keep them. The below example keeps a month's worth of weekly incremental backups.
15 3 * * Sun backup-weekly >/dev/null 2>&1 0 5 * * * find backup/log -type f -mtime +14 -exec rm -rf {} \;
backup-weekly script:
#!/bin/sh cd backup for DIR in `ls -1d */daily.8 | awk -F / '{print $1}'`; do cd $DIR rm -rf weekly.[3456789] mv weekly.2 weekly.3 mv weekly.1 weekly.2 mv daily.8 weekly.0 cd .. done