SQL Server Log Backup Restore Chain Guide
Bringing a database back to a recent point means applying its backups in one order: the full backup, the latest differential if there is one, then every log backup since, oldest first. This script reads that chain out of msdb and then shows the restore sequence, with NORECOVERY on every step except the last.
Purpose and Overview
Getting a database back to the minute before a failed deployment or an accidental delete is a restore-chain job. The full backup alone returns the database to the night it was taken; the differential and log backups carry it forward from there. Each piece has to be applied in sequence, and the database has to stay in the restoring state until the last one is in.
The first part of the script lists the backups msdb has recorded for one database: type, start and finish time, the file each was written to, and the log sequence numbers (LSNs) it covers. That is the inventory to check before starting. The second part is the restore itself, in two shapes: full, differential and logs, or full and logs when no differential exists.
WITH NORECOVERY leaves the database restoring so the next backup can be applied. WITH RECOVERY on the final step brings it online.
Code Breakdown
The script has two parts: reading the backup history from msdb, then applying the chain.
1-- =============================================
2-- SQL Server Restore Chain: Full + Diff + Log
3-- Step 1: Query msdb to find the restore chain
4-- =============================================
5
6-- Find the most recent Full, Differential, and Log backups for a database
7DECLARE @DatabaseName SYSNAME = N'YourDatabaseName'; -- Set your database name
8
9SELECT
10 bs.database_name,
11 CASE bs.type
12 WHEN 'D' THEN 'Full'
13 WHEN 'I' THEN 'Differential'
14 WHEN 'L' THEN 'Transaction Log'
15 END AS backup_type,
16 bs.backup_start_date,
17 bs.backup_finish_date,
18 bmf.physical_device_name AS backup_file,
19 bs.first_lsn,
20 bs.last_lsn
21FROM msdb.dbo.backupset bs
22JOIN msdb.dbo.backupmediafamily bmf
23 ON bs.media_set_id = bmf.media_set_id
24WHERE bs.database_name = @DatabaseName
25 AND bs.type IN ('D', 'I', 'L')
26ORDER BY bs.backup_start_date DESC;
27
28-- =============================================
29-- Step 2: Apply the restore chain
30-- Replace file paths with your actual backup file locations
31-- =============================================
32
33-- Restore Full backup (NORECOVERY keeps DB in restoring state)
34RESTORE DATABASE [YourDatabaseName]
35FROM DISK = N'C:\Backups\YourDatabaseName_Full.bak'
36WITH NORECOVERY, STATS = 10;
37
38-- Restore Differential backup (if one exists — use NORECOVERY)
39RESTORE DATABASE [YourDatabaseName]
40FROM DISK = N'C:\Backups\YourDatabaseName_Diff.bak'
41WITH NORECOVERY, STATS = 10;
42
43-- Restore Transaction Log backups in order (use NORECOVERY for each except the last)
44RESTORE LOG [YourDatabaseName]
45FROM DISK = N'C:\Backups\YourDatabaseName_Log1.bak'
46WITH NORECOVERY;
47
48RESTORE LOG [YourDatabaseName]
49FROM DISK = N'C:\Backups\YourDatabaseName_Log2.bak'
50WITH NORECOVERY;
51
52-- Final log backup: use RECOVERY to bring the database online
53RESTORE LOG [YourDatabaseName]
54FROM DISK = N'C:\Backups\YourDatabaseName_LogFinal.bak'
55WITH RECOVERY;
56
57-- =============================================
58-- Alternative: If no differential exists,
59-- apply log backups directly after full backup
60-- =============================================
61
62-- Restore Full (NORECOVERY)
63RESTORE DATABASE [YourDatabaseName]
64FROM DISK = N'C:\Backups\YourDatabaseName_Full.bak'
65WITH NORECOVERY;
66
67-- Apply each log in sequence, RECOVERY on the last one
68RESTORE LOG [YourDatabaseName]
69FROM DISK = N'C:\Backups\YourDatabaseName_Log1.bak'
70WITH RECOVERY;
Step 1: Read the Backup History from msdb
msdb.dbo.backupset holds one row per backup; msdb.dbo.backupmediafamily holds the file or device each was written to. Joining them on media_set_id gives the type, the start and finish times, the physical file and the LSN range of every backup of the database.
The CASE expression turns the one-letter type code into a label: D is a full backup, I a differential, L a log backup. Sorting by backup_start_date DESC puts the newest first, which is where the chain to restore begins its search: the latest full backup, the latest differential after it, then every log backup after that.
first_lsn is the first log record in a backup and last_lsn is the next log record after it. For an unbroken run of log backups, each one's first_lsn matches the previous log backup's last_lsn. A mismatch means a log backup is missing and the chain stops there.
Step 2: Restore the Full Backup with NORECOVERY
1RESTORE DATABASE [YourDatabaseName]
2FROM DISK = N'C:\Backups\YourDatabaseName_Full.bak'
3WITH NORECOVERY, STATS = 10;
The full backup always comes first. NORECOVERY leaves the database restoring: it cannot be queried yet, but it can take the next backup. STATS = 10 prints a progress line every 10 percent.
Step 3: Restore the Latest Differential (Optional)
1RESTORE DATABASE [YourDatabaseName]
2FROM DISK = N'C:\Backups\YourDatabaseName_Diff.bak'
3WITH NORECOVERY, STATS = 10;
A differential holds every change since the full backup it is based on, so only the most recent one is needed. It is restored with RESTORE DATABASE, again WITH NORECOVERY because log backups follow. With no differential, skip straight to the logs.
Step 4: Restore the Log Backups in Order
1RESTORE LOG [YourDatabaseName]
2FROM DISK = N'C:\Backups\YourDatabaseName_Log1.bak'
3WITH NORECOVERY;
4
5RESTORE LOG [YourDatabaseName]
6FROM DISK = N'C:\Backups\YourDatabaseName_Log2.bak'
7WITH NORECOVERY;
Log backups taken after the full or differential are applied with RESTORE LOG, in the order they were taken. If one in the middle is lost or damaged, the database can only be restored up to the log before it.
Step 5: The Final Log Backup WITH RECOVERY
1RESTORE LOG [YourDatabaseName]
2FROM DISK = N'C:\Backups\YourDatabaseName_LogFinal.bak'
3WITH RECOVERY;
RECOVERY on the last step rolls back any transactions that were still open and brings the database online. Once it has run, no further backups can be applied; going further means starting again from the full backup.
Alternative: Full Backup Then Logs
1RESTORE DATABASE [YourDatabaseName]
2FROM DISK = N'C:\Backups\YourDatabaseName_Full.bak'
3WITH NORECOVERY;
4
5RESTORE LOG [YourDatabaseName]
6FROM DISK = N'C:\Backups\YourDatabaseName_Log1.bak'
7WITH RECOVERY;
Without a differential, the log backups follow the full backup directly. The rule is the same: NORECOVERY on every step but the last.
Key Benefits and Use Cases
- Point-in-time recovery — replaying log backups returns the database to a moment inside the backup window, for example just before rows were deleted.
- Disaster recovery — full, differential and logs is the standard way to rebuild a database on new hardware after a failure.
- Server migration — restore the chain onto the new server WITH NORECOVERY, then apply a final log backup taken from the source and recover.
- Test refreshes — load a copy of production into a test environment as of a known point.
- Log shipping — the same restore-log-with-NORECOVERY cycle, run on a schedule against a secondary server.
Performance Considerations
- Watch the progress:
STATS = 10(or5) on full and differential restores gives regular progress lines; without it a large restore prints nothing until it finishes. - Restores are I/O-bound: read speed on the backup files and write speed on the data and log volumes decide most of the time, so backups on a network share restore more slowly than local ones.
- Take the tail of the log first: before restoring over a damaged database, back up the tail of its log so the transactions after the last scheduled log backup are not lost.
- Restoring over an existing database: restoring onto a live database name replaces it; use a new name, with MOVE for the files, when the current copy may still be needed.
Practical Tips
- Query msdb first — run the Step 1 query and confirm every file it lists is still on disk before the first RESTORE.
- Recover once, at the end — a RECOVERY on any earlier step ends the chain, and the remaining backups cannot be applied without starting over from the full backup.
- Recover as a separate step — Microsoft's suggested pattern is to restore every log WITH NORECOVERY, then run
RESTORE DATABASE YourDatabaseName WITH RECOVERYon its own. - Stop at a time — add
STOPAT = '2026-04-11T14:30:00'to the final RESTORE LOG to recover only up to that moment. - Rehearse it — restore the chain on a non-production server on a schedule; a backup that has never been restored has not been tested.
Conclusion
The restore chain is full, then the latest differential, then every log backup in order, with NORECOVERY until the last step and RECOVERY at the end. Reading msdb.dbo.backupset first shows whether the chain is complete before anything is overwritten. Together with a RESTORE ... MOVE script, it covers most of the restores a DBA runs.
References
- Apply transaction log backups — Microsoft's requirements for the order of a restore sequence and when to recover
- RESTORE statements (Transact-SQL) — RESTORE DATABASE and RESTORE LOG syntax, including NORECOVERY, RECOVERY and STOPAT
- Transaction log backups — how the log chain works and why a tail-log backup comes before a restore
- backupset (Transact-SQL) — the msdb table the Step 1 query reads, with the meaning of first_lsn and last_lsn
- backupmediafamily (Transact-SQL) — the msdb table that records each backup's physical file
Posts in this series
- SQL Server Point-in-Time Restore with STOPAT
- SQL Server Verify Backup Files: RESTORE VERIFYONLY
- SQL Server Log Backup Restore Chain Guide
- SQL Server Script to Backup All Databases to Disk
- SQL Server Restore Database Script with MOVE
- SQL Server Backup Status Report: All Databases
- SQL Server Backup History: Duration and Size Report
- SQL Server Backup Compression: Enable and Verify
- SQL Server: Find Databases With No Recent Backup
- SQL Server Copy-Only Backup Script for Dev Restore