Rotating backups

Rotating backups

Posted by: haggis
Posted on: 2005-08-09 12:23:00

Say I have a cron job that regularly dumps a few mysql databases into a backup directory, with the date suffixed, like so

mydatabase_08-06-2005.sql
myotherdatabase_08-06-2005.sql
mydatabase_08-07-2005.sql
myotherdatabase_08-07-2005.sql
mydatabase_08-08-2005.sql
myotherdatabase_08-08-2005.sql
... and so on ...

Is there a (very) simple way to automatically limit the number of dumps in that directory, for instance, to the ten freshest? I could save the dumps with the date-stamp as a prefix if it would help.

Re: Rotating backups

Posted by: kchrist
Posted on: 2005-08-09 13:46:00

Set up another daily cron job running something like this:

#!/bin/sh

PATH="/usr/local/bin:/usr/bin:/bin"

# Directory holding the backups
BACKUP_DIR="/home/username/db-backups/"

# Number of days to keep
DAYS="10"

DATABASES="mydatabase myotherdatabase"

for db in ${DATABASES}; do
find ${BACKUP_DIR}/${db}* -type f -mtime ${DAYS} | xargs rm -f
done

exit 0


This just checks the dates on the files and deletes anything older than the specified number of days (assuming you're doing your backups daily).

Re: Rotating backups

Posted by: haggis
Posted on: 2005-08-09 14:03:00

I may not understand it completely, but I can definitely use it. I shall sleep better at night. Thanks so much.

Re: Rotating backups

Posted by: section31
Posted on: 2006-05-09 21:46:00

Thanks for sharing.

Tags: sqlmydatabasecron jobdumpsmysql databasesbackup directorydate stampprefixbackupshelp