add cleanup-backups.sh

This commit is contained in:
Olaf Rempel 2006-07-26 17:19:44 +02:00
commit 97cb7ad208

47
cleanup-backups.sh Normal file
View File

@ -0,0 +1,47 @@
#!/bin/bash
# format: db-dumps-YYYY-MM-DD.sql.gz
BACKUPS="/home/backups/db-dump-*"
DAILY_COUNT="14"
WEEKLY_COUNT="8"
MONTHLY_COUNT="12"
# -----
TODAY=$(date +%Y%m%d)
TODAY_WEEKDAY=$(date +%w)
TEMPFILE=$(mktemp)
# create list
for offset in `seq 0 $DAILY_COUNT`; do
tmp=$(date +%Y-%m-%d -d "-$offset days")
echo $tmp >> $TEMPFILE
done
for offset in `seq 0 $WEEKLY_COUNT`; do
calc=$[$offset * 7]
calc=$[$calc + $TODAY_WEEKDAY]
tmp=$(date +%Y-%m-%d -d "-$calc days")
echo $tmp >> $TEMPFILE
done
for offset in `seq 0 $MONTHLY_COUNT`; do
calc=$[$offset * 28]
calc=$[$calc + $TODAY_WEEKDAY]
tmp=$(date +%Y-%m-%d -d "-$calc days")
echo $tmp >> $TEMPFILE
done
# remove files not on the list
ls -1 ${BACKUPS} | while read filename; do
filedate=$(echo $filename | awk -F'-' '{print $3-$4-$5}')
grep $filedate $TEMPFILE &> /dev/null || rm $filename
done
rm $TEMPFILE