Date created: Monday, December 9, 2013 11:57:00 AM. Last modified: Thursday, August 2, 2018 9:03:09 AM

Backups: Grandfather-father-son

Simple BASH notes on keeping staggered grandfather - father - son style backups

#!/ bin/bash
backupDir=/path/to/backup dir/
cd "$backupDir"

# Files older than one month (we shall keep daily backups for one month) but younger than 6 month
# are searched for and then if the backup wasn't created on a Monday it is deleted, meaning that
# after 1 month but less than 6 months we are keeping weekly backups
#
# Here we are guestimating 185 days to be 6 months roughly

for backup in `find . -type f -name "*.rar" -mtime +30 -mtime -185`
do
    if [ `date -r $backup +%a` != "Mon" ]
    then
        rm $backup
    fi
done

# After 6 months just keep monthly backups (delete backups not made in days 1 to 7 of the month, this will
# be the first backup from that month from the weekly backups above)

for backup in `find . -type f -name "*.rar" -mtime +185`
do
    if [[ `date -r $backup +%-d` -gt 7 ]]
    then
        rm $backup
    fi
done

# We could also do something like this for certain periods within a month such as the 2nd week of a month

# if [[ `date -r $backup +%-d` -ge 8  && `date -r $backup +%-d` -le 14 ]];

Previous page: Backup Github
Next page: Curl Backup with Login Example