Automated Restore Script Output for Ola Hallengren’s Maintenance Solution

Intro

This blog post is designed to be useful to two different audiences:

  1. Those implementing Ola Hallengren’s Maintenance Solution for the first time
  2. Those that have been using it for awhile, but might have a hole in their DR plan – what to restore when a resource goes offline

There’s numerous blogs and presentations out there about Ola’s Maintenance Solution. I don’t feel a strong need to try to replicate that information here. What I am presenting is a custom extension that works out of the box for those of you that have created jobs using the maintenance solution. The scripts will add an additional job step to all DatabaseBackup jobs which will output a continuously updated text file that contains the most current restore scripts for each database in the instance.

Solution

Let’s just get to it then, shall we?

You can download the latest version of my scripts here: https://github.com/jzagelbaum/OlaHallengrenRestoreScript

Please note that there are three requirements for the solution to function correctly:

  1. The DatabaseBackup jobs must use @LogToTable = ‘Y’
  2. In order for the generated restore script to be valid, at least one full database backup should be logged to the CommandLog Table
  3. SQL Server agent should have read / write access to the directory where the restore scripts will be written. The default is the @Directory value.

If you don’t know what @LogToTable, CommandLog, and @Directory are, then please read up on Maintenance Solution before continuing.

Implementation

RestoreCommand Stored Procedure

Use the same database that the Maintenance Solution objects are created in; the default is Master. First create the RestoreCommand s proc. This is the procedure that will be executed each time the DatabaseBackup jobs are run. There are no input parameters for this procedure. It reads the records from CommandLog and outputs the restore commands for each database being backed up on the instance. This includes log, diff, and full based on last completed backup and backup type. You can create the procedure and execute it to view the output as a sample:

RestoreCommandOutput

Please, pleaseplease do not just create this procedure without implementing the job script step. The whole point of this solution is to have a standby restore script available if your instance / database becomes unusable (and you’re not clustering, etc.). I’ve really tried to make it as easy as possible to add the job step (assuming you’re using the OOTB maintenance solution created jobs). That being said…

Create Restore Script Job Steps

This script accepts two parameter values:

  1. @DatabaseName, the database where the Maintenance Solution and RestoreCommand objects are located (default of Master)
  2. @RestoreScriptDir, accepts ‘Backup_Dir’, ‘Error_Log’, or custom defined dir, e.g., ‘C:\’. If the dir is a custom value, the directory must be created prior to running the job.

For the @RestoreScriptDir parameter, the default value ‘Backup_Dir’ places the script in the instance level folder in the directory of the backup files, which is determined by the value of the @Directory parameter. ‘Error_Log’ places the restore script in the same dir as the default location of Maintenance Solution output files.

Make sure SQL Agent has read / write access to any custom directory if used.

After the script is run, you now have an additional job step in all of your DatabaseBackup jobs:

RestoreScriptStep RestoreScriptStepProp

RestoreScriptStepPropAdv

Result

Now, each time a DatabaseBackup job is run, a continuously updated text file containing the latest restore script is written to the appropriate instance folder of the configured directory. Here is a screenshot using the default backup directory:

RestoreCommandJobResult

RestoreCommandJobResultScript

 Parting Thoughts

So, you now have the power to restore your instance at the database level up to the last verified, completed backup. No GUI, no guesswork, and most importantly, no trying to figure out what needs to be restored when you have the least amount of time to think about it.

Hope this helps!

Advertisements

24 thoughts on “Automated Restore Script Output for Ola Hallengren’s Maintenance Solution

  1. Thanks for this solution! I am testing it out now on a server that uses differential backups. My current solution for generating a restore script doesn’t have the option for diff’s.

    Like

  2. Thanks for this, Jared. The script doesn’t seem to account for multiple backup files and only will give a restore command for the first of n files.

    Like

    • Thanks, John. I haven’t had to account for multiple files before. There’s other supported Ola configs that I’m sure I’m missing as well. Please feel free to extend the script to meet your needs.

      Like

  3. Friendly edit to the “Create Restore Scripts Job Step.sql” file, to handle both updating the @RestoreScriptDirValue and for adding these steps to already-modified versions of Ola Hallengren’s scripts (for example, if you already have a step that runs after Ola’s backup job step):

    /*Scott Doubleday-Stern 2015-11-04
    Replaces:
    EXEC msdb.dbo.sp_update_jobstep
    @job_id=@jobID
    ,@step_id = 1
    ,@on_success_action=3
    ,@on_fail_action=2

    EXEC msdb.dbo.sp_add_jobstep
    @job_id = @jobid
    ,@step_name=N’Generate Restore Script’
    ,@step_id=2
    ,@cmdexec_success_code=0
    ,@on_success_action=1
    ,@on_fail_action=2
    ,@retry_attempts=0
    ,@retry_interval=0
    ,@os_run_priority=0
    ,@subsystem=N’CmdExec’
    ,@command=@RestoreCommand
    ,@database_name=@DatabaseName
    ,@output_file_name=@RestoreScriptDirValue
    ,@flags=0
    */
    DECLARE @StepID_GenerateRestoreScript INT = -9
    DECLARE @StepID_Max INT = 1
    DECLARE @StepID_Next INT = 2

    select @StepID_GenerateRestoreScript =
    MAX(CASE WHEN step_name like ‘Generate Restore Script’
    THEN step_id
    ELSE -9
    END
    )
    , @StepID_Max = MAX(step_id)
    , @StepID_Next = MAX(step_id) + 1
    FROM msdb.dbo.sysjobsteps
    where job_id = @jobID

    /*If the step does not exist, change the control flow of the last
    step and add the Generate Restore Script step after it.*/
    IF (@StepID_GenerateRestoreScript = -9)
    BEGIN

    EXEC msdb.dbo.sp_update_jobstep
    @job_id=@jobID
    ,@step_id = @StepID_Max
    ,@on_success_action=3
    ,@on_fail_action=2

    EXEC msdb.dbo.sp_add_jobstep
    @job_id = @jobid
    ,@step_name=N’Generate Restore Script’
    ,@step_id= @StepID_Next
    ,@cmdexec_success_code=0
    ,@on_success_action=1
    ,@on_fail_action=2
    ,@retry_attempts=0
    ,@retry_interval=0
    ,@os_run_priority=0
    ,@subsystem=N’CmdExec’
    ,@command=@RestoreCommand
    ,@database_name=@DatabaseName
    ,@output_file_name=@RestoreScriptDirValue
    ,@flags=0
    END
    /*If the Generate Restore Script step DOES exist, we are
    updating it (possibly with new @RestoreScriptDirValue) without
    touching the rest of the job.*/
    ELSE
    BEGIN
    EXEC msdb.dbo.sp_update_jobstep
    @job_id = @jobid
    ,@step_name=N’Generate Restore Script’
    ,@step_id= @StepID_GenerateRestoreScript
    ,@cmdexec_success_code=0
    ,@on_success_action=1
    ,@on_fail_action=2
    ,@retry_attempts=0
    ,@retry_interval=0
    ,@os_run_priority=0
    ,@subsystem=N’CmdExec’
    ,@command=@RestoreCommand
    ,@database_name=@DatabaseName
    ,@output_file_name=@RestoreScriptDirValue
    ,@flags=0
    END

    Like

  4. Hi Jared,

    Quick question. How would you recommend I use your restore script to automatically execute the restore command generated via your restorecommand stored procedure? I would like to use this in conjunction with Ola’s scripts to do a nightly restore to a testing server. Would you simply execute the command that is scripted in your stored procedure and run it through Ola’s CommandExecute stored procedure?

    Thanks,

    Michael

    Like

    • Hi Michael, sorry for the delay in getting back to you; crazy busy! If you want to schedule the restore to a remote server, you could save the restore script off as .sql instead of .txt and execute the script from a sqlcmd job step. See this link: https://msdn.microsoft.com/en-us/library/ms170572.aspx
      Place the backup files and script in a shared directory.
      My question back to you would be, perhaps you’d rather consider log shipping for something like this, as the design of the solution is really meant to be a DR scenario.

      Like

  5. Hi Jarred,

    thanks for sharing the script,
    but how does it works when you run the logs on secondary replica and Full and diff on primary?
    As there are two different commanlog table how we can get the restore script that contains everythink

    Like

    • Hi There. As Ola scripts are designed to log locally, there isn’t a supported way I know of to log to a dedicated admin instance. Perhaps use a synonym on each instance? This introduces a potential single point of failure and additional licensing if done with HA in mind (which I think would be the point). Otherwise, I would guide you to run all backups (including logs) on the primary if performant enough to avoid the issue of splitting out the command log records. Diverting read workloads to a secondary instance if not already in place might help offset putting log backups on the primary instance.

      Like

  6. I believe the problem with SQLLOGDIR is that the SQL Agent token (SQLLOGDIR) does not exist in versions prior to SQL Server 2014.

    Like

    • Tim, you nailed it. At least that was my problem. I had scripted the job from a 2014 instance, and installed on 2012 instance. Once I fixed the Output File value on the Advanced tab of the Job Step, I was able to execute job without issue.

      Like

  7. When I do FULL backups, I create 4 files. The current RestoreCommand scripts was only restoring the first file. My friendly modification to count for multiple files also.

    with completed_ola_backups as
    (
    SELECT [ID]
    ,[DatabaseName]
    ,[SchemaName]
    ,[ObjectName]
    ,[ObjectType]
    ,[IndexName]
    ,[IndexType]
    ,[StatisticsName]
    ,[PartitionNumber]
    ,[ExtendedInfo]
    ,[Command]
    ,[CommandType]
    ,[StartTime]
    ,[EndTime]
    ,[ErrorNumber]
    ,[ErrorMessage]
    ,CASE WHEN [Command] LIKE ‘%\LOG\%’ THEN ‘Log’
    WHEN @IncludeCopyOnly = ‘Y’ AND [Command] LIKE ‘%\LOG_COPY_ONLY\%’ THEN ‘Log’
    WHEN [Command] LIKE ‘%\DIFF\%’ THEN ‘Diff’
    WHEN [Command] LIKE ‘%\FULL\%’ THEN ‘Full’
    WHEN @IncludeCopyOnly = ‘Y’ AND [Command] LIKE ‘%\FULL_COPY_ONLY\%’ THEN ‘Full’
    End BackupType
    ,CASE WHEN [Command] LIKE ‘%\LOG\%’ THEN 3
    WHEN @IncludeCopyOnly = ‘Y’ AND [Command] LIKE ‘%\LOG_COPY_ONLY\%’ THEN 3
    WHEN [Command] LIKE ‘%\DIFF\%’ THEN 2
    WHEN [Command] LIKE ‘%\FULL\%’ THEN 1
    WHEN @IncludeCopyOnly = ‘Y’ AND [Command] LIKE ‘%\FULL_COPY_ONLY\%’ THEN 1
    End BackupTypeOrder
    ,CASE CommandType
    WHEN ‘BACKUP_LOG’
    THEN CHARINDEX(‘.trn’, Command)
    WHEN ‘BACKUP_DATABASE’
    –First Modification
    –THEN CHARINDEX(‘.bak’, Command)
    THEN CHARINDEX(‘WITH’, Command) –have WITH COMPRESSION
    END filechar
    FROM [dbo].[CommandLog]
    WHERE CommandType IN (‘BACKUP_LOG’, ‘BACKUP_DATABASE’)
    AND EndTime IS NOT NULL — Completed Backups Only
    AND ErrorNumber = 0
    )
    ,lastfull as
    (
    SELECT MAX( [id]) FullId
    ,DatabaseName
    FROM completed_ola_backups
    WHERE BackupType = ‘Full’
    GROUP BY DatabaseName
    )
    ,lastdiff as
    (
    SELECT MAX( [id]) DiffId
    ,cob.DatabaseName
    FROM completed_ola_backups cob
    INNER JOIN lastfull lf
    ON cob.DatabaseName = lf.DatabaseName
    AND cob.[ID] > lf.FullId
    WHERE BackupType = ‘Diff’
    GROUP BY cob.DatabaseName
    )
    ,lastnonlog as
    (
    SELECT Max([Id]) LogIdBoundary
    ,DatabaseName
    FROM
    (
    SELECT Fullid Id, DatabaseName
    FROM lastfull
    UNION ALL
    SELECT DiffId Id, ld.DatabaseName
    FROM lastdiff ld
    ) Nonlog
    GROUP BY DatabaseName
    )
    ,lastlogs as
    (
    SELECT cob.[Id] logid
    FROM completed_ola_backups cob
    INNER JOIN lastnonlog lnl
    ON cob.DatabaseName = lnl.DatabaseName
    AND cob.[ID] > lnl.LogIdBoundary
    )
    ,validbackups as
    (
    SELECT FullId backupid
    FROM lastfull
    UNION
    SELECT DiffId backupid
    FROM lastdiff
    UNION
    SELECT logid backupid
    FROM lastlogs
    )

    SELECT cob.DatabaseName
    ,
    Replace(
    Replace(
    Replace(
    LEFT(Command, filechar + 3)
    , ‘BACKUP LOG’, ‘RESTORE LOG’)
    , ‘BACKUP DATABASE’, ‘RESTORE DATABASE’)
    –Second Modification
    — , ‘TO DISK’, ‘FROM DISK’) + ”’ WITH NORECOVERY’
    , ‘TO DISK’, ‘FROM DISK’) + ‘ NORECOVERY’ –IF you have Multiple FULL backup Files
    + CASE BackupType
    WHEN ‘Full’
    THEN ‘, REPLACE;’
    ELSE ‘;’
    END RestoreCommand

    FROM completed_ola_backups cob
    WHERE EXISTS
    (SELECT *
    FROM validbackups vb
    WHERE cob.[ID] = vb.backupid
    )
    ORDER BY cob.DatabaseName, Id, BackupTypeOrder
    ;

    Like

  8. Thank you for the solution.
    Chris, thanks to you too, I was able to use the modified script for multiple files and generate the restore script.

    Like

  9. The script does it job when run through windows powershell window. However the same script does not work from SQL Agent job

    Note: Added “-recurse”

    $erroractionpreference = “Stop”
    $computer = gc env:computername
    Get-ChildItem -path Microsoft.PowerShell.Core\FileSystem::F:\SQLBackups\RestoreScripts\$computer_DatabaseRestore*.txt | where {$_.Lastwritetime -lt (date).addhours(-168)} | remove-item -recurse

    Error:
    Message
    Unable to start execution of step 1 (reason: line(1): Syntax error). The step failed.
    What could be the reason to fail from SQL Agent?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s