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 
 

你可能感兴趣的:(sql,数据库,差异备份,Server备份,自动化任务)