web-dev-qa-db-fra.com

Comment interroger la dernière date de restauration dans SQL Server?

Existe-t-il une requête T-SQL qui affiche la dernière date de restauration pour une certaine base de données?

72
Kees C. Bakker

Cela répertoriera toutes les restaurations "les plus récentes" pour chaque base de données sur votre serveur:

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
124
marc_s

Le script qui peut être utilisé est:

declare @DB sysname = 'MyDB';
select * from msdb.dbo.restorehistory where destination_database_name = @DB;
16
Kees C. Bakker

la table principale à cet effet est msdb..restorehistory

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

de mssqltips

11
Iman Abidi

Il vous donnera la dernière date de rafraîchissement de votre base de données:

use [msdb]
select top 1 * from restorehistory 
where destination_database_name='DB_NAME'
order by restore_history_id desc 
4
Rajendra Maloo

Existe-t-il une requête T-SQL qui affiche la dernière date de restauration pour une certaine base de données?

Bonjour, j'ai généré le script ci-dessous qui peut vous donner des informations beaucoup plus détaillées sur votre requête.

Requête pour obtenir les informations "Restauration la plus récente" sur la base de données spécifique:

WITH MostRecentRestore AS
(
SELECT 
RowNum = ROW_NUMBER() OVER (PARTITION BY RH.Destination_database_name ORDER BY RH.Restore_Date DESC),
RH.Restore_date, 
BS.[database_name] as Source_Database, 
RH.Destination_Database_Name, 
BS.Backup_Start_Date, 
BS.Backup_Finish_Date, 
CASE WHEN RH.restore_type = 'D' THEN 'Database'
  WHEN RH.restore_type = 'F' THEN 'File'
  WHEN RH.restore_type = 'G' THEN 'Filegroup'
  WHEN RH.restore_type = 'I' THEN 'Differential'
  WHEN RH.restore_type = 'L' THEN 'Log'
  WHEN RH.restore_type = 'V' THEN 'Verifyonly'
  WHEN RH.restore_type = 'R' THEN 'Revert'
  ELSE RH.restore_type 
END AS Restore_Type,
RH.[Replace],
RH.[Recovery],
RH.Restore_Date AS Restored_On,
BMF.physical_device_name AS Restored_From,
RF.destination_phys_name AS Current_DB_File_Location,
RH.user_name AS Restored_By,
BS.machine_name,
BS.Server_Name
FROM msdb.dbo.RestoreHistory RH 
INNER JOIN msdb.dbo.BackupSet BS ON RH.backup_set_id = BS.backup_set_id
INNER JOIN msdb.dbo.restorefile RF ON RH.Restore_History_id = RF.Restore_History_id
INNER JOIN msdb.dbo.Backupmediafamily BMF ON bs.media_set_id = bmf.media_set_id
)
SELECT *
FROM MostRecentRestore
WHERE [RowNum] = 1 AND destination_database_name = 'YourDatabaseName'
1
Kundan Dasange

Ajout d'informations importantes sur le nom d'utilisateur du système d'exploitation et le nom de la machine:

Select Destination_database_name, 
       restore_date,
       database_name as Source_database,
       Physical_device_name as Backup_file_used_to_restore,
       bs.user_name,
       bs.machine_name
from msdb.dbo.restorehistory rh 
  inner join msdb.dbo.backupset bs 
    on rh.backup_set_id=bs.backup_set_id
  inner join msdb.dbo.backupmediafamily bmf 
    on bs.media_set_id =bmf.media_set_id
ORDER BY [rh].[restore_date] DESC
1
Edwin Arias
Select Destination_database_name, 
       restore_date,database_name as Source_database,
       Physical_device_name as Backup_file_used_to_restore 
from msdb.dbo.restorehistory rh 
  inner join msdb.dbo.backupset bs 
    on rh.backup_set_id=bs.backup_set_id
  inner join msdb.dbo.backupmediafamily bmf 
    on bs.media_set_id =bmf.media_set_id
ORDER BY [rh].[restore_date] DESC

Pour des informations détaillées, vous pouvez consulter le lien ci-dessous:

http://www.passionforsql.com/how-to-find-when-last-database-backuprestore-was-done/

0
Ritu