Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 用于删除旧备份的脚本或util_Windows_Batch File_Backup_Dos - Fatal编程技术网

Windows 用于删除旧备份的脚本或util

Windows 用于删除旧备份的脚本或util,windows,batch-file,backup,dos,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,我宁愿使用本机D

我正在为正在设置的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脚本技能非常原始,所以我想知道是否有其他人有类似的脚本/工具我可以使用

干杯

您可以使用来实现一个相当复杂的备份过程。或者,您可以使用Python或其他可执行脚本语言编写脚本

一开始可能很复杂,但基本上是批处理脚本on crack,可以访问.NET运行时



注意:我认为PowerShell只在2003、Vista和Windows7上可用(我知道它是随它们一起提供的,至少,它可以与XP/2000一起工作,但我不确定)。Python可以安装在prettymuch上。为此,我编写了一个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 
#/bin/bash
死(){
回声>&2“$@”
出口1
}
[“$#”-等式3]| | die”用法run-backup.sh.“$'\n”需要3个参数,提供“$'\n'”选项应以d#w#m#y#的形式给出,其中#是一个数字,表示指定的备份周期的保持时间。$'\n'”例如d7w4m12y10表示备份将保持:7次每日备份、4次每周备份、12次每月备份和10次年度备份。”
#################################
#工艺参数
backupDir=$2
手工艺品图案=3美元
regex=“d\([0-9]*\)w\([0-9]*\)m\([0-9]*\)y\([0-9]*\)”
天数=`echo$1 | sed“s/$regex/\1/g”`
weeks=`echo$1 | sed“s/$regex/\2/g”`
月数=`echo$1 | sed“s/$regex/\3/g”`
年份=`echo$1 | sed“s/$regex/\4/g”`
echo“针对匹配$artefactPattern keeping的人工制品在文件夹$backupDir中运行备份:$days-days、$weeks-weeks、$months-months、$years-years”
#################################
#每年
如果[$年-ne 0];然后
#检查年度文件夹是否有最近365天的备份
fileListing=`find$backupDir/yearly-mtime-364 | grep“$artifactpattern”`
如果[“$fileListing”==”;然后
echo“未找到最近365天的文件,正在进行最新的每日备份…”
cd$backupDir/每日/
ls-t1 | grep“$artifactpattern”| head-n1 | xargs-t-I{}cp-r{}./每年/
cd../。。
其他的
echo“找到年度备份,无需复制…”
fi
#删除早于x年的年度备份
查找$backupDir/yearly-mtime+$((years*365)-1))| grep“$artefactPattern”| xargs rm-rf
fi
#################################
#月刊
若[$月-东北0];然后
#检查每周文件夹是否有过去30天的备份
fileListing=`find$backupDir/monthly-mtime-29 | grep“$artifactpattern”`
如果[“$fileListing”==”;然后
echo“未找到过去30天的文件,正在进行最新的每日备份…”
cd$backupDir/每日/
ls-t1 | grep“$artifactpattern”| head-n1 | xargs-t-I{}cp-r{}./每月/
cd../。。
其他的
echo“发现每月备份,无需复制…”
fi
#删除早于x个月的每月备份
查找$backupDir/每月-mtime+$((月*30)-1))| grep“$artefactPattern”| xargs rm-rf
fi
#################################
#周报
若[$weeks-东北0];然后
#检查每周文件夹是否有过去7天的备份
fileListing=`find$backupDir/weekly-mtime-6 | grep“$artifactpattern”`
如果[“$fileListing”==”;然后
echo“未找到过去7天的文件,正在进行最新的每日备份…”
cd$backupDir/每日/
ls-t1 | grep“$artifactpattern”| head-n1 | xargs-t-I{}cp-r{}./每周/
cd../。。
其他的
echo“发现每周备份,无需复制…”
fi
#删除早于X天的每周备份
查找$backupDir/周-mtime+$((周*7)-1))| grep“$artefactPattern”| xargs rm-rf
fi
#################################
#每日
若[$days-东北0];然后
#删除早于X天的每日备份
查找$backupDir/daily-mtime+$((天-1))| grep“$artefactPattern”| xargs rm-rf
其他的
查找$backupDir/每日| grep“$artefactPattern”| xargs rm-rf
fi