Last Updated: February 25, 2016
·
320
· jackdoe

daily rotating backups

backup 7 days:

$ mysqldump -uXX -pXX --all-databases > /BACKUP/$(expr \( $(date "+%s") / 86400 \) % 7).sql

will create and rotate thru:

/BACKUP/0.sql
....
/BACKUP/6.sql

you can date +%s returns the epoch time stamp, (seconds since 01.01.1970) and you just need to divide it by the interval of which you want (86400 = 24 * 3600) and then just mod number of entries you want to keep

/BACKUP/$(expr \( $(date "+%s") / 3600 \) % 24).sql - keep 24 hours, one per hour 
/BACKUP/$(expr \( $(date "+%s") / 300 \) % 10).sql - keep 50 minutes, one per 5 minutes

of course when you are extracting the data you will have to look at the ctime of the files