Backup Rotation with Date

Again (see ring buffer), this is a post, which shows an approach I will not need, since my backup script will change encore un fois.

I began to write a backup script using hard links and rsync in order to have incremental backups inspired by incremental backups with rsync. But after a little bit of hassle with bash script a friend gave me the hint that rsnapshot is out there and this little perl tool is either enlightened by Mike Rubels approach. So I will have to change my backup strategy from ‘push’ to ‘pull’ and make some other subtle changes, but don’t have to bother with ring buffers and backup rotation, because rsnapshot has already worked that out for me.

But since it might be helpful for someone, here is what I’ve done in order to rotate backups by date. Be aware, that this script does not do anything it has been work in progress till the end:

# leaving out 'source' lists the files found on destination
# so grepping for hourly and sorting it and 'tail -n 1' will reveal the last entry
lasthourly=`rsync ${host}::backup | grep "hourly-" | sort | tail -n 1`

# will give us the hourly backup 24 entries back (this is more reliable 
# than going back 24 hours since there might be some backups skipped!!!)
deletehourly=`rsync $host::backup | grep "hourly-" | sort | tail -n $counthourly | head -n 1\`

# e.g.: 20110512
currdate=`date "+%Y%m%d"`
# e.g.: 08
currhour=`date "+%H"`

# This format ensures, that the 'sort' command above will sort the entries in the 
# right order since it will sort them lexicographically
destinationhourly="hourly-$currdate-$currhour"

So, I used the feature of rsync to simulate an ‘ls’-command by omitting a ‘source’ and the ‘sort’/’grep’/’tail’-command in combination with good old UNIX pipes, in order to have a backup solution that pushes the data instead of pulling. It even omits the need of the bunch of ‘mv’ calls necessary in Mike Rubels solution and supports rotation without the need of a ssh connection (which is nice for a cronjob) facilitating a rsync daemon. That is especially helpful, when you need to dump a database before backupping it or similar things on the ‘source’. But still the effort to code and debug this solution was not worth the advantages. If someone likes this solution and implements it, please tell me!

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha * Time limit is exhausted. Please reload CAPTCHA.