I’ve now got backup scripts happily creating copies of all my subversion repositories and MySQL databases every 24 hours. This is great but it means you end up with an awful lot of backups. I realy don’t need a backup from every day going back forever. But it is nice to have snapshots going back into history in case something subtle has gone wrong.
What I’d really like is to copy the oldest backups into a different directory every seven days, and delete all the backups in the main directory that are older than seven days. Of course I’ll then end up with piles of backups building up in the weekly directory. So I’d like to go through the weekly directory every month, copy the oldest backups into another directory and delete all the weekly backups that are more than one month old.
To do this I give you the snappily named rotate-backups
rotate-backup rotates backups in a given directory weekly,or monthly -b directory to rotate backups in -f file containing list of backup dirs -t time period (weekly,monthly)
The config file is just a new-line separated list of directories. To make it work I put a script in /etc/cron.weekly like:
rotate-backups -t weekly -f /etc/rotate-backups
and one in cron.monthly:
rotate-backups -t monthly -f /etc/rotate-backups
The backup script makes an assumption that backups created on the same day are from the same backup run. It copies the ‘oldest’ backups by copying files from the same day as the oldest file in the directory. This way it doesn’t have to know anything about what you are backing up or what your naming conventions are.
Also it culls old backups relative to the date of the latest file in the directory. This means that if you stop taking backups the script won’t keep deleting files until you have none left.
Thank you very much for sharing these backup scripts. According to rotate-backups, I need to modify the script a little to make it works:
Line 35,36
I have to change from {print $6″ “$7” “$8} to {print $6” “$7}
Line 40: typo
daate should be date