数据库中有关差异备份的恢复问题

2010-08-28 10:50:01来源:西部e网作者:

A : 情况是这样的

create  database  test  
create  table  t(a  int)  
insert  into  test..t  select  1

然后进行一次完整备份

backup  database  test  to  disk='c:\test.bak'  
insert  into  test..t  select  2

再进行一次完整备份

backup  database  test  to  disk='c:\test.bak'  
insert  into  test..t  select  3

此时用

restore  database  test  from  disk='c:\test.bak'  with  file=1

结果为 1, 此为正确

restore  database  test  from  disk='c:\test.bak'  with  file=2

结果为 1,

2 此也为正确

当表t中为1,2,3的时候,在插入一条纪录结果为1,2,3,4然后进行一次差异备份

backup  database  test  to  disk='c:\test.bak'  with  differential

然后往执行delete from t 删除所有纪录

我现在想恢复最后的那次差异备份(结果为1,2,3,4),用语句改如何实现呢?

下面的是详细的过程,在我的电脑上测试成功:

--清除环境,防止现有的数据影响测试结果

exec  master..xp_cmdshell  'del  c:\text.bak'  
if  exists(select  *  from  master..sysdatabases  where  name='test')  
drop  database  test  
go

--创建数据库

create  database  test 
go

--打开创建的数据

use  test  
go

--创建测试表

create  table  t(a  int)

--切换回master数据库

use  master  
go

--插入数据1

insert  into  test..t  select  1  
go

--然后进行一次完整备份

backup  database  test  to  disk='c:\test.bak'  
go

--插入数据2

insert  into  test..t  select  2  
go

--再进行一次完整备份

backup  database  test  to  disk='c:\test.bak'  
go

--插入3,4

insert  into  test..t  select  3  
insert  into  test..t  select  4  
go

--差异备份:

backup  database  test  to  disk='c:\test.bak'  with  differential

--删除数据库

drop  database  test

--还原数据库和差异数据库备份

--还原完整备份

restore  database  test  from  disk='c:\test.bak'  with  file=2,norecovery

--还原差异备份的内容

restore  database  test  from  disk='c:\test.bak'  with  file=3,recovery

--显示恢复后的数据

select  *  from  test..t

都已经说的好明白了,怎么可能会不行呢?

前段时间我就做过类似程序的!

必须说明的是:在恢复差异备份时,必须恢复最后一次的完整备份!!(切记)

而且下面的两个语句必须同时执行,即放在一个事务中。

restore  database  test  from  disk='c:\test.bak'  with  file=离你要恢复的差异备份最近一次的完整备份号,
norecovery    
restore  database  test  from  disk='c:\test.bak'  with  file=你要还原的差异备份号,recovery

具体的备份号可以从下面得到:(你可以认真研究一下backupfile,backupset,backmediaset,backupmediafamily几个表,可以发现规律)

select  backup_start_date  as  备份时间,position  as  备份号,  
case  type    when  'D'  then  '完整备份'  when  'I'  then  '差异备份'  end  as  备份类型    
from  msdb..backupset  where  database_name='test' 
and  media_set_id  in    
(select  distinct  media_set_id  from  msdb..backupmediafamily  
where  physical_device_name='c:\test.bak')  
order  by  position

如果还不行的话,可以给我留言~

---执行下面的序列:

create  database  test  
go  
use  test  
go  
create  table  test..t(a  int)  
insert  test..t  select  1  
backup  database  test  to  disk='c:\test.bak' 
insert  test..t  select  2  
backup  database  test  to  disk='c:\test.bak'  
insert  test..t  select  3  
insert  test..t  select  4  
backup  database  test  to  disk='c:\test.bak'  with  differential  
delete  test..t  
go

--下面开始恢复:

restore  database  test  from  disk='c:\test.bak'  with  file=2,norecovery  --对应你最后一次的完整备份
restore  database  test  from  disk='c:\test.bak'  with  file=3  --对应你要还原的差异备份   
go   
select  *  from  test