Solved

date when the database was restored?

Posted on 2016-07-19
6
53 Views
Last Modified: 2016-07-20
other than
msdb..restorehistory
is it possible to know when was the current database restored? (from the database itself, if possible..)
0
Comment
Question by:25112
6 Comments
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 250 total points
ID: 41720245
Hi,

You may try this:

WITH LastRestores AS
(
SELECT
    DatabaseName = [d].[name] ,
    [d].[create_date] ,
    [d].[compatibility_level] ,
    [d].[collation_name] ,
    r.*,
    RowNum = ROW_NUMBER() OVER (PARTITION BY d.Name ORDER BY r.[restore_date] DESC)
FROM master.sys.databases d
LEFT OUTER JOIN msdb.dbo.[restorehistory] r ON r.[destination_database_name] = d.Name
)
SELECT *
FROM [LastRestores]
WHERE [RowNum] = 1

Open in new window


I will not claim the answer as my own. I found it through a Google Search and it is hosted here.

By the way, this does not seem to work on AzureSQL.

Giannis
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41721127
Try this:
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] 
WHERE
    [rs].[destination_database_name]='<db_name_here>' -- if ignored all database restore info will be returned
ORDER BY 
    [rs].[restore_date] DESC

Open in new window

0
 
LVL 5

Author Comment

by:25112
ID: 41721361
thank you experts..
both solution refers to msdb database..

the confirmation I need is that this information is not available on the user database, at all?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 26

Accepted Solution

by:
Zberteoc earned 250 total points
ID: 41721421
I already stated that the query will return the restore history info for ALL the databases if there is no filter to it. The reason you see the msdb database is that all the backup and restore info for ALL the databases on a SQL instance is stored in tables that reside in the msdb database. That is by design.  But be aware that the restore and backup history is purged periodically so you may miss it for some databases. If you have multiple entries just take the last one in chronological order. In my query I ordered by [restore_date] DESC(ending) so the last restore would be the first backup row for any database. Actually I would restore by database name first:
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] 
--WHERE [rs].[destination_database_name]='<db_name_here>' -- if ignored all database restore info will be returned
ORDER BY
    [rs].[destination_database_name], 
    [rs].[restore_date] DESC

Open in new window

This should return the info for ALL database restored that exist in the history.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41721535
One more thing. I just realize it is possible that the restore process has nothing to do with the backup process if the restore was done from an outside backup file or if the database has no backup info. That means the query should use left join with the backup set but add the [msdb].[dbo].[restorefile] table with INNER JOIN to have the accurate restore info:
SELECT --rs.*,
     [destination_database_name]
    ,[restore_date]
    ,rs.[user_name]
    ,[restore_type]
    ,[replace]
    ,[recovery]
    ,[file_number]
    ,[destination_phys_drive]
    ,[destination_phys_name]
    ,[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].[dbo].[restorefile] rf
	   on rf.restore_history_id=rs.restore_history_id    
    LEFT JOIN msdb..backupset bs
	   ON [rs].[backup_set_id] = [bs].[backup_set_id]
    LEFT JOIN msdb..backupmediafamily bmf 
	   ON [bs].[media_set_id] = [bmf].[media_set_id] 
--WHERE [rs].[destination_database_name]='<db_name_here>' -- if ignored all database restore info will be returned
ORDER BY
    [rs].[destination_database_name], 
    [rs].[restore_date] DESC

Open in new window

0
 
LVL 25

Expert Comment

by:Mohammed Khawaja
ID: 41721599
That is a nice query.
0

Featured Post

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using CTE to insert records into a table 2 28
Add total to a tablix with ReportItem values 15 36
Need a starter for ETL protocol? 4 39
Add '#' to end of file 2 29
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now