4 sql-server restore transaction-log
我们的一个客户端 SQL Server 崩溃了,需要将其恢复到最近的可能时间。我们有从周一开始的完整备份以及每小时运行一次的事务日志,直到今天凌晨 02:01,但是我们缺少数据库中的一个事务日志,因此我们只能将该数据库恢复到周三。
是否可以跳过事务日志并继续使用剩余日志将数据库恢复到最新日期?
编辑:在我们失踪时没有对数据库进行任何更改,因此事务日志中将没有任何内容
是否可以跳过事务日志并继续使用剩余日志将数据库恢复到最新日期? 编辑:在我们失踪时没有对数据库进行任何更改,因此事务日志中没有任何内容
是的,仅当日志备份的日志序列号中没有间隙时。
下面将为您详细讲解。
create database logbackup_test
go
-- take a full backup, else the database will be in pseudo-full recovery
backup database logbackup_test to disk = 'C:\sandbox\logbackup_test_full.bak'
with compression, stats =10
use logbackup_test
go
CREATE TABLE dbo.Table_1
(
Name varchar(50) NULL
) ON [PRIMARY]
GO
--Insert a 1st Row in TEST table
Insert into dbo.Table_1 values ('Kin-1')
GO
--Take 1st T-LOG backup.
BACKUP LOG logbackup_test TO DISK = N'C:\sandbox\logbackup_test-TLog1.trn' with stats =10
go
--Insert a 2nd Row in TEST table
Insert into dbo.Table_1 values ('Kin-2')
GO
--Take 2nd T-LOG backup.
BACKUP LOG logbackup_test TO DISK = N'C:\sandbox\logbackup_test-TLog2.trn' with stats =10
go
--Insert a 3rd Row in TEST table
Insert into dbo.Table_1 values ('Kin-3')
GO
--Take 3rd T-LOG backup
BACKUP LOG logbackup_test TO DISK = N'C:\sandbox\logbackup_test-TLog3.trn' with stats =10
go
Run Code Online (Sandbox Code Playgroud)
--
--Take 4th T-LOG backup -- NOTE That there is no activity .. no transactions (NO INSERTS DONE) !!!
BACKUP LOG logbackup_test TO DISK = N'C:\sandbox\logbackup_test-TLog4.trn' with stats =10
go
/*Processed 0 pages for database 'logbackup_test', file 'logbackup_test_log' on file 1.
100 percent processed.
BACKUP LOG successfully processed 0 pages in 0.072 seconds (0.000 MB/sec).
*/
Run Code Online (Sandbox Code Playgroud)
--
--Insert a 4th Row in TEST table
Insert into dbo.Table_1 values ('Kin-4')
GO
--Take 5th T-LOG backup
BACKUP LOG logbackup_test TO DISK = N'C:\sandbox\logbackup_test-TLog5.trn' with stats =10
go
Run Code Online (Sandbox Code Playgroud)
现在检查数据:

下面将向您解释为什么可以跳过日志备份。它依赖于LSN(日志序列号)。详细了解 LSN 超出了本答案的范围,但该链接将使您很好地了解它是什么。
RESTORE HEADERONLY将告诉您first LSN和Last LSN如下:

--- 现在开始恢复
-- restore a full backup
restore database logbackup_test
from disk = N'C:\sandbox\logbackup_test_full.bak'
with norecovery, replace, stats =10
go
-- restore consequent log backups !! - 1,2,3 and 5. WE ARE SKIPPING TLog4 !!
restore log logbackup_test
from disk = N'C:\sandbox\logbackup_test-TLog1.trn'
with norecovery, stats =10
go
restore log logbackup_test
from disk = N'C:\sandbox\logbackup_test-TLog2.trn'
with norecovery, stats =10
go
restore log logbackup_test
from disk = N'C:\sandbox\logbackup_test-TLog3.trn'
with norecovery, stats =10
go
-- NOTE -- I am skipping Log4 as it did not have any transaction .....
restore log logbackup_test
from disk = N'C:\sandbox\logbackup_test-TLog5.trn'
with recovery, stats =10
Run Code Online (Sandbox Code Playgroud)
下面是结果..我们能够恢复日志备份 1、2、3 和 5。我们跳过了 Tlog4,因为 Tlog3 的 LastLSN 是 Tlog5 的第一个 LSN。
