join the MSSQLTips community

Today's Site Sponsor


 

SQL Compare quickly and easily compares and synchronizes SQL Server database schemas
 



SQL Server backup and recovery: Idera SQL safe backup

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 -- read/post comments -- print -- Bookmark and Share

Rating: (not rated yet) Rate

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 Free Live Webcast Comment or Ask Questions About This Tip



Get Our Tips Newsletter

We keep 50,000+ SQL Server professionals informed.



Red Gate Software - SQL Backup

Need to create smaller, more reliable backups? Ensure your backups are optimized for robustness and speed with Red Gate SQL Backup Pro. Compress your backups by up to 95% and minimize disruptions to your backups caused by flaky networks with new network resilience. 'Network resilience puts SQL Backup Pro 6 at the top of the list of backup tools. It’s the cherry on top, and I definitely recommend using SQL Backup over SQL Server 2008 native backups.' William Durkin, Development DBA. Download now.

Download now!

More SQL Server Tools
SQL comparison toolset

SQL defrag manager

SQL Backup

SQL Compare

SQL safe backup


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

Free trial: Red Gate SQL Response for no-nonsense monitoring & alerting of SQL Server health & activity. Download now.

SQL Server Consultants - What you don't know could be your biggest asset - Guaranteed Results

Interested in SharePoint? Love the tips? Check this out...

Free whitepaper - Managing Complex Database Changes



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.


CareerQandA.com | MSSharePointTips.com | MSSQLTips.com