Time Machine like backups

If you backup your data to a remote server or a local disk with a script (I strongly recommend rsync to do so, otherwise this script will most probably not work), you should also keep old versions of that data to be able to rollback to an earlier version. But if you have a LOT of data, your HD will run full. So better just keep the deltas.

In my case its half a terabyte of email data from a mail server. Many files change every day. Get deleted, added, moved.
To keep old versions (you could call them snapshots) of the backups, I use hardlinks, that will just create a new link to an existing file in the filesystem. It is not a symbolic link, meaning the actual file will only get deleted, when the last hardlink will be deleted from the filesystem. In my case, this will happen 10 days after the original file was deleted.

#!/bin/bash

BASEDIR="/var/bkp/" # base directory
BDIR=mybackup # directory to backup
DAYS=10
cp -al $BASEDIR/$BDIR/ $BASEDIR/$BDIR$(date +%Y%m%d)
if [ -d $BASEDIR/$BDIR$(date +%Y%m%d --date="-$DAYS day") ]; then
 echo "deleting old backup $BDIR$(date +%Y%m%d --date="-$DAYS day")..."
 rm -rf $BASEDIR/$BDIR$(date +%Y%m%d --date="-$DAYS day")
else
 echo -n "No old backups at "
 date +%Y%m%d --date="-$DAYS day"
fi
Time Machine like backups