/**1. 完整还原到指定时间点.设时间点为DT
前提:a).DT前有完整备份,或者完整备份+差异备份的组合 b).完整备份后的所有日志备份均可用
**/
USE master
GO
CREATE DATABASE Test
GO
use Test
go
create table tb(id int identity(1,1),val varchar(10),CreateOn datetime default(getdate()))
go
insert into tb values ('a',default),('b',default),('c',default)
go
--Generate full backup file
use master
go
backup database Test to disk=N'D:\MSSQL\Backup\Test_Full.bak' with compression
go
--Insert a new row and back up the log
use Test
go
insert into tb values ('d',default)
go
use master
go
backup log Test to disk=N'D:\MSSQL\Backup\Test_log_1.trn' with compression
go
---Insert another row and back up the log
use Test
go
insert into tb values ('e',default)
go
use master
go
backup log Test to disk=N'D:\MSSQL\Backup\Test_log_2.trn' with compression
go
--something happened.you want to restore the database to the time when the row 'e' was inserted.
use Test
go
insert into tb values ('e',default)
go
print(getdate())
go
insert into tb values ('f',default)
go
--Jan 30 2015 3:33PM
--我需要恢复到插入'e'这行后的时间点.
--接下来,是整个恢复的过程
--1.back up the tail of the log
use master
go
--if the database is still in use,you should set it to be single_user
alter database Test Set single_user with rollback immediate
go
backup log Test to disk=N'D:\MSSQL\Backup\Test_log_tail.trn' with compression,norecovery
go
--2.restore the full backup.
use master
go
--the database was corrupt,so I droped it
restore database Test with recovery
drop database Test
go
restore database Test from disk=N'D:\MSSQL\Backup\Test_Full.bak'
with norecovery
go
--3.restore the log backup
use master
go
restore log Test from disk=N'D:\MSSQL\Backup\Test_log_1.trn'
with norecovery;
restore log Test from disk=N'D:\MSSQL\Backup\Test_log_2.trn'
with norecovery;
--4.restore database to the specified time.
use master
go
restore log Test from disk=N'D:\MSSQL\Backup\Test_log_tail.trn'
with recovery,stopat=N'Jan 30 2015 3:33PM'
go
--5.bring database to mutil_user
use master
go
alter database Test set multi_user
go
1 个回复
郑大鹏 2016-03-20 回答
赞同来自: