Rotating backups

software development

#1

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.


#2

Set up another daily cron job running something like this:

[code]#!/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
[/code]
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).


If you want useful replies, ask smart questions.


#3

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


#4

Thanks for sharing.