Acabei escrevendo um script para fazer isso. run-backup-maintenance.sh
Exemplo de uso: run-backup-maintenance.sh d7w4m12y10 / cygdrive / d / backup ". *. tar"
NOTA: Todos os backups diários devem ser gravados em: / cygdrive / d / backup / daily
Se for executado com o parâmetro d7w4m12y10, esse script garante que, com o tempo:
/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
Aproveite!
#!/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//g"'
weeks='echo $1 | sed "s/$regex//g"'
months='echo $1 | sed "s/$regex//g"'
years='echo $1 | sed "s/$regex//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