删除7天以上的备份文件 [英] Delete backup files older than 7 days
问题描述
使用osql命令,创建数据库的SSQL备份. 它被保存到磁盘. 然后重命名以匹配进行备份的日期. 所有这些文件一直都保存在一个文件夹中.
Using osql command, SSQL backup of a database is created. It is saved to Disk. Then renamed to match the date of the day backup was taken. All these files are saved in a single folder all the time.
例如: Batch1.bat执行以下操作 1)创建了backup.bak 2)重命名为备份12-13-2009.bak(这是通过%〜-等的组合来获得date参数)
for example: Batch1.bat does the following 1) Created backup.bak 2) renamed to backup 12-13-2009.bak (this is done by the combination of % ~ - etc to get the date parameter)
现在,它已自动执行,可以由Windows中的Task Scheduler每天进行备份.
This is now automated to take backup everyday by Task scheduler in Windows.
是否还可以修改批处理文件以删除7天以上的备份文件?如果可以,怎么办?
Can the batch file also be modified to delete backup files older than 7 days? if so, how ?
如果无法通过批处理文件进行操作,则除了手动删除文件外,还有其他替代方法可以自动执行删除作业吗?
If it is not possible via batch file, other than manually deleting the files are there any other alternatives to automate the deleting job ?
预先感谢,巴拉吉S
推荐答案
我正在使用相同的技术从数据库进行备份.我创建了一个存储过程,如下所示.
I'm using the same technique to make a backup from database. I've created a stored procedure as follows.
Create Procedure [dbo].[CreateBackup]
As
Begin
Declare @path nvarchar(256),
@filename nvarchar(256),
@currentDateTime datetime
Set @currentDateTime = GetDate()
Set @path = 'C:\DBBackup\'
Set @filename = @path + Cast(DatePart(month, @currentDateTime) As nvarchar) + Cast(DatePart(day, @currentDateTime) As nvarchar) + '.bak'
Backup Database Foo To Disk = @filename
Set @currentDateTime = DateAdd(day, -3, @currentDateTime)
Set @filename = 'del ' + @path + Cast(DatePart(month, @currentDateTime) As nvarchar) + Cast(DatePart(day, @currentDateTime) As nvarchar) + '.bak'
Exec xp_cmdshell @filename
End
要使用xp_cmdshell
,请先启用它.
http://weblogs.sqlteam.com/tarad/archive/2006/09/14/12103.aspx
这篇关于删除7天以上的备份文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!