我正在为正在设置的 SharePoint 服务器创建备份策略。
每天都会进行备份。
从长远来看,我想保留:
- 过去一周的每日备份。
- 上个月的每周备份。
- 去年的每月备份。
- 每年备份。
如果我使用 bash/cygwin 编写脚本,我会发现编写脚本来清除此策略不需要的备份相当容易。但是我的 DOS 脚本编写技能非常初级,因此我很难处理这种事情。
想知道是否有其他人有我可以使用的类似脚本/实用程序。
干杯!
答案1
我最终编写了一个 bash 脚本来执行此操作。run-backup-maintenance.sh
使用示例:run-backup-maintenance.sh d7w4m12y10 /cygdrive/d/backup ".*.tar"
注意:所有每日备份必须写入:/cygdrive/d/backup/daily
如果使用参数 d7w4m12y10 运行,此脚本可确保随着时间的推移:
/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
享受!
#!/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
答案2
在 cmd 或 powershell 中,有各种各样的方法可以本地执行此操作,但作为一种快速解决方案,请查看擦除临时文件. 将其与他们的脚本支持结合使用,您应该能够完成您想要做的事情。