在前面forfiles命令批量删除N天前文件 这篇文章里面讲述了如何在SQL Server 2000中使用forfile删除N天前备份文件,但是现在又有新需求:
需求描述:通过数据库维护计划将备份生成在本地磁盘M(M盘位于单独的SAN存储,该SAN存储专门用于保留备份),完整备份保留2 天,事务日志备份保留2天,但是M:\DB_BACKUP\FULL_BACKUP目录下只能保留最新的一份完整备份,旧的备份必须移动到M: \DB_BACKUP\OLD_BACKUP,这样我就可以通过Symantec Backup Exec只将FULL_BAKCUP和LOG_BACKUP目录收上磁带。否则通过Symantec Backup Exec将备份收上磁带时,将出现冗余。一来需要消耗更多的磁带,最麻烦的是因为SAN存储和磁带机位于不同地域的机房,虽然通过1G的光纤连接,但是由 于我们磁带机只有两个Drive,只能并行跑两个作业,所以必须减少冗余,防止作业被滞留到白天,影响网络带宽,所以必须修改手头几台SQL Server 2000的备份策略,修改delete_old_backup.bat命令
M:\DB_BACKUP\FULL_BACKUP
M:\DB_BACKUP\LOG_BACKUP
M:\DB_BACKUP\LOG_BACKUP
delete_old_backup.bat 脚本如下所示:
echo --**************************************************************-- >>delete_old_backup.log
echo --Delete the backup log start at %Date% - %time% -->>delete_old_backup.log
rem Keep the backup log days definition.
set LogKeepDays=2
rem delete the old backup log files LogKeepDays ago.
set LogPath=M:\DB_BACKUP\
forfiles -p%LogPath% -m*.txt -d-%LogKeepDays% -c"cmd /c del /q @FILE" >> delete_old_backup.log
echo Delete the backup log End at %Date% - %time% >>delete_old_backup.log
echo --Delete the folder old_bakcup full backup start at %Date% - %time% -- >>delete_old_backup.log
set OldBackupPath=M:\DB_BACKUP\OLD_BACKUP
set OldBkKeepDays=1
forfiles -p%OldBackupPath% -m*.bak -d-%OldBkKeepDays% -c"cmd /c del /q @FILE" >> delete_old_backup.log
echo --Delete the folder old_bakcup full backup end at %Date% - %time% -->>delete_old_backup.log
echo Move the full backup start at %Date% - %time% >>delete_old_backup.log
set FullBkKeepDays=1
set FullBackupPath=M:\DB_BACKUP\FULL_BACKUP
forfiles -p%FullBackupPath% -m*.bak -d-%FullBkKeepDays% -c"cmd /c move @FILE M:\DB_BACKUP\OLD_BACKUP" >> delete_old_backup.log
echo Move the full backup End at %Date% - %time% >>delete_old_backup.log
echo Delete the log backup start at %Date% - %time% >>delete_old_backup.log
set LogBackupPath=M:\DB_BACKUP\LOG_BACKUP
set LogBkKeepDays=2
forfiles -p%LogBackupPath% -m*.TRN -d-%LogBkKeepDays% -c"cmd /c del /q @FILE" >> delete_old_backup.log
echo Delete the log backup Stop at %Date% - %time% >>delete_old_backup.log
echo --****************************************-- >>delete_old_backup.log