2

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!

2 Answers 2

2

This is how I would do this:

find /var/backup/ -type f -maxdetph 1 $(printf "! -name %s " ${keep[*]}) \
  -exec rm {} \;

Note that this will not work if your file names have spaces in them.

1
  • It won't work as expected, you need wildcard path matchings. And you cannot pass escaped characters to find args in a trivial way.
    – yaegashi
    Commented May 28, 2015 at 3:18
1

How about using grep -vE with the following expression:

$ echo "$(IFS=\| && echo "${keep[*]}")"
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 you can do like this:

ls /var/backup/* | grep -vE "$(IFS=\| && echo "${keep[*]}")" | xargs -r rm

Updated: If you prefer using find:

find /var/backup -maxdepth 1 -type f | grep -vE "$(IFS=\| && echo "${keep[*]}")" | xargs -r rm

Or more complicated one:

find /var/backup -maxdepth 1 -type f -regextype posix-egrep ! -regex ".*($(IFS=\| && echo "${keep[*]}")).*" -print0 | xargs -0r rm
3
  • perfect thank you! do you have a solution with "find" instead of "ls" because as I see everybody discourages the use of "ls" together with xargs and encourages the use of "find".
    – Petros Mastrantonas
    Commented May 27, 2015 at 14:17
  • I posted an answer using find. Commented May 27, 2015 at 19:09
  • Updated my answer.
    – yaegashi
    Commented May 28, 2015 at 3:52

You must log in to answer this question.