Backup and Restore TimeStamps

Database  Restore History
How old is the Database if restored from the Backup file :
In lab environment , we always get the copy of production database and restore it. Have you ever wondered how old is the database in lab environment.

SELECT   DISTINCT TOP 100
        RH.destination_database_name ,
        RF.file_number ,
        RH.restore_date ,
        RF.destination_phys_drive ,
        RF.destination_phys_name ,
        BS.name AS [Backup_Set_Name] ,
        BS.description ,
        BS.database_creation_date ,
        BS.backup_start_date ,
        BS.database_name ,
        BS.server_name ,
        RH.restore_type ,
        RH.replace ,
        RH.stop_at
FROM    msdb.dbo.restorehistory AS RH
        LEFT OUTER JOIN msdb.dbo.restorefile AS RF ON RF.restore_history_id = RH.restore_history_id
        LEFT OUTER JOIN msdb.dbo.restorefilegroup AS RG ON RG.restore_history_id = RH.restore_history_id
        LEFT OUTER JOIN msdb.dbo.backupset AS BS ON BS.backup_set_id = RH.backup_set_id
WHERE   1 = 1
-- AND RH.destination_database_name = N'MyDatabaseName' -- SELECT DB_NAME()
-- restore_type : D=Full, I=Differential, L=Log, G=FileGroup, V=VerifyOnly
-- AND RH.restore_type = 'D'
ORDER BY RH.restore_date DESC ,
        RF.file_number DESC;

Database Backup  History


SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

SELECT  p.database_name AS DatabaseName ,
        p.backup_start_date AS 'Backup_Start_Time' ,
        p.backup_finish_date AS 'Backup_Finish_Time' ,
        CAST(( DATEDIFF(MINUTE, p.backup_start_date, p.backup_finish_date) ) AS VARCHAR)
        + ' min  '
        + CAST(( DATEDIFF(ss, p.backup_start_date, p.backup_finish_date) ) AS VARCHAR)
        + ' sec ' AS [Total Time] ,
        CASE p.type
          WHEN 'D' THEN 'Full '
          WHEN 'I' THEN 'Diffrential'
          WHEN 'L' THEN 'Log'
        END AS 'Backup_Type' ,
        CAST(p.backup_size / 1024 / 1024 AS NUMERIC(10, 2)) AS 'Backup_Size_in_MB' ,
        a.physical_device_name AS 'Physical_File_Location' ,
        p.flags
FROM    msdb..backupmediafamily a ,
        msdb..backupset p
WHERE   a.media_set_id = p.media_set_id
        AND p.database_name = ISNULL(NULL, p.database_name)
        AND p.type = 'D'
        AND p.flags = '1536' -- COPY_ONLY Backups
ORDER BY Backup_Start_Time DESC ;

You may also like...