Tail-log backup

On this page 7

A tail-log backup is a transaction log backup that captures the log records not yet backed up at the moment a database is damaged, taken offline or about to be restored, so that recovery can continue up to the point of failure instead of stopping at the last scheduled log backup. The term comes from Microsoft SQL Server, where it is a defined step in the restore sequence, and it belongs to the core vocabulary of backup & recovery in relational databases.

The tail of the log is the active portion written after the most recent log backup. Without it, every committed transaction since that backup is lost, so the tail-log backup is often the difference between zero data loss and a gap of minutes or hours.

How a tail-log backup preserves the last transactions

A tail-log backup works by reading the active transaction log directly from the log file and writing every record after the last backed-up log sequence number (LSN) into a backup file. Because the log contains the redo information for every committed change, replaying those records during restore brings the database to its final consistent state.

The operation is only meaningful under the full or bulk-logged recovery model. Under the simple recovery model, the log is truncated at each checkpoint and no log chain exists, so there is nothing to capture. In SQL Server the command is BACKUP LOG with options that control how it behaves when the database is damaged or must remain unchanged.

The following table lists the main options and their effect on the source database.

OptionWhat it doesTypical situation
NORECOVERYBacks up the tail and leaves the database in the restoring statePlanned restore or migration to another server
NO_TRUNCATEBacks up the log even when data files are damaged or missingData file loss with an intact log file
CONTINUE_AFTER_ERRORProceeds despite page or checksum errorsSeverely damaged database, best-effort capture
COPY_ONLYTakes a log copy without affecting the log chainAd hoc copy that must not disturb backup routines
Key options for a tail-log backup in SQL Server

When a tail-log backup is required

A tail-log backup is required whenever a restore must reach the latest committed transaction and the database still has a readable log file. SQL Server enforces this rule: since version 2005, a RESTORE DATABASE over an existing database in the full recovery model fails with an error unless the tail has been backed up or the operator explicitly uses WITH REPLACE or STOPAT.

The common triggers fall into a small number of scenarios, summarized below with the recommended approach.

ScenarioRecommended actionReason
Data file corruptionTail-log backup with NO_TRUNCATELog survives on a separate volume and holds recent work
Planned restore in placeTail-log backup with NORECOVERYFreezes the database so no new changes are lost
Migration or failover cutoverTail-log backup with NORECOVERYFinal log is applied on the target with no gap
Log file destroyedNo tail-log backup possibleRecovery stops at the last regular log backup
Simple recovery modelNot applicableNo log chain is maintained
Scenarios that call for a tail-log backup

The essential precondition is a surviving log file. This is why storage designs place data files and log files on separate physical volumes: a failure of the data volume then leaves the tail intact and recoverable.

The tail-log backup in the restore sequence

In a complete recovery, the tail-log backup is the last link of the log chain and is restored after the full backup, any differential backup and all regular log backups. Each backup in the chain must continue exactly where the previous one ended in terms of LSN, otherwise the restore stops with a gap error.

  1. Take the tail-log backup of the damaged or source database.
  2. Bring back the newest full backup, leaving the database in the restoring state.
  3. Apply a differential backup when available, again without recovering.
  4. Restore each transaction log backup in order with NORECOVERY.
  5. Restore the tail-log backup last, with RECOVERY, to bring the database online.

The tail can also serve point-in-time recovery: with the STOPAT option, the restore halts at a chosen timestamp inside the tail, for example just before an accidental DELETE statement. This makes the tail useful not only after hardware failure but also after logical errors.

A tail-log backup is technically a transaction log backup, but it differs in timing and purpose from routine log backups and from the equivalent mechanisms in other database engines. The comparison below places it among its closest neighbors.

ConceptWhen it runsMain purpose
Tail-log backupOnce, at failure or before restoreCapture the final unsaved log records
Transaction log backupOn a schedule, for example every 15 minutesMaintain the log chain and limit data loss
Full backupDaily or weeklyProvide the base for every restore
WAL archivingContinuously, per completed segmentPostgreSQL equivalent of a log chain
Binary logContinuously, rotated by size or timeMySQL change record used for replay
Tail-log backup compared with related concepts

Other engines solve the same problem differently. In PostgreSQL, the last partially filled WAL segment in pg_wal plays the role of the tail and must be copied manually before restoring. In Oracle, RMAN applies online redo logs during recovery when they survive. The concept is universal; the term and the explicit command are specific to SQL Server.

Limitations and common mistakes

The most serious limitation is that a tail-log backup cannot rescue a lost log file. If the log volume fails, recovery ends at the last scheduled log backup, and the interval between log backups becomes the real recovery point objective (RPO).

  • Skipping the tail by using WITH REPLACE overwrites the database and silently discards recent transactions.
  • Bulk-logged operations in the tail cannot be captured if the data files holding the changed extents are damaged, so the tail may be unusable.
  • Forgetting NORECOVERY during a migration lets users keep writing to the source after the final backup, creating changes that never reach the target.
  • Storing the tail on the failing server defeats its purpose; it should be written to independent storage immediately.
  • A tail taken with CONTINUE_AFTER_ERROR may contain incomplete metadata, and its restore must be tested before trusting it.

The table below links typical symptoms during restore to their cause and remedy.

SymptomLikely causeFix
Tail not backed up errorRestore attempted without capturing the tailBack up the tail or consciously use REPLACE
LSN gap errorA log backup is missing from the chainLocate the missing file or stop at the gap
Database stuck restoringFinal restore issued without RECOVERYRun RESTORE DATABASE with RECOVERY
Restore errors related to the tail of the log

Explained without jargon

A database keeps a running diary of every change it makes. Regular backups copy that diary every few minutes, but the pages written since the last copy exist only on the server. A tail-log backup grabs those final pages before anything else happens.

Imagine a notebook damaged by water while its last few pages are still dry. Copying those dry pages first lets you rebuild everything. The key point: take the tail first, then restore, and almost nothing is lost.

A typical use case

An online retailer keeps customer orders in a SQL Server database configured for full logging, with a nightly full backup and log backups every 15 minutes. Data files sit on one storage array and the log file on another. At 14:07 the data array fails, and the last log backup completed at 14:00.

The database administrator runs BACKUP LOG with NO_TRUNCATE against the intact log file, writing the tail to a network share. She then restores the nightly full backup, all log backups up to 14:00 and finally the tail with RECOVERY on a standby server. All orders placed between 14:00 and 14:07 are recovered, and the shop returns to service in about 40 minutes with zero lost transactions instead of seven minutes of missing orders.