Automated Incremental Backups With rsync
April 28th, 2007
When I installed my software RAID array, I was a little unsure of stability, mainly because I’ve never configured RAID before, not because the software is unstable. So as a precaution, I decided to create a backup scheme (which I should have had anyway).
Using some tools found on almost every Linux system, "rsync" and "cp", I wrote a script, to create incremental backups of my /home directory.
My code looks like this:
/usr/local/sbin/backup.sh
#!/bin/sh
SRC_DIR=/home
BACKUP_DIR=/backup
BACKUPS=14
# Delete the oldest backup
if [ -d ${BACKUP_DIR}${SRC_DIR}.${BACKUPS} ]
then
rm -rf ${BACKUP_DIR}${SRC_DIR}.${BACKUPS}
fi
SRC=0
for i in `seq ${BACKUPS} -1 2`
do
let SRC=$i-1
if [ -d ${BACKUP_DIR}${SRC_DIR}.${SRC} ]
then
mv ${BACKUP_DIR}${SRC_DIR}.${SRC} ${BACKUP_DIR}${SRC_DIR}.${i}
fi
done
if [ -d ${BACKUP_DIR}${SRC_DIR}.0 ]
then
cp -al ${BACKUP_DIR}${SRC_DIR}.0 ${BACKUP_DIR}${SRC_DIR}.1
fi
rsync -a –update –delete ${SRC_DIR}/ ${BACKUP_DIR}${SRC_DIR}.0
touch ${BACKUP_DIR}${SRC_DIR}.0
It incrementally backs up the entire /home directory to /backup, by placing it in my crontab:
@daily /usr/local/sbin/backup.sh
I can, without intervention, maintain 2 weeks worth of backups.
Since this is basically a single user system, and the backups are only meant to protect against my stupidity, I haven’t gone to the trouble of making the backups read-only. But this would certainly be an improvement to the system. A couple of solutions are discussed in the article that my script is based on. Mine only backs up to a different directory (which is mounted on a different drive), not to a separate system/location (although, this is a good idea if possible).
As a side note, I haven’t had any trouble with the RAID, but backups are still reassuring.



