用于删除旧备份的脚本或实用程序

发布于 2024-09-13 23:22:17 字数 388 浏览 3 评论 0原文

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

每天都有备份运行。

从长远来看,我想保留:

* 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 脚本编写技能非常原始,所以我想知道是否其他人有我可以使用的类似脚本/实用程序。

干杯!

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 I would rather not have to install cygwin, I'd rather do it with native DOS scripting, or some other specialized tool.

My DOS scripting skills are very primitive, so I was wondering if anyone else had a similar script/util I could use.

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

年少掌心 2024-09-20 23:22:17

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

PowerShell 一开始可能会很复杂,但它基本上是批处理脚本破解 可以访问 .NET 运行时。


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

You could use PowerShell to achieve a rather compelx backup procedure. Or you could write the script in Python or another executable scripting language.

PowerShell can be convoluted at first, but it's basically Batch Scripts on crack with access to the .NET Runtime.


Note: PowerShell I believe is only availiable on 2003, Vista, Windows 7 (I know it comes with them, at least, it may work with XP/2000, but im not sure). Python can be installed on prettymuch anything.

吹泡泡o 2024-09-20 23:22:17

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

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文