Monday, May 24, 2010

Create backup stored procedure

This stored procedure helps you to backup the database in 7 day cycle and each cycle has one file.


exec sp_addumpdevice 'disk', 'db_mon_bak',
'c:\mssql\backup\db_mon_bak.bak'
go
exec sp_addumpdevice 'disk', 'db_tue_bak',
'c:\mssql\backup\db_tue_bak.bak'
go
exec sp_addumpdevice 'disk', 'db_wed_bak',
'c:\mssql\backup\db_wed_bak.bak'
go
exec sp_addumpdevice 'disk', 'db_thu_bak',
'c:\mssql\backup\db_thu_bak.bak'
go
exec sp_addumpdevice 'disk', 'db_fri_bak',
'c:\mssql\backup\db_fri_bak.bak'
go
exec sp_addumpdevice 'disk', 'db_sat_bak',
'c:\mssql\backup\db_sat_bak.bak'
go
exec sp_addumpdevice 'disk', 'db_sun_bak',
'c:\mssql\backup\db_sun_bak.bak'
go

if exists( select * from sysobjects
where name = 'mgt_backup_db')
drop proc mgt_backup_db
go

CREATE proc mgt_backup_db (
@db_name sysname
)
as
begin

declare @d int

select @d = datepart( weekday, getdate())

if (@d = 2)
backup database @db_name
to db_mon_bak
with init

else if (@d = 3)
backup database @db_name
to db_tue_bak
with init

else if (@d = 4)
backup database @db_name
to db_wed_bak
with init

else if (@d = 5)
backup database @db_name
to db_thu_bak
with init

else if (@d = 6)
backup database @db_name
to db_fri_bak
with init

else if (@d = 7)
backup database @db_name
to db_sat_bak
with init



end


GO


No comments:

Post a Comment