加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

SQL Server基于时间点(STOPAT)的数据备份及还原处理过程

(2015-01-09 15:40:56)
标签:

基于stopat数据还原

基于时间点的数据还原

数据还原到某个时间点

stopat数据还原

sqlserver时间点还原

分类: 数据库学习

一、数据备份
--添加备份设备
sp_addumpdevice 'disk','mytest_backup','d:\backup\mytest_backup.bak'

go
--删除备份设备(也可以从 服务器对象-备份设备 下删除)
--sp_dropdevice 'mytest_backup'

--创建测试表
drop table t_test
go
create table t_test(
id int identity(1,1) not null,
name varchar(30)
)

--创建数据库完整备份
backup database mytest
to mytest_backup

go
--添加测试数据1
insert into t_test(name)
select '添加时间1:'+convert(varchar(20),GETDATE(),120)

--备份事务日志1
backup log mytest
to mytest_backup

--添加测试数据2
insert into t_test(name)
select '添加时间2:'+convert(varchar(20),GETDATE(),120)

--备份事务日志2
backup log mytest
to mytest_backup

--添加测试数据3
insert into t_test(name)
select '添加时间3:'+convert(varchar(20),GETDATE(),120)

--备份事务日志3
backup log mytest
to mytest_backup

select * from t_test
id          name
----------- ------------------------------
          添加时间1:2015-01-06 09:46:58
          添加时间2:2015-01-06 09:47:02
          添加时间3:2015-01-06 09:47:06

 

--stopat时间点的获取
1、可以在ssms配置管理器中,单击处理的数据库,右键菜单 任务—还原—数据库(选择用户还原的备份集(E))表格记录项中的 开始时间 和完成时间 ,一般 stopat 时间点取完成时间。
2、可以在ssms配置管理器中,展开服务器对象—备份设备 ,找到你添加的备份设备 双击 打开如下图所示 中的 日期也可以作为stopat的时间点。

SQL <wbr>Server基于时间点(STOPAT)的数据备份及还原处理过程


二、还原到某一时间点的部分还原
--完整还原数据库
use master
go
restore database mytest
from mytest_backup
with file=1,--备份集为1
  replace,
  norecovery

--根据日志信息以及备份集恢复日志恢复到某一时间点
restore log mytest
from mytest_backup
with file=2,--备份集为2
  recovery,--使用recovery,表示使数据库恢复到一致状态
  stopat='2015-01-06 09:46:59'

--根据日志信息以及备份集恢复日志恢复到某一时间点
restore log mytest
from mytest_backup
with file=3,--备份集为3
  recovery,
  stopat='2015-01-06 09:47:04'

--执行上述操作以后,mytest数据库处于restoring状态,执行此语句使数据库可用
restore database mytest
with recovery

 

--还原后查看数据

select * from mytest.dbo.t_test
id          name
----------- ------------------------------
          添加时间1:2015-01-06 09:46:58
          添加时间2:2015-01-06 09:47:02

 

三、还原到某一时间点的全部还原
--完整还原数据库
use master
go
restore database mytest
from mytest_backup
with file=1,--备份集为1
  replace,
  norecovery

--根据日志信息以及备份集恢复日志恢复到某一时间点
restore log mytest
from mytest_backup
with file=2,--备份集为2
  recovery,
  stopat='2015-01-06 09:46:59'

--根据日志信息以及备份集恢复日志恢复到某一时间点
restore log mytest
from mytest_backup
with file=3,--备份集为3
  recovery,
  stopat='2015-01-06 09:47:04'

--根据日志信息以及备份集恢复日志恢复到某一时间点
restore log mytest
from mytest_backup
with file=4,--备份集为4
  recovery,
  stopat='2015-01-06 09:47:08'


--执行上述操作以后,mytest数据库处于restoring状态,使用此语句时数据库可用
restore database mytest
with recovery

 

--参看还原结果
select * from mytest.dbo.t_test
id          name
----------- ------------------------------
          添加时间1:2015-01-06 09:46:58
          添加时间2:2015-01-06 09:47:02
          添加时间3:2015-01-06 09:47:06

 

 

 

 

本文参考:http://www.cnblogs.com/xwdreamer/archive/2012/07/09/2582999.html

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有