Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm creating a backup strategy for a sharepoint server I'm setting up.

Have got a backup running daily.

In the long term I'd like to keep:

  • Daily backups for the last week.
  • Weekly backups for the last month.
  • Monthly backups for the last year.
  • Yearly backups.

If I was writing in bash/cygwin I would find it fairly easy to write a script to purge backups that are not required by this strategy. However my DOS scripting skills are very primitive, so I'd struggle with this type of thing.

Was wondering if anyone else had a similar script/util I could use.

Cheers!

share|improve this question

2 Answers 2

There are all kinds of ways of doing this natively in cmd or powershell but as a quick solution look into EraseTemp. Use that with their scripting support and you should be able to do just what you are looking for.

share|improve this answer

I ended up writing a bash script to do this. run-backup-maintenance.sh

Usage example: run-backup-maintenance.sh d7w4m12y10 /cygdrive/d/backup ".*.tar"

NOTE: All daily backups must be written to: /cygdrive/d/backup/daily

If run with the parameter d7w4m12y10, this script ensures that over time:

/cygdrive/d/backup/daily    will contain daily backups for the last 7 days
/cygdrive/d/backup/weekly   will contain weekly backups for the last 4 weeks
/cygdrive/d/backup/monthly  will contain monthly backups for the last 12 months
/cygdrive/d/backup/yearly   will contain yearly backups for the last 10 years

Enjoy!

#!/bin/bash
die () {
    echo >&2 "$@"
    exit 1
}

[ "$#" -eq 3 ] || die "Usage run-backup.sh <options> <backupDir> <artefact>."$'\n'"3 arguments required, $# provided."$'\n'"Options should be given as d#w#m#y#, where # is a number denoting how long to keep the specified backup period."$'\n'"e.g. d7w4m12y10 says that backup will keep: 7 daily backups, 4 weekly backups, 12 monthly backups and 10 yearly backups."

#################################
#PROCESS PARAMETERS
backupDir=$2
artefactPattern=$3

regex="d\([0-9]*\)w\([0-9]*\)m\([0-9]*\)y\([0-9]*\)"
days=`echo $1 | sed "s/$regex/\1/g"`
weeks=`echo $1 | sed "s/$regex/\2/g"`
months=`echo $1 | sed "s/$regex/\3/g"`
years=`echo $1 | sed "s/$regex/\4/g"`

echo "Running backup in folder $backupDir against artefacts matching $artefactPattern keeping: $days days, $weeks weeks, $months months, $years years"

#################################
#YEARLY

if [ $years -ne 0 ]; then
    #Check that the yearly folder has a backup for the last 365 days
    fileListing=`find $backupDir/yearly -mtime -364 | grep "$artefactPattern"`
    if [ "$fileListing" == "" ]; then
        echo 'No files from last 365 days found, taking most recent daily backup...'
        cd $backupDir/daily/
        ls -t1 | grep "$artefactPattern" | head -n1 | xargs -t -I {} cp -r {} ../yearly/
        cd ../..
    else
        echo 'Yearly backup found, no copying required...'
    fi

    #Remove yearly backups older than x years
    find $backupDir/yearly -mtime +$(((years*365)-1)) | grep "$artefactPattern" | xargs rm -rf
fi

#################################
#MONTHLY

if [ $months -ne 0 ]; then
    #Check that the weekly folder has a backup for the last 30 days
    fileListing=`find $backupDir/monthly -mtime -29 | grep "$artefactPattern"`
    if [ "$fileListing" == "" ]; then
        echo 'No files from last 30 days found, taking most recent daily backup...'
        cd $backupDir/daily/
        ls -t1 | grep "$artefactPattern" | head -n1 | xargs -t -I {} cp -r {} ../monthly/
        cd ../..
    else
        echo 'Monthly backup found, no copying required...'
    fi

    #Remove monthly backups older than x months
    find $backupDir/monthly -mtime +$(((months*30)-1)) | grep "$artefactPattern" | xargs rm -rf
fi

#################################
#WEEKLY

if [ $weeks -ne 0 ]; then
    #Check that the weekly folder has a backup for the last 7 days
    fileListing=`find $backupDir/weekly -mtime -6 | grep "$artefactPattern"`
    if [ "$fileListing" == "" ]; then
        echo 'No files from last 7 days found, taking most recent daily backup...'
        cd $backupDir/daily/
        ls -t1 | grep "$artefactPattern" | head -n1 | xargs -t -I {} cp -r {} ../weekly/
        cd ../..
    else
        echo 'Weekly backup found, no copying required...'
    fi

    #Remove weekly backups older than X days
    find $backupDir/weekly -mtime +$(((weeks*7)-1)) | grep "$artefactPattern" | xargs rm -rf
fi

#################################
#DAILY

if [ $days -ne 0 ]; then
    #Remove daily backups older than X days
    find $backupDir/daily -mtime +$((days-1)) | grep "$artefactPattern" | xargs rm -rf
else
    find $backupDir/daily | grep "$artefactPattern" | xargs rm -rf
fi 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.