这几天了解SQL server,针对sql server 定时自动备份数据库做了一些了解,
现将收集的资料做份整理
步骤一、写SQL脚本,保存到sqlserverbackup.sql
sql脚本内容(ecology为数据库名)
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 ecology to @device
end
else
begin
backup database ecology to @device with differential
end
步骤二、写bat脚本,t.bat
内容:
@net start "mssqlserver"
@sqlcmd -Usa -Psa -i d:\sqlscript\sqlserverbackup.sql -o d:\sqlscript\sqlserverbackup.out
@echo ……正在备份中……
@pause
注: @sqlcmd 是sqlserver 2005
@isql是sqlserver2000
以上在sqlserver 2008下测试通过
步骤三、设置windows操作系统的 定时任务
打开(附件)里的(任务计划),选择(添加任务计划),点击(下一步),点击(浏览),找到想要执行的程序或文件,点击(打开),输入任务名字,可以任意填写,选择执行任务的时间,点击(下一步),选择任务的起始时间,点击(下一步),输入用户名和密码,没有就不填,点击(下一步),点击(完成)。
不知道是否还有其他方式备份sql server ,如果有的话请给我留言
select * from sys.backup_devices 查询永久磁盘备份信息
exec sp_addumpdevice 'disk','bak1','c:\bak1.bak' --创建永久磁盘备份设备
exec sp_dropdevice 'ecology20110327' --删除备份设备
backup database ecology to bak2 with init,name='d2_full' --差异备份,第一次备份时应做完全备份
create table b1(c1 int not null,c2 char(10) not null)
backup database ecology to bak2 with differential,name='d2_diff1'
insert b1 values(1,'a')
backup database ecology to bak2 with differential,name='d2_diff2'
insert b1 values(2,'b')
backup database ecology to bak2 with differential,name='d2_diff3'
insert b1 values(3,'c')
backup database ecology to bak2 with differential,name='d2_diff4'
restore headeronly from bak2