SQL Server自動備份腳本

         這幾天在給企業培訓時,幫助他們寫了一個備份的腳本,留給你們分享吧。此腳本的目的是,每週日作徹底備份,每週一到週六作差別備份,每週的備份寫入到一個備份設備當中,備份設備起名規則爲「備份設備名+週日日期」,下週備份再從新備份到一個新的備份設備中。此腳本編寫完之後,能夠作成做業,讓它天天自動完成就能夠了。
 
declare @device varchar(30); declare @filename varchar(50); declare @datetime varchar(50); declare @weekname varchar(50); declare @weeknametmp varchar(4); declare @weekday int; set @weekname=datename(weekday,getdate()); set @datetime =  convert(varchar(20),getdate(),112); set @device= 'NWdevice' + @datetime; set @filename='c:\NWdevice'+@datetime+'.bak'; set @weeknametmp=substring(@weekname,3,1); if (@weeknametmp='一')               set @weekday=1; else if (@weeknametmp='二')               set @weekday=2; else if (@weeknametmp='三')                set @weekday=3; else if (@weeknametmp='四')                 set @weekday=4; else if (@weeknametmp='五')                 set @weekday=5; else if (@weeknametmp='六')                 set @weekday=6; else if (@weeknametmp='日')                 set @weekday=0; set @datetime =  convert(varchar(20),getdate()-@weekday,112); set @device= 'NWdevice' + @datetime; set @filename='c:\NWdevice'+@datetime+'.bak' if (@weekname='星期日')                  begin                  execute sp_addumpdevice 'disk',@device,@filename;                   backup database northwind to @device                   end else                   begin                   backup database northwind to @device with differential                   end 
相關文章
相關標籤/搜索