脚本或util用于删除旧备份

时间:2010-08-13 23:44:20

标签: windows batch-file backup dos

我正在为我正在设置的sharepoint服务器创建备份策略。

每天都有备份。

从长远来看,我想保留:

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

如果我用bash / cygwin编写,我会发现写一个脚本来清除这个策略不需要的备份相当容易。但是我宁愿不必安装cygwin,我宁愿使用本机DOS脚本或其他专业工具。

我的DOS脚本技巧非常原始,所以我想知道是否有其他人有类似的脚本/工具我可以使用。

干杯!

2 个答案:

答案 0 :(得分:0)

您可以使用PowerShell来实现相当强制的备份过程。或者您可以用 Python 或其他可执行脚本语言编写脚本。

PowerShell最初可以进行复杂处理,但它基本上是批处理脚本 on crack ,可以访问.NET运行时。


注意:PowerShell我相信只能在2003年,Vista,Windows 7上使用(我知道它附带它们,至少,可能可以使用XP / 2000,但我不是当然)。 Python可以安装在任何东西上。

答案 1 :(得分:0)

我最终编写了一个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