A quick script to remove files matching pattern from rsnapshot backups

Needed to free up some disk space and identified some files which had been backed up, but which I'll never need, so cooked up this quick little script to loop through all the backup directories (daily.0, daily.1, etc) and purge files matching a regex, while logging each one for the record.

base=/var/backups/rsnapshot
dir=path/to/dir/
file="*SomeFile*"
log=rsnap.cleanup.log
 
for d in $(ls $base); do
  echo $(date "+%F %T") processing directory $base/$d | tee -a $log
  find $base/$d/$dir | egrep "*$file*" | 
  while read f; do
    echo $(date "+%F %T") removing file: >> $log
    ls -li "$f" >> $log
    rm "$f"
  done
done

Example output:

2012-11-20 10:58:48 processing directory /var/backups/rsnapshot/daily.0
2012-11-20 10:58:48 processing directory /var/backups/rsnapshot/daily.1
2012-11-20 10:58:48 processing directory /var/backups/rsnapshot/daily.2
2012-11-20 10:58:51 processing directory /var/backups/rsnapshot/daily.3
2012-11-20 10:58:53 processing directory /var/backups/rsnapshot/daily.4
2012-11-20 10:58:56 processing directory /var/backups/rsnapshot/daily.5
2012-11-20 10:58:58 processing directory /var/backups/rsnapshot/daily.6
2012-11-20 10:59:01 processing directory /var/backups/rsnapshot/monthly.0
2012-11-20 10:59:04 processing directory /var/backups/rsnapshot/monthly.1
2012-11-20 10:59:07 processing directory /var/backups/rsnapshot/monthly.10
2012-11-20 10:59:09 processing directory /var/backups/rsnapshot/monthly.2
2012-11-20 10:59:11 processing directory /var/backups/rsnapshot/monthly.3
2012-11-20 10:59:13 processing directory /var/backups/rsnapshot/monthly.4
2012-11-20 10:59:16 processing directory /var/backups/rsnapshot/monthly.5
2012-11-20 10:59:18 processing directory /var/backups/rsnapshot/monthly.6
2012-11-20 10:59:20 processing directory /var/backups/rsnapshot/monthly.7
2012-11-20 10:59:23 processing directory /var/backups/rsnapshot/monthly.8
2012-11-20 10:59:25 processing directory /var/backups/rsnapshot/monthly.9
2012-11-20 10:59:27 processing directory /var/backups/rsnapshot/weekly.0
2012-11-20 10:59:31 processing directory /var/backups/rsnapshot/weekly.1
2012-11-20 10:59:33 processing directory /var/backups/rsnapshot/weekly.3
2012-11-20 10:59:36 processing directory /var/backups/rsnapshot/yearly.0
2012-11-20 10:59:38 processing directory /var/backups/rsnapshot/yearly.1

Log will contain stuff like:

2012-11-20 10:58:23 processing directory /var/backups/rsnapshot/daily.0
2012-11-20 10:58:23 removing file:
2290475698 -rw-rw-r-- 1 1003 1007 136353642 2012-06-28 20:13 /var/backups/rsnapshot/daily.0/path/to/dir/SomeFile.1
2012-11-20 10:58:23 removing file:
2290475644 -rw-rw-r-- 1 1003 1007 318410539 2012-11-19 20:13 /var/backups/rsnapshot/daily.0//path/to/dir/SomeFile.2
...

2 Comments

  • 1. Tamadite replies at 5th February 2013, 3:07 pm :

    Thanks for sharing this script!
    I have used it to delete backup entries on a QNAP NAS. I tailored my own backup solution based on rsnapshot.

    I had to modify the script to get it to work under QNAP. egrep was not 100% compatible.

    MODIFY
    egrep “*$file*”
    TO
    egrep “$file”

    MODIFY
    file=”*SomeFile*”
    TO
    file=”SomeFile”

    In order to be able to delete folders, which is what I was looking for, I modified the removing command

    MODIFY
    rm “$f”
    TO
    rm -r “$f”

    A side effect of doing simply so is that the screen and the log get filled with all content of the directory when actually the main entry should be enough.

  • 2. ian replies at 13th November 2014, 8:34 pm :

    Whoops! I backed up a huge directory with rsnapshot by mistake, and didn’t notice for a few weeks.

    I came here via google, but ended up doing instead since I didn’t need a log:

    find . -type d | grep /some/path/ | xargs rm -rf

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).