下面示例为数据库DB05102的完全备份、差异备份、事务日志备份及其还原操作。呵,建作业时用的是企业管理器。因为那样方便多了,当然如果只能用代码编写时,那再麻烦也只能硬着头皮写啦^_^ 1.建备份设备: use master alter database pubs set recovery simple go
go if exists(select name from sysdevices where name='DB05102FBK') exec sp_dropdevice 'DB05102FBK' exec sp_addumpdevice 'disk','DB05102FBK','F:\0509\Backup\DB05102FBK' --完全备份设备 go if exists(select name from sysdevices where name='DB05102DBK') exec sp_dropdevice 'DB05102DBK' exec sp_addumpdevice 'disk','DB05102DBK','F:\0509\Backup\DB05102DBK' --差异备份设备 go if exists(select name from sysdevices where name='DB05102LBK') exec sp_dropdevice 'DB05102LBK' exec sp_addumpdevice 'disk','DB05102LBK','F:\0509\Backup\DB05102LBK' --事务日志备份设备 go exec sp_addumpdevice 'disk','DB05102LBK2','F:\0509\Backup\DB05102LBK2' --当前事务日志备份设备 go
2.在企业管理器中新建如下三种作业,安排调度为: 完全备份每1天执行1次;步骤中的T-SQL 语句为:backup database DB05102 to DB05102FBK 差异备份每天每隔4小时执行1次;其中的T-SQL语句为:
backup database DB05102 to DB05102DBK with differential 事务日志备份每天每隔1小时执行1次:其中的t-sql语句为:
backup log DB05102 to DB05102LBK. 注意数据库在创建时,默认恢复模式为simple.而要备份事务日志的话,必须将其改为full 或 bulk_logged.即alter database DB05102 set recovery Bulk_logged.当然这个语句只执行一次就行了.没必要每次执行事务日志备份都带进去
3.还原数据库:
备份当前活动事务日志: backup log DB05102 to DB05102LBK2 with no_truncate go
还原上一次的完全备份.file为要还原的备份集,一次备份产生一个备份集,norecovery说明恢复未结束.直到最后一个日志恢复方指定为recovery或不指定 (默认为recovery) . restore database DB05102 from DB05102FBK with file=4,norecovery go
----.还原上面完全备份后的离现在最近一次的差异备份 restore database DB05102 from DB05102dbk with file=29,norecovery go
----.还原上面的差异备份后的所有事务日志备份.必须按顺序执行. restore log DB05102 from DB05102lbk with file=115,norecovery go restore log DB05102 from DB05102lbk with file=116,norecovery go restore log DB05102 from DB05102lbk with file=117,norecovery go
还原当前备份的事务日志备份. restore log DB05102 from DB05102lbk2 with file=8 go