?
Solved

date when the database was restored?

Posted on 2016-07-19
6
Medium Priority
?
75 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 1000 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 27

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Accepted Solution

by:
Zberteoc earned 1000 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 27

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

777 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