Right now I backup the mysql databases using a cronjob everyday. Now that the server is getting full, I only want to keep one backup for every month, one backup from sunday every week and the last 7 backups.
My backup files look like this (in a directory /var/backup
):
full_2015-05-16-061115.sql.gz.enc
full_2015-05-17-061117.sql.gz.enc
...
This serverfault post: https://serverfault.com/a/643335/274380 helped me to create the shell script and how to determine the correct days and put them in an array.
I altered the script from above post a little bit and here is it:
for i in {0..7};
do
index=$(date +"%Y%m%d" -d "-$i day");
value=$(date +"%Y-%m-%d" -d "-$i day");
keep[index]=$value;
done;
for i in {0..4};
do
index=$(date +"%Y%m%d" -d "sunday-$((i+1)) week");
value=$(date +"%Y-%m-%d" -d "sunday-$((i+1)) week");
keep[index]=$value;
done
for i in {0..12};
do
DW=$(($(date +%-W)-$(date -d $(date -d "$(date +%Y-%m-15) -$i month" +%Y-%m-01) +%-W)))
for (( AY=$(date -d "$(date +%Y-%m-15) -$i month" +%Y); AY < $(date +%Y); AY++ )); do
((DW+=$(date -d $AY-12-31 +%W)))
done
index=$(date +"%Y%m%d" -d "sunday-$DW weeks");
value=$(date +"%Y-%m-%d" -d "sunday-$DW weeks");
keep[index]=$value;
done
echo ${keep[@]};
output is:
2014-05-04 2014-06-01 2014-07-06 2014-08-03 2014-09-07 2014-10-05 2014-11-02 2014-12-07 2015-01-04 2015-02-01 2015-03-01 2015-04-05 2015-04-26 2015-05-03 2015-05-10 2015-05-17 2015-05-20 2015-05-21 2015-05-22 2015-05-23 2015-05-24 2015-05-25 2015-05-26 2015-05-27
So my question is, based on ${keep[@]}
how to keep the files which dates are in ${keep[@]}
and delete the rest?
Thank you in advance!