Click here to Skip to main content
15,885,080 members
Articles / Database Development / SQL Server / SQL Server 2012
Tip/Trick

SQL Server : How to Keep Database Restore History

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
24 Oct 2012CPOL 14.6K   3   2
Backup and then restore of a database is a normal job for developers and DBAs. This script will be helpfull to findout, from which path, and which backup file a database was restored.

Background 

Recently, one of our DBA restored a database on live server, but with old backup accidentally. Later on, we have found that it was hard to detect which backup was actually restored. To check, which backups we have created for database we have a perfect script, which you can find over here and here. But unfortunately no such script found anywhere to get restore history.

Here is a script, we have used to create a job, which will fetch restore related log entries from SQL Server Log and will archive it to a user created history table.

Using the code

/************************
Script Purpose: To Keep Database Restore Log
Script By : Aasim Abdullah for https://connectsql.blogspot.com
************************/

USE master
GO
CREATE TABLE DatabaseRestoreLog
(DatabaseName VARCHAR(50), RestoreDate DATETIME, RestoredFrom VARCHAR(500))

GO
/****** Start: Job step script *****/
-- Table variable to hold intermediate data
DECLARE @ReportSQLErrorLogs TABLE
    (
      [log_date] [datetime] NULL,
      [processinfo] [varchar](255) NULL,
      [processtext] [text] NULL
    )

DECLARE @NumErrorLogs INT,
    @CurrentLogNum INT

SET @CurrentLogNum = 0

-- Get total number of log files from registry
EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
    N'Software\Microsoft\MSSQLServer\MSSQLServer', N'NumErrorLogs',
    @NumErrorLogs OUTPUT

SELECT @NumErrorLogs = ISNULL(@NumErrorLogs, 6)
 -- NULL in registry entry for Error Log files mean default of 6 value

WHILE @CurrentLogNum < @NumErrorLogs
    BEGIN
        insert into @ReportSQLErrorLogs
                exec master..xp_readerrorlog @CurrentLogNum
        PRINT @CurrentLogNum
        SELECT @CurrentLogNum = @CurrentLogNum + 1
    END

INSERT INTO DatabaseRestoreLog
SELECT SUBSTRING(processtext, CHARINDEX('base:', processtext, 1) + 5,
                  CHARINDEX(',', processtext, 0) - ( CHARINDEX('base:', processtext, 0) )
                  - 5), log_date,
            SUBSTRING(processtext, CHARINDEX(': {''', processtext, 1) + 4,
                  CHARINDEX('''})', processtext, 0) - ( CHARINDEX(': {''', processtext, 0) )- 4)
FROM @ReportSQLErrorLogs
WHERE processtext LIKE 'Database was restored%'
--For last 24 hours
AND DATEDIFF(HH,log_date,GETDATE()) <=24
ORDER BY log_date DESC

/****** End: Job step script *****/

Output of history table will be as following. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader CureMD
Pakistan Pakistan
Aasim Abdullah is working as SQL Server DBA with CureMD (www.curemd.com) based in NY, USA. He has been working with SQL Server since 2007 (Version 2005) and has used it in many projects as a developer, administrator, database designer. Aasim's primary interest is SQL Server performance tuning. If he finds the time, he like to sketch faces with graphite pencils.

Comments and Discussions

 
QuestionThanks Pin
one_database29-Oct-12 9:28
one_database29-Oct-12 9:28 
GeneralMy vote of 5 Pin
Tinier24-Oct-12 19:45
Tinier24-Oct-12 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.