The data-file disk of a SQL Server database in FULL recovery has failed but its log disk survived; how do you restore it without losing committed transactions?
answer
- back up before you restore
- the tail of the log
- NO_TRUNCATE when the data files are gone
- an identical STOPAT for an earlier target
basics
~20 sFirst back up the log tail with BACKUP LOG ... WITH NO_TRUNCATE. Then restore the last full, newest differential and later log backups WITH NORECOVERY, apply the tail and recover. Skipping the tail loses every commit since the last log backup.
solid answer
~50 sEverything committed since the last scheduled log backup exists only in the surviving log file, so the first step is a **tail-log backup**. With the data files gone the database cannot open normally, so use `BACKUP LOG ... WITH NO_TRUNCATE` (or `CONTINUE_AFTER_ERROR`), which attempts the backup even though the database is damaged; it succeeds only if the log files are intact and the tail holds no bulk-logged changes. Then restore the most recent full backup and the latest differential `WITH NORECOVERY`, every later log backup `WITH NORECOVERY`, and the tail-log backup `WITH RECOVERY`. If the goal were to stop before a bad statement instead, the same chain is restored with an identical `STOPAT` on every `RESTORE LOG`. On a database that is still online, the tail is taken `WITH NORECOVERY` so nothing can change after it.
code
sql · 10 lines-- 1. Save the tail while the data files are unavailable
BACKUP LOG Payments
TO DISK = N'E:\bak\Payments_tail.trn'
WITH NO_TRUNCATE, CHECKSUM;
-- 2. Rebuild from the chain, ending with the tail
RESTORE DATABASE Payments FROM DISK = N'E:\bak\Payments_full.bak' WITH NORECOVERY, REPLACE;
RESTORE DATABASE Payments FROM DISK = N'E:\bak\Payments_diff.bak' WITH NORECOVERY;
RESTORE LOG Payments FROM DISK = N'E:\bak\Payments_log_1400.trn' WITH NORECOVERY;
RESTORE LOG Payments FROM DISK = N'E:\bak\Payments_tail.trn' WITH RECOVERY;go deeper
Recall that the log since the last log backup must be backed up before a restore, or those commits are lost.
Explain the difference between NORECOVERY, NO_TRUNCATE and CONTINUE_AFTER_ERROR on BACKUP LOG, and why each RESTORE LOG needs the same STOPAT.
Walk through the full sequence under pressure, including REPLACE, chain verification from msdb and what bulk-logged work does to the tail.
Make sure log files sit on storage that fails independently of data files and that restore drills include the tail-log step, not just the full backup.
## Why the tail matters In the FULL recovery model SQL Server can restore a database to the last committed transaction, but only if every log record since the last data backup is in some backup. Scheduled log backups cover everything up to the most recent one. The records written after it, the **tail of the log**, live only in the log file. A **tail-log backup** copies them and is the last backup of interest in the restore. If it cannot be taken, every transaction committed after the latest log backup is lost. A tail-log backup is not needed when the target point is already inside an earlier log backup, or when you are deliberately overwriting the database with an older copy. ## Choosing the BACKUP LOG options Which options you use depends on the state of the database: | Situation | Option | Effect | |---|---|---| | Database online, about to be restored | `WITH NORECOVERY` | Backs up the tail and leaves the database in the RESTORING state, so nothing changes after the backup | | Data files lost or damaged, log intact | `WITH NO_TRUNCATE` | Attempts the backup even though the database is damaged and does not truncate the log; equivalent to `COPY_ONLY` plus `CONTINUE_AFTER_ERROR` | | Database damaged | `WITH CONTINUE_AFTER_ERROR` | Continues past errors; the backup's metadata may be incomplete, but the log it captures is usable | `NO_TRUNCATE` is for emergencies only. On a healthy database it skips truncation, so routine log backups taken that way never free space. A tail-log backup taken from a damaged database may show `has_incomplete_metadata = 1` in `msdb.dbo.backupset`; only the metadata is incomplete, not the log. ## The restore sequence With the tail saved and new storage in place for the data files: 1. Restore the most recent full backup `WITH NORECOVERY`. Because the tail is already saved, `REPLACE` can override the safety check that otherwise refuses to overwrite a database whose tail has not been backed up. 2. Restore the most recent differential based on that full backup `WITH NORECOVERY`, if one exists. 3. Restore every log backup taken after that differential, in order, `WITH NORECOVERY`. 4. Restore the tail-log backup `WITH RECOVERY`. Recovery rolls back transactions that were still open at the failure and brings the database online with every committed transaction. Afterwards, take a new full backup so that the next incident does not depend on replaying the whole chain again, and check `msdb.dbo.restorehistory` and the error log to confirm every step ran in the order intended. Keeping the log files on storage that fails independently of the data files is what made this recovery lossless, so preserve that separation on the rebuilt server. ## Stopping earlier with STOPAT If the problem was a bad statement rather than a dead disk, the same chain is restored but stopped short of the damage: - Every `RESTORE LOG` in the sequence carries the **same** `STOPAT = 'time'`, so no step can run past the target. `STOPATMARK` and `STOPBEFOREMARK` do the same for a named, marked transaction. - The recovery point is the latest commit at or before that time. - If a log backup does not reach the requested time, SQL Server warns and leaves the database unrecovered, so you can apply the next one. - The full backup you start from must finish before the target time; an older full backup works too, as long as every later log backup is applied. This is usually done into a database with a new name, so that rows can be copied back without throwing away the hours of valid work done since. ## What can stop a tail-log backup - The log file itself is damaged or lost; that is why log files belong on fault-tolerant storage, apart from the data files. - Bulk-logged changes since the last log backup. A log backup of minimally logged work must read the changed data extents, so with the data files gone it fails. - The database is in a state where `BACKUP` is refused outright, such as `OFFLINE` or `EMERGENCY`. ## Verifying the chain first Before restoring, read `msdb.dbo.backupset` for the database ordered by `backup_finish_date`. For log backups, each one's `first_lsn` should equal the previous one's `last_lsn`. `RESTORE HEADERONLY` shows the same values for backup files copied from elsewhere. A gap means a missing log backup, and the restore cannot get past it.
- In SQL Server, what is lost if bulk-logged operations ran after the last log backup and the data files then fail?A log backup that covers minimally logged work has to read the changed data extents, and those are on the failed disk, so the tail-log backup fails. Every commit since the last log backup is then lost. That is a reason to keep BULK_LOGGED windows short and to take a log backup as soon as the database is back in FULL.
- How do you confirm that a SQL Server log chain is complete before starting a restore?Query msdb.dbo.backupset for the database in backup_finish_date order and check that each log backup's first_lsn equals the previous log backup's last_lsn; for files from another server, RESTORE HEADERONLY shows the same LSNs. A gap means a missing log backup, and the restore cannot cross it, so find the missing file first.
- Restore the latest full backup first and take the tail-log backup afterwards.
- WITH REPLACE alone is enough; the log since the last backup is not needed.
- A tail-log backup can still be taken after the log file is lost.
- STOPAT is needed only on the last RESTORE LOG in the sequence.
- NO_TRUNCATE is a good default for routine scheduled log backups.