Site icon Eitan Blumin's blog

T-SQL Script to Roll Forward Transaction Log Backups

Performing a transaction log backup every 15 minutes has its advantages. But it also has disadvantages. Ever tried to restore hundreds of transaction log backups one by one in order to bring your database back from disaster? Not a bit of fun that is. Check out this useful script that I prepared to help sort this problem out.

This script is especially useful for those “Maintenance Plan” backups where each backup is in a separate file, all located in a single folder, and has uniform file names that represent their date and time of backup.

The script uses XP_CMDSHELL in order to get a listing of all the files in the folder. Take serious note of this, because having the XP_CMDSHELL procedure enabled is a huge security risk! It’s highly recommended to disable XP_CMDSHELL again once you’re done with the script.

EDIT 2022-02-23: I finally replaced the usage of xp_cmdshell with xp_dirtree instead, which is a safe, built-in stored procedure for listing files in a folder.

 Parameters

The script uses 4 parameters:

DECLARE 
   @TransactionLogBackupFolder VARCHAR(4000) = 'C:\SqlDBBackupsMyDB'
 , @FileNameQualifier VARCHAR(4000) = 'MyDB_%.trn'
 , @DatabaseName SYSNAME = 'MyDB'
 , @PerformRecovery BIT = 0

 The Script

The script is available in my GitHub Gists

Or from the Madeira Toolbox on GitHub.

Or from my TechNet Gallery.

This article was originally published by Eitan Blumin in August 2015, at www.madeiradata.com

Exit mobile version