views:

69

answers:

1

I have everyday schedule to backup my database in sqlserver 2005. Now it's looks like this:

BACKUP DATABASE [db1] TO  DISK = N'D:\SqlServer\Backup\db1.bak' WITH  NOINIT ,  NOUNLOAD ,  NAME = N'db1backup',  NOSKIP ,  STATS = 10,  NOFORMAT

But in this case, it's will grow infinitely and I want to store only last 7 backups in file. How can I do that(maybe erase old backups somehow)?

+1  A: 

Assuming you want to re-initialize the file every sunday (you can change this to your favorite day of the week) you can use the following:

declare @init_option nvarchar(50)
declare @cmd nvarchar(1000)
set @init_option = 'NOINIT'
IF (datename(dw, getdate())) = 'Sunday' set @init_option = 'INIT'
set @cmd ='BACKUP DATABASE [db1] TO  DISK = N''D:\SqlServer\Backup\db1.bak'' WITH  ' + @init_option + ' ,  NOUNLOAD ,  NAME = N''db1backup'',  NOSKIP ,  STATS = 10,  NOFORMAT'
EXECUTE(@cmd)
Nick Kavadias
It's not exactly what i want but very close to it. Thanks
x2
@Nick: Not related to this answer... Please don't add tags like "belongs-on-serverfault". Tags are for categorizing information for indexing and search. Until you have enough rep to vote to close, feel free to flag for moderator attention if you find a post that needs migrated. Thanks.
Bill the Lizard