Solved

date when the database was restored?

Posted on 2016-07-19
6
51 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 24

Expert Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

760 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

19 Experts available now in Live!

Get 1:1 Help Now