你并不需要乱用RECOVERY
和NORECOVERY
在这里,所有你需要的是STANDBY
选项。这是有关如何使用它的快速演示。
创建一个数据库,将其设置为简单恢复,然后创建一个表。
插入数据,进行一些比较。
好玩吧
USE master;
/*Create a dummy database*/
CREATE DATABASE DiffRestoreTest
/*We simple now*/
ALTER DATABASE DiffRestoreTest SET RECOVERY SIMPLE
/*Context is everything*/
USE DiffRestoreTest
/*If nothing changes, do we even need a diff backup?*/
CREATE TABLE dbo.t1 (Id INT)
/*Take a full backup, dummy*/
BACKUP DATABASE DiffRestoreTest
TO DISK = 'F:\Backup\DRT_FULL.bak'
WITH INIT, FORMAT, COMPRESSION
/*Make a change*/
INSERT dbo.t1 (Id )
VALUES ( 1 )
/*Take a differential backup*/
BACKUP DATABASE DiffRestoreTest
TO DISK = 'F:\Backup\DRT_DIFF_1.bak'
WITH INIT, FORMAT, COMPRESSION, DIFFERENTIAL
/*Make another change*/
INSERT dbo.t1 (Id )
VALUES ( 2 )
/*Take another diff backup*/
BACKUP DATABASE DiffRestoreTest
TO DISK = 'F:\Backup\DRT_DIFF_2.bak'
WITH INIT, FORMAT, COMPRESSION, DIFFERENTIAL
/*Make another change*/
INSERT dbo.t1 (Id )
VALUES ( 3 )
/*Take another diff backup*/
BACKUP DATABASE DiffRestoreTest
TO DISK = 'F:\Backup\DRT_DIFF_3.bak'
WITH INIT, FORMAT, COMPRESSION, DIFFERENTIAL
是的,我撒谎了。那是无聊的部分。
您可以在中还原完整备份STANDBY
:
/*Exit stage left*/
USE master
/*Restore the full backup*/
RESTORE DATABASE DiffRestoreTest
FROM DISK = 'F:\Backup\DRT_FULL.bak'
WITH REPLACE, STANDBY = 'F:\Backup\DRT_STANDBY.tuf'
您可以按以下顺序恢复Diffs STANDBY
:
/*Square one*/
RESTORE DATABASE DiffRestoreTest
FROM DISK = 'F:\Backup\DRT_DIFF_1.bak'
WITH STANDBY = 'F:\Backup\DRT_STANDBY.tuf'
/*Square 2*/
RESTORE DATABASE DiffRestoreTest
FROM DISK = 'F:\Backup\DRT_DIFF_2.bak'
WITH STANDBY = 'F:\Backup\DRT_STANDBY.tuf'
/*Square 3*/
RESTORE DATABASE DiffRestoreTest
FROM DISK = 'F:\Backup\DRT_DIFF_3.bak'
WITH STANDBY = 'F:\Backup\DRT_STANDBY.tuf'
与那些讨厌的日志文件不同,在还原Diff时,您也可以跳过:
/*Restore the full backup*/
RESTORE DATABASE DiffRestoreTest
FROM DISK = 'F:\Backup\DRT_FULL.bak'
WITH REPLACE, STANDBY = 'F:\Backup\DRT_STANDBY.tuf'
/*What happens if I try to jump the restores?*/
RESTORE DATABASE DiffRestoreTest
FROM DISK = 'F:\Backup\DRT_DIFF_3.bak'
WITH STANDBY = 'F:\Backup\DRT_STANDBY.tuf'
如果要测试可读性,只需在还原命令之间运行它。您应该看到ID每增加一个。如果不这样做,那么您做错了什么。
请记住,还原文件时,它将把所有最终用户踢出数据库,并且不会等待他们的查询完成。
这也使数据库处于只读状态,此处无法进行任何更改。
SELECT *
FROM DiffRestoreTest.dbo.t1 AS t
最后,自己清理。
/*Bring'er online, lad*/
RESTORE DATABASE DiffRestoreTest WITH RECOVERY
DROP DATABASE DiffRestoreTest
希望这可以帮助!!