This article explores the SQL Server Transaction log backups and log sequence number (LSN) in combination with the Full backups.
本文探讨了SQL Server事务日志备份和日志序列号(LSN)与完整备份的组合。
The database backups are crucial for database recovery and disaster planning. It is the primary duty of a DBA to define the backup policy for each database based on the criticality, Recovery time object (RTO) and Recovery Point Objective (RPO). The database backups are useful even if you implemented the disaster recovery solutions like HADR SQL Server Always On. To meet these requirements, we schedule native or third-party backup tools to take database backups. We have the following database backups in SQL Server.
数据库备份对于数据库恢复和灾难计划至关重要。 DBA的主要职责是根据重要性,恢复时间对象(RTO)和恢复点目标(RPO)为每个数据库定义备份策略。 即使您实施了灾难恢复解决方案(例如HADR SQL Server Always On),数据库备份也很有用。 为了满足这些要求,我们安排本机或第三方备份工具进行数据库备份。 我们在SQL Server中具有以下数据库备份。
We are not going to talk in detail about these backup types. You can refer to the article Understanding SQL Server Backup Types to gather details about them.
我们不会详细讨论这些备份类型。 您可以参考文章了解SQL Server备份类型以收集有关它们的详细信息。
DBA combines these database backups to have a backup policy of a database. Usually, for large databases, we take a weekly full backup and the combination of differential and log backups in between. These database backups build a log chain, and it is very critical to maintain the log chain for database backups. We should also be aware of the actions that can break the log sequence. If the LSN chain is broken, it is difficult to restore the database, and in case of any disaster, if we cannot restore the database, it might create a problematic scenario for the DBA.
DBA组合了这些数据库备份以具有数据库的备份策略。 通常,对于大型数据库,我们每周进行一次完整备份,并在两者之间进行差异备份和日志备份的组合。 这些数据库备份会建立一个日志链,因此维护数据库备份的日志链非常关键。 我们还应该意识到可能破坏日志序列的操作。 如果LSN链断开,则很难还原数据库,如果发生任何灾难,如果我们无法还原数据库,则可能会给DBA带来问题。
Suppose we have the following backup policy for a critical database.
假设我们对关键数据库具有以下备份策略。
In the above scenario, let’s say someone took a Full database backup after the SQL Server Transaction Log backup.
在上述情况下,假设某人在SQL Server事务日志备份之后进行了完整数据库备份。
Now let me ask a few questions here:
现在让我在这里问几个问题:
If you know the answers to these questions, you can skip this article. I am sure most of the DBA would be confused and fail to answer these questions.
如果您知道这些问题的答案,则可以跳过本文。 我确信大多数DBA都会感到困惑,并且无法回答这些问题。
Let’s prepare the environment to explore answers to these questions.
让我们准备一个环境来探索这些问题的答案。
Create a sample database and take a full database backup using the following query. This query takes the full backup, performs CHECKSUM and verify backup once finished.
创建一个示例数据库,并使用以下查询进行完整的数据库备份。 此查询将执行完整备份,执行CHECKSUM并在完成后验证备份。
BACKUP DATABASE [SampleDB] TO DISK = N'E:\DBbackup\SampleDB.bak' WITH NOFORMAT, INIT, NAME = N'SampleDB-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10, CHECKSUM
GO
declare @backupSetId as int
select @backupSetId = position from msdb..backupset where database_name=N'SampleDB' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'SampleDB' )
if @backupSetId is null begin raiserror(N'Verify failed. Backup information for database ''SampleDB'' not found.', 16, 1) end
RESTORE VERIFYONLY FROM DISK = N'E:\DBbackup\SampleDB.bak' WITH FILE = @backupSetId, NOUNLOAD, NOREWIND
GO
Once backup is finished, execute the following query in database context for which we want to extract the details. It checks backup history for full, differential and SQL Server transaction log backups. It also gives the log sequence details for each backup types.
备份完成后,在我们要提取其详细信息的数据库上下文中执行以下查询。 它检查完整,差异和SQL Server事务日志备份的备份历史记录。 它还提供了每种备份类型的日志序列详细信息。
SELECT
s.database_name,
CAST(CAST(s.backup_size / 1000000 AS INT) AS VARCHAR(14)) + ' ' + 'MB' AS bkSize,
CAST(DATEDIFF(second, s.backup_start_date,
s.backup_finish_date) AS VARCHAR(4)) + ' ' + 'Seconds' TimeTaken,
s.backup_start_date,
CAST(s.first_lsn AS VARCHAR(50)) AS first_lsn,
CAST(s.last_lsn AS VARCHAR(50)) AS last_lsn,
CAST(s.database_backup_lsn AS VARCHAR(50)) AS database_backup_lsn,
CAST(s.checkpoint_lsn AS VARCHAR(50)) AS checkpoint_lsn,
CASE s.[type] WHEN 'D' THEN 'Full'
WHEN 'I' THEN 'Differential'
WHEN 'L' THEN 'Transaction Log'
END AS BackupType,
s.recovery_model
FROM msdb.dbo.backupset s
INNER JOIN msdb.dbo.backupmediafamily m ON s.media_set_id = m.media_set_id
WHERE s.database_name = DB_NAME()
ORDER BY backup_start_date DESC, backup_finish_date
GO
In the output, we can look at the following values.
在输出中,我们可以查看以下值。
Now open two new query windows.
现在打开两个新的查询窗口。
Query Window 1: Execute the following query to take SQL Server Transaction Log backup at every 1-minute interval.
查询窗口1:执行以下查询以每1分钟间隔进行一次SQL Server事务日志备份。
BACKUP LOG [SampleDB] TO DISK = N'E:\DBbackup\SampleDB.trn' WITH NOFORMAT, INIT,
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
WAITFOR DELAY '00:01:00.000'
BACKUP LOG [SampleDB] TO DISK = N'E:\DBbackup\SampleDB1.trn' WITH NOFORMAT, INIT,
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
WAITFOR DELAY '00:01:00.000'
BACKUP LOG [SampleDB] TO DISK = N'E:\DBbackup\SampleDB2.trn' WITH NOFORMAT, INIT,
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
WAITFOR DELAY '00:01:00.000'
BACKUP LOG [SampleDB] TO DISK = N'E:\DBbackup\SampleDB3.trn' WITH NOFORMAT, INIT,
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
WAITFOR DELAY '00:01:00.000'
BACKUP LOG [SampleDB] TO DISK = N'E:\DBbackup\SampleDB4.trn' WITH NOFORMAT, INIT,
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
WAITFOR DELAY '00:01:00.000'
BACKUP LOG [SampleDB] TO DISK = N'E:\DBbackup\SampleDB5.trn' WITH NOFORMAT, INIT,
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
WAITFOR DELAY '00:01:00.000'
BACKUP LOG [SampleDB] TO DISK = N'E:\DBbackup\SampleDB6.trn' WITH NOFORMAT, INIT,
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
WAITFOR DELAY '00:01:00.000'
BACKUP LOG [SampleDB] TO DISK = N'E:\DBbackup\SampleDB7.trn' WITH NOFORMAT, INIT,
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
WAITFOR DELAY '00:01:00.000'
BACKUP LOG [SampleDB] TO DISK = N'E:\DBbackup\SampleDB8.trn' WITH NOFORMAT, INIT,
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
Query Window 2: Execute the following query to generate transaction log activity every 30 seconds.
查询窗口2:执行以下查询以每30秒生成一次事务日志活动。
INSERT INTO ExamResult
VALUES
('Lily',
'Maths',
65
);
Go 100
WAITFOR DELAY '00:00:30.000'
INSERT INTO ExamResult
VALUES
('Lily',
'Maths',
65
);
Go 100
WAITFOR DELAY '00:00:30.000'
INSERT INTO ExamResult
VALUES
('Lily',
'Maths',
65
);
Go 100
WAITFOR DELAY '00:00:30.000'
INSERT INTO ExamResult
VALUES
('Lily',
'Maths',
65
);
Go 100
WAITFOR DELAY '00:00:30.000'
INSERT INTO ExamResult
VALUES
('Lily',
'Maths',
65
);
Go 100
WAITFOR DELAY '00:00:30.000'
INSERT INTO ExamResult
VALUES
('Lily',
'Maths',
65
);
Go 100
Once both the query gets completed, rerun the query to check the log backup history.
两个查询都完成后,请重新运行查询以检查日志备份历史记录。
In this screenshot, you can note the following things.
在此屏幕截图中,您可以注意以下几点。
Let’s take two full backup and subsequent SQL Server transaction log backups.
让我们进行两个完整备份和后续SQL Server事务日志备份。
BACKUP DATABASE [SampleDB] TO DISK = N'E:\DBbackup\SampleDB1.bak' WITH NOFORMAT, INIT, NAME = N'SampleDB-Full Database Backup',
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10, CHECKSUM
GO
BACKUP DATABASE [SampleDB] TO DISK = N'E:\DBbackup\SampleDB2.bak' WITH NOFORMAT, INIT, NAME = N'SampleDB-Full Database Backup',
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10, CHECKSUM
GO
WAITFOR DELAY '00:01:00.000'
BACKUP LOG [SampleDB] TO DISK = N'E:\DBbackup\SampleDB9.trn' WITH NOFORMAT, INIT,
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
WAITFOR DELAY '00:01:00.000'
BACKUP LOG [SampleDB] TO DISK = N'E:\DBbackup\SampleDB10.trn' WITH NOFORMAT, INIT,
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
Let’s view the database backup LSN information. In the following screenshot, we can note the following things.
让我们查看数据库备份LSN信息。 在以下屏幕截图中,我们可以注意以下几点。
In the next step, let’s start log backup while the full backup is in running state. Open two new query window in SSMS.
在下一步中,让我们在完整备份处于运行状态时开始日志备份。 在SSMS中打开两个新的查询窗口。
Execute the following query to take full database backup in the first window.
执行以下查询以在第一个窗口中进行完整的数据库备份。
BACKUP DATABASE [SampleDB] TO DISK = N'E:\DBbackup\SampleDBfull23.bak' WITH NOFORMAT, INIT, NAME = N'SampleDB-Full Database Backup',
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10, CHECKSUM
In the second query window, execute the query to take transaction log backup.
在第二个查询窗口中,执行查询以进行事务日志备份。
BACKUP LOG [SampleDB] TO DISK = N'E:\DBbackup\SampleDB23.trn' WITH NOFORMAT, INIT,
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
Once both the backups are finished, view the database backup history again.
两个备份完成后,再次查看数据库备份历史记录。
Observe the following things from the query output.
从查询输出中观察以下内容。
Let’s go back to questions asked initially and find out the answers.
让我们回到最初提出的问题,找出答案。
Will the full backup break the LSN chain?
完整备份会中断LSN链吗?
No, Full backup does not break the log sequence chain.
不,完全备份不会中断日志序列链。
The transaction log backup after the full backup contains data from the full backup or not?
完全备份后的事务日志备份是否包含来自完全备份的数据?
The transaction log backup takes data from the last LSN of previous log backup. It maintains the log chain; however, we can restore the full backup followed by the transaction log backup. SQL Server prepares a restoration plan as per the LSN during restore planning.
事务日志备份从先前日志备份的最后一个LSN中获取数据。 它维护日志链; 但是,我们可以还原完整备份,然后再还原事务日志备份。 在还原计划期间,SQL Server根据LSN准备还原计划。
If the database size is enormous and full backup takes 4-5 hours to complete, what happens to hourly log backup?
如果数据库很大,并且完全备份需要4-5个小时才能完成,那么每小时日志备份会怎样?
Nothing, Log backup can continue to run as usual. The only difference is that it cannot truncate the transaction log due to in-progress full backup. Once we execute log backup after full backup completion, it truncates the log as well.
日志备份可以照常继续运行。 唯一的区别是由于正在进行完全备份,它无法截断事务日志。 在完全备份完成后执行日志备份后,它也会同时截断日志。
Would SQL Server transaction log backup work while the full backup is in progress?
在进行完全备份时,SQL Server事务日志备份是否可以工作?
As per the previous question, transaction log backup execution can work as usual. You will not face any failure in the transaction log backup job due to full backup progress.
按照前面的问题,事务日志备份执行可以照常进行。 由于完整的备份进度,您不会在事务日志备份作业中遇到任何失败。
We can have many reasons that can break the log chain for database backup. If a particular database backup is corrupted or missing, it might impact the log chain and would make it difficult to restore the database.
我们有很多原因可能会破坏数据库备份的日志链。 如果特定的数据库备份已损坏或丢失,则可能会影响日志链,并使恢复数据库变得困难。
Let’s explore the reasons that can break the log sequence.
让我们探讨破坏日志序列的原因。
In this article, we explored the concept of a SQL Server transaction log backup with LSN and how SQL Server maintains the chain with multiple scenarios. DBA should be aware of these scenarios to avoid any mistake that can break the log sequence. You should also perform the database restoration drills on a timely basis to test the recovery of databases from the backups.
在本文中,我们探讨了使用LSN进行SQL Server事务日志备份的概念,以及SQL Server如何在多种情况下维护链。 DBA应该注意这些情况,以避免可能破坏日志序列的任何错误。 您还应该及时执行数据库还原演练,以测试从备份中恢复数据库的情况。
翻译自: https://www.sqlshack.com/understanding-log-sequence-numbers-for-sql-server-transaction-log-backups-and-full-backups/