Technical Article

Restore Multiple Backup Logs

,

Use This Script to restore Multiple Log backups from a Log Device file.
The script will restore all the logs on the device and will leave the databse ready to use after finish.
Before running this script you must verify if the database is in No recovery mode or read only state

/* Use This Script to restore Multiple Log backups from a Log Device file.
 Before running this script you must verify if the database is in No recovery mode or read only state.
By Rafael A. Colón, Stericycle Inc.
*/
Declare @NumberOfLogs Int,
             @LogDeviceNumber Int,
             @LogDeviceName  varchar (20),
             @DatabaseName varchar (20)

 
SET  @NumberOfLogs = ''   -- Total number of log backups in the SQL backup device 
SET  @LogDeviceNumber = 1
SET  @LogDeviceName = ''   -- Name of the log backup device
SET  @DatabaseName = ''    -- Database where the log backups will be restored.

-- This loop will restore all the current log backups to the database and will set the database option to recovery after the last log processed.
WHILE  @LogDeviceNumber <  @NumberOfLogs
BEGIN
    PRINT 'Restoring' 
    PRINT @LogDeviceNumber 
    PRINT '===================================================='
   RESTORE LOG  @DatabaseName
    FROM @LogDeviceName
    WITH FILE = @LogDeviceNumber,  
--    STATS,              -- OPTIONAL PARAMETER TO DISPLAY LOG  RESTORE PROGRESS
    NORECOVERY
     IF @LogDeviceNumber > @NumberOflogs
         BREAK
 ELSE
       SET  @LogDeviceNumber = @LogDeviceNumber+1
  END 

PRINT 'This is the last log file to restore'
PRINT '===================================================='
PRINT @LogDeviceNumber 
 RESTORE LOG  @DatabaseName
 FROM @LogDeviceName
WITH FILE = @LogDeviceNumber,  RECOVERY

Rate

5 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (1)

You rated this post out of 5. Change rating