scripts/cleanup-backups.sh

48 lines
917 B
Bash

#!/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