2

I have a database backup running hourly between 7am and 7pm everyday, each file has a unique name. Is there a way that I can somehow add a 14 day retention time?

DECLARE @FileName AS VARCHAR(120)
DECLARE @Date AS CHAR(19)
SET @Date = CONVERT(CHAR(19), GETDATE(), 120)
SET @FileName = N'\\10.250.145.26\hfnfs\Trans\Hourly\South_RP' + @Date + '.bak'
BACKUP DATABASE [South_RP] TO DISK =@FileName

File name Example South_RP2012-06-26 11:00:00.bak

*Edit this is SQL sever 2008 R2

4

2 に答える 2

1

I would not do this using SQL Server. Instead, I would set up a scheduled task that runs a batch file similar to the following:

cd /d "%~dp0"
forfiles -d-14 -m*.bak -c"cmd /c del @PATH\@FILE"

Adapted from this answer. The way I wrote the batch file above, you'll want to place it in the same directory as the .bak files. Please, test it with echo first instead of del.

To make it work, you'll need to get the forfiles.exe utility from Microsoft's FTP site and place it in your C:\WINDOWS\system32 directory.

If you really want to start this command from SQL Server, you can use the xp_cmdshell procedure to run the batch file that deletes old items.

于 2012-06-26T15:20:12.357 に答える
0

14日以上経過したバックアップを削除するには、cronジョブ/スケジュールされたタスクを設定する必要があります。

于 2012-06-26T15:15:01.593 に答える