Full backup does not truncate the transaction log file.
Assuming the Following Scenario
- Full backup at 6AM
- TLog backup at 10AM
- Full backup at 1PM
- TLog backup at 6PM
- Crash immediately at the next second (so no tail log backup needed)
Steps to Restore:
- Restore full backup from point 3 (contains data as at 1PM)
- Then, restore the tlog backup from step 4 (contains log from 10AM to 6PM)
Questions
- During recovery, how does the system know that only certain portion of the log file (post 1PM [exclude 10AM to 1PM]) needs to be replayed on the database?
- Does it check the timestamps with in the transaction log backup to compare it with the full backup? Or does it check the LSN?
Additional Scenario
Similarly, in another scenario, let's say we do a Full backup at 10AM, then take a transaction log backup at 12PM. The transaction log will have all transactions up-till 12PM (and even prior to 10AM assuming db existed prior to 10AM and there was no previous transaction log backup).
Now, when we restore the full backup and then apply the transaction log backup, how does the system know that only transactions in the log post 10AM need to be replayed? Since all those prior to 10AM will be already there as part of the full backup restore. Is it checked via timestamps or LSN's?
Best Answer
Each and every restore operation relies (in some part) on the LSN that is stored together with the backup and internally in the
msdb
database.Covering The Basics
From some Microsoft documentation:
(emphasis mine)
Reference: Recover to a Log Sequence Number (Microsoft | Docs | SQL Server)
After reading through some loops you will eventually land on the Microsoft Documentation page for SQL Server Transaction Log Architecture and Management Guide
(emphasis mine)
Reference: SQL Server Transaction Log Architecture and Management Guide (Microsoft | Docs | SQL Server)
Getting Into The System
When you query the
msdb
database with a script I created for my own purpose, you will receive an overview of the relevant LSN numbers that are stored together with the FULL, DIFF and TLOG backups.This will give you an idea of the LSNs that are stored in the backup files. The
msdb
doesn't contain a full list of all LSNs in the backups, but just the main ones considered vital for a manual restore of a database:A description of these columns can be found in the official Microsoft Documentation for the
backupset
table.Example Output of My StackExchange Database
The exact LSNs are stored in the backup files themselves and can be retrieved using 3-rd party tools.
Answering Your Questions
...For SMSS
When you start a restore process a couple of statements are execute in the background, which result in the GUI presenting you with an overview of FULL, DIFF and TLOG backups that would be required to bring your database back into a consistent state. The actual
RESTORE DATABASE ...
andRESTORE LOG ...
will then determine which portions of the corresponding backup files are required to bring the database back into a consistent state, according to the documentation previously mentioned....For Transact-SQL
When you manually have to restore a database, then you will have to look into the
msdb
database (possibly with the aid of my script) to determind which files you require to bring your database back into a consistent state. The actualRESTORE DATABASE ...
andRESTORE LOG ...
will then determine which portions of the corresponding backup files are required to bring the database back into a consistent state, according to the documentation previously mentioned.Summary
The magic is in the LSN stored in the backup files and restore process coded by Microsoft.
You can obtain a license to allow you to plug in to various APIs that would allow you to read more information from the backup files. For that you would have to contact a Microsoft representative.