T-SQL Script to Roll Forward Transaction Log Backups
Let's roll, baby!! Ahem... what? it was funny in my head. shut up. [photo credit: MichaelGaida]

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 have 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.

 Parameters

The script uses 4 parameters:

  • @TransactionLogBackupFolder: The folder path where all the transaction log backups are located. You must end this path with a backslash ( \ ).
  • @FileNameQualifier: This is a file name qualifier which will be used in a LIKE clause in order to filter the files which you want to restore. This is useful if there are backup files of more than one database in the same folder, or if you want to make SQL’s job easier by filtering which files to restore (for example: ‘MyDB_backup_2015_08_%.trn’ will only try to restore from the backups made in August 2015). If you don’t want to use any filter, please specify ‘%’.
  • @DatabaseName: This is the database name which should be restored. Needless to say, since we’re restoring transaction log backups here, the database must be in NORECOVERY or STANDBY mode before you begin.
  • @PerformRecovery: If you want the script to finalize the restore process by bringing the database back online WITH RECOVERY, then specify “1” for this parameter. It’s recommended to leave this parameter as “0” at least at first, to make sure all the backups you need were properly restored.

 The Script

You can download the full script from my GitHub Gist here: https://gist.github.com/EitanBlumin/a037a82df730bf19af014ca03d0a11a4

Or from my TechNet Gallery here: https://gallery.technet.microsoft.com/scriptcenter/T-SQL-Script-to-Roll-61ac3415

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

Guy Glantser

CEO & Founder, Madeira Data Solutions

6 年

Cool, thanks!

要查看或添加评论,请登录

Eitan Blumin的更多文章

社区洞察

其他会员也浏览了