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
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:
Adapted from this answer. The way I wrote the batch file above, you’ll want to place it in the same directory as the
.bakfiles. Please, test it withechofirst instead ofdel.To make it work, you’ll need to get the
forfiles.exeutility from Microsoft’s FTP site and place it in yourC:\WINDOWS\system32directory.If you really want to start this command from SQL Server, you can use the
xp_cmdshellprocedure to run the batch file that deletes old items.