Click here to Skip to main content
13,344,281 members (59,264 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 24 Oct 2012

SQL Server : How to Keep Database Restore History

, 24 Oct 2012
Rate this:
Please Sign up or sign in to vote.
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.


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

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

/****** Start: Job step script *****/
-- Table variable to hold intermediate data
      [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
        insert into @ReportSQLErrorLogs
                exec master..xp_readerrorlog @CurrentLogNum
        PRINT @CurrentLogNum
        SELECT @CurrentLogNum = @CurrentLogNum + 1

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. 


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


About the Author

aasim abdullah
Team Leader CureMD
Pakistan Pakistan
Aasim Abdullah is working as SQL Server DBA with CureMD ( 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.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Tinier24-Oct-12 20:45
memberTinier24-Oct-12 20: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 24 Oct 2012
Article Copyright 2012 by aasim abdullah
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid