rotate backups

  • Canada
  • Windows
  • Mozilla Thunderbird
In my last scripts, I showed how to make daily ackups of mysql database dumps. This script
can be scheduled on a daily basis using
cron. This is good, but you will end up filling your hard disk with daily backup files. It
might be useful to have a history of
backups, but not necessarily for a long period. Let's say you want to keep a week's worth
of backups not more. You could add the
following script to your weekly cron jobs that would delete all backups done 2 weeks ago.

I explain again with an example:

The perfect month

S M T W T F S
wk1 1 2 3 4 5 6 7
wk2 8 9 10 11 12 13 14
wk3 15 16 17 18 19 20 21
wk4 22 23 24 25 26 27 28

Let's say our script runs every Sunday and it is the 15th. We have backups for wk1 and wk2
for each day of the format
backup-YYYYMMDD.bz2. We only want to keep the backups from last week (wk2), so we want to
delete every backup done on wk1.

This is what the following script does.

<SCRIPT>
#!/bin/bash
BACKUP_DIR="/backups"

logger -i -t backup "CLEAR BACKUPS: starting to delete last weeks backups."

pushd ${BACKUP_DIR} &> /dev/null
for i in {sunday,monday,tuesday,wednesday,thursday,friday,saturday}; do
MYDATE=`date +%Y%m%d --date="$i 2 weeks ago"`;
rm -f backup-${MYDATE}.bz2;
done
popd &> /dev/null

logger -i -t backup "CLEAR BACKUPS: finished deleting last weeks backups."
</SCRIPT>



--
Stefan Michalowski
Email: mitch(à)ptaff.ca
PGP Key: http://screamerone.zapto.org/k.asc
 

 

Propulsé par xhtmail