John McCormack DBA

SQL Server Databases and Cloud

  • Personal
    • About
  • Free Training
    • SQL Server on Amazon RDS (Free Course)
    • Free practice questions to help you pass DP-900
  • Save money in Azure
    • Azure IaaS SQL Backups – Stop burning money
    • Your Azure SQL Database and Managed Instance is too big
    • Turn the cloud off at bedtime to save 70%
    • Your Azure SQL Virtual Machine might be too big
    • Save money with Azure SQL DB serverless
    • Save up to 73% with reserved instances
    • Delete unused instances to save money in Azure
  • Hire me
    • 60 minute cost optimization
    • Let me solve your SQL Server problems
    • Take a look at my Sessionize speaker’s profile

Check when log backups were last restored

17th November 2014 By John McCormack Leave a Comment

This script is useful if want to check when transaction log backups were last restored to a specific database.

With minor amendments, you can also check other restore types and check for all databases, not just a specific one.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/***************************************************************************************
* Checks when a specific database was last restored from a log backup
* restore_type options are listed at bottom of script
* restore_date is useful in alerting/troubleshooting. This example is looking for log
restores within the last day. If no rows are returned, this would be an indication that
log shipping is not working
* destination_database_name can be given a value to check for specific database or
commented out if you want to return info on all databases
****************************************************************************************/
select restore_date, physical_device_name
from msdb..restorehistory as a
inner join msdb..backupset as b on a.backup_set_id = b.backup_set_id
inner join msdb..backupmediaset as c on b.media_set_id = c.media_set_id
inner join msdb..backupmediafamily as d on c.media_set_id = d.media_set_id
where a.destination_database_name = 'ANALYSIS' -- Comment out this line for all databases
and restore_type = 'L' -- See comments below for restore_type
and restore_date > DATEADD(d, -1, getdate())
order by restore_date DESC
/*
Restore_type =
D = Database
F = File
G = Filegroup
I = Differential
L = Log
V = Verifyonly
Can be NULL
*/
/*************************************************************************************** * Checks when a specific database was last restored from a log backup * restore_type options are listed at bottom of script * restore_date is useful in alerting/troubleshooting. This example is looking for log restores within the last day. If no rows are returned, this would be an indication that log shipping is not working * destination_database_name can be given a value to check for specific database or commented out if you want to return info on all databases ****************************************************************************************/ select restore_date, physical_device_name from msdb..restorehistory as a inner join msdb..backupset as b on a.backup_set_id = b.backup_set_id inner join msdb..backupmediaset as c on b.media_set_id = c.media_set_id inner join msdb..backupmediafamily as d on c.media_set_id = d.media_set_id where a.destination_database_name = 'ANALYSIS' -- Comment out this line for all databases and restore_type = 'L' -- See comments below for restore_type and restore_date > DATEADD(d, -1, getdate()) order by restore_date DESC /* Restore_type = D = Database F = File G = Filegroup I = Differential L = Log V = Verifyonly Can be NULL */
/***************************************************************************************
* Checks when a specific database was last restored from a log backup

* restore_type options are listed at bottom of script

* restore_date is useful in alerting/troubleshooting. This example is looking for log
restores within the last day. If no rows are returned, this would be an indication that
log shipping is not working

* destination_database_name can be given a value to check for specific database or
commented out if you want to return info on all databases
****************************************************************************************/

select restore_date, physical_device_name
from msdb..restorehistory as a
inner join msdb..backupset as b on a.backup_set_id = b.backup_set_id
inner join msdb..backupmediaset as c on b.media_set_id = c.media_set_id
inner join msdb..backupmediafamily as d on c.media_set_id = d.media_set_id
where a.destination_database_name = 'ANALYSIS' -- Comment out this line for all databases
and restore_type = 'L' -- See comments below for restore_type
and restore_date > DATEADD(d, -1, getdate())
order by restore_date DESC

/*
Restore_type =
D = Database
F = File
G = Filegroup
I = Differential
L = Log
V = Verifyonly

Can be NULL
*/

More info on log shipping is available at msdn.

Share this:

  • Tweet
  • Email

Related

Filed Under: front-page, T-SQL Tagged With: check when log backups were last restored, log restore, log restores, log shipping, t-logs, t-sql, transaction logs

About John McCormack

John McCormack is an experienced SQL DBA with extensive knowledge of the two largest public clouds: AWS and Azure.

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

John McCormack · Copyright © 2025