join the MSSQLTips community

Today's Site Sponsor


 

Find performance issues related to Analysis Services memory limits.
 


How to identify when a SQL Server database was restored, the source of the backup and the date of the backup
Written By: Thomas LaRock -- 10/16/2009 -- 7 comments -- printer friendly -- become a member



SQL Server backup and recovery: Idera SQL safe backup

        Win SQL Books  -----  SharePoint Tips  -----  Live Webcast - SQL Backup Mistakes  -----  Bookmark and Share        

Problem
After restoring a database your users will typically run some queries to verify the data is as expected. However, there are times when your users may question whether the restore was done using the correct backup file.  In this tip I will show you how you can identify the file(s) that was used for the restore, when the backup actually occured and when the database was restored.

Solution
The restore information is readily available inside the msdb database, making the solution as easy as a few lines of T-SQL.


When I ask people about how they verify their database restores I often get back a response that includes something similar to the following code:

RESTORE VERIFYONLY FROM DISK = 'G:\dbname.bak'

The above command simply returns this message when successful: "The backup set on file 1 is valid." Is that really useful for your end user that is complaining that the data is not correct? Chances are their complaint is not about if the backup set was valid, but more specifically it is about your selection of the backup file, or the timing of the backup itself.

If the backup was done at the wrong time, or if you restored from the wrong backup file, then the end user may be seeing exactly that problem while reviewing the data. So, how do you provide some proof that you did the restore from the correct backup file? The following script can give you this information.

SELECT [rs].[destination_database_name], 
[rs].[restore_date], 
[bs].[backup_start_date], 
[bs].[backup_finish_date], 
[bs].[database_name] as [source_database_name], 
[bmf].[physical_device_name] as [backup_file_used_for_restore]
FROM msdb..restorehistory rs
INNER JOIN msdb..backupset bs
ON [rs].[backup_set_id] = [bs].[backup_set_id]
INNER JOIN msdb..backupmediafamily bmf 
ON [bs].[media_set_id] = [bmf].[media_set_id] 
ORDER BY [rs].[restore_date] DESC

The script will return the following result set:

Column Name

Description

destination_database_name The name of the database that has been restored.
restore_date The time at which the restore command was started.
backup_start_date The time at which the backup command was started.
backup_finish_date The time at which the backup command completed.
source_database_name The name of the database after it was restored.
backup_file_used_for_restore The file(s) that the restore used in the RESTORE command.

Images

Here is a screenshot of a sample result set returned by the script.


Next Steps

  • Take the above code and execute against your instance.
  • Keep this script handy next time you want to know when a database was restored, what file it came from and when the backup actually occurred
  • Take a look at these other Backup and Recovery tips
Readers Who Read This Tip Also Read Comment or Ask Questions About This Tip Twitter This Tip!



 

 



Idera - SQL secure

Idera SQL secure collects and analyzes permissions data from SQL Server and Active Directory as well as the file system and registry to show who has access to what database objects and how that access is granted. SQL secure also monitors changes made to access rights so that unapproved changes can be easily identified and fixed. SQL secure also collects and evaluates key security settings within SQL Server and provides proactive recommendations to improve server security.

Download now!

More SQL Server Tools
SQL diagnostic manager

SQL secure

SQL defrag manager

SQL Backup

SQL Compare


Sponsor Information
Free SQL Server performance monitoring dashboard – Idera SQL check

The SQL Toolbelt – all 13 Red Gate SQL Server Tools.

Need SQL Server Secrets? We deliver innovative answers via our SQL Server Consulting Services

Stop here to prepare for your next SQL Server interview!

Top 10 SQL Server Backup Mistakes and How to Avoid Them web cast by Greg Robidoux - February 10, 2010

Make the most of MSSQLTips...Sign-up for the newsletter

Getting started with SharePoint? Start your journey with MSSharePointTips.com...

Free whitepaper - Developing Something for Nothing with SQL Server: A Closer Look at SQL Server Express



Copyright (c) 2006-2010 Edgewood Solutions, LLC All rights reserved
privacy statement | disclaimer | copyright | advertise | write for mssqltips | feedback | about
Some names and products listed are the registered trademarks of their respective owners.