DBAtlas · Database operations atlas

Monitor log shipping status and latency

SQL ServerAlways On & HA

Command

-- Summary (on the primary, secondary or monitor server):
EXEC master.sys.sp_help_log_shipping_monitor;

-- PRIMARY: when the last log backup was taken:
SELECT p.primary_server, p.primary_database,
       p.last_backup_date, p.last_backup_file,
       DATEDIFF(MINUTE, p.last_backup_date, GETDATE()) AS backup_age_min,
       p.backup_threshold                              AS threshold_min
FROM   msdb.dbo.log_shipping_monitor_primary p;

-- SECONDARY: copy and restore latency:
SELECT s.secondary_server, s.secondary_database, s.primary_server,
       s.last_copied_date, s.last_copied_file,
       s.last_restored_date, s.last_restored_file,
       s.last_restored_latency                           AS restore_latency_min,
       DATEDIFF(MINUTE, s.last_restored_date, GETDATE()) AS since_last_restore_min,
       s.restore_threshold                               AS threshold_min
FROM   msdb.dbo.log_shipping_monitor_secondary s;

-- Errors in the last 24 hours:
SELECT e.database_name, e.agent_type, e.log_time, e.message
FROM   msdb.dbo.log_shipping_monitor_error_detail e
WHERE  e.log_time >= DATEADD(HOUR, -24, GETDATE())
ORDER BY e.log_time DESC;

-- Secondary DB mode (RESTORING = NORECOVERY, is_in_standby = STANDBY):
SELECT name, state_desc, is_in_standby FROM sys.databases WHERE name = N'Musteri';

What it does

Shows the age of the last log backup on the primary, the last copied/restored file and restore latency on the secondary together with their thresholds, and the backup/copy/restore job errors from the last 24 hours.

When to use it

When the log shipping alert job fires (errors 14420/14421), before a DR test, or when reporting how far behind the secondary is.

Caution

Monitor tables contain only the side the server sees: the primary table is populated on the primary/monitor, the secondary table on the secondary/monitor. A manual (non-log-shipping) log backup breaks the chain and the restore job keeps failing; use COPY_ONLY for ad-hoc log backups. In STANDBY mode user connections are dropped before each restore. Read-only query; safe.

Open in DBAtlas →

Related