T-SQL Restore Script Generator

  • Comments posted to this topic are about the item T-SQL Restore Script Generator

  • Thanks for sharing your script, Paul!

    I do have a question, though. In SSMS, when I bring up the restore dialog, I can select a point in time and then have SSMS generate the restore statements for me. What are the benefits of using your script over using SSMS to generate them?

    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • The restore dialogue in SSMS has a GUI front end and works for one database at a time. sp_RestoreScriptGenie can build a restore script for all databases in an instance (handy if you have 50 or 60) and can be scheduled as a SQL agent job to, for example, automate development environment refreshes, reinstate log shipping, mirroring, alwayson availability groups and so on.

    The functionality available in the ssms restore dialogue and sp_RestoreScriptGenie and very similar (I hope to make it identical by adding file group and page restores) but the circumstances you might use each in are different.

    Thanks for the question and best wishes


  • Thanks for the quick and enlightening reply, Paul!

    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Thanks for a great question.

  • Very interesting - though I would have to note that this has the very common limitation that it won't function when trying to restore specific databases to Server B after Server A is completely unavailable (and other similar situations), since there won't be an msdb with valid history.

    Likewise, I would be concerned about cases (SQL2005, primarily) where msdb has incorrect path information, i.e. http://connect.microsoft.com/SQLServer/feedback/details/306604/physical-device-name-not-captured-correctly-in-msdb-dbo-backupmediafamily.

    Question: did you test it with empty transaction log backups (i.e. LastLSN = FirstLSN) - for instance, a server set to do very frequent T-log backups that, for whatever reason, had no activity during that time. I usually generate those test cases with once-a-minute T-log backups on a development server with absolutely 0 activity.

  • At work here we use the Ola Hallengran scripts to run out backups to a dedicated backup server. I have added a final step to the user database and log backup jobs to run sp_RestoreScriptGenie and, basically, save the T-SQL RESTORE script in the same directory as the backups. If disaster strikes we have the recovery scripts saved in the same directory as the backup files.

    Regarding LSN's, the checks were added later after I saw the '31 days of disaster recover' series by SQLSoldier. They are basically his checks appended to the procedure. It hasn't been a problem for me during many executions and (I think) worst case scenario is you would need to comment out one of the transaction log restores if it were a problem. It has been tested with empty log file backups though and they are not a problem.

    Regarding the incorrect paths, you can override data/log/backup file locations and in the restore commands it builds and it shouldn't matter. I wasn't aware of the SQL Server 2005 connect item to be honest although the procedure has been tested with SQL Server 2005 <-> SQL Server 2012.

    Thanks and best wishes

  • Hi - thanks for the script, it's great.

    I have one (probably rookie) question: It appears to only select from msdb.dbo.backupmediafamily where device_type = 2. We have some backups where the device type = 102 (still disc) Was there any reason for that?



  • Hi Tim,

    The WHERE [Device_Type] = 2

    could probably be

    WHERE [Device_Type] <> 7

    All our backups at work are device type 2, same at home in my virtual lab at home. The idea was to ignore Symatec backups or anything that uses volume shadow copy. (I was surprised these even show up in the backup history but they do). I'll take your comments onboard in any new versions of the script.

    Many thanks for the feedback and best wishes, pleased you like it.


  • understood - thanks.

  • HI Paul, this is Awesome , thanks for sharing your knowledge with SQL community much appreciated .

    is your second version on this topic ready ? if so can you please provide a link.



  • Thanks Kiran,

    This is the latest version of the procedure - https://paulbrewer.wordpress.com/sp_restoregene/

    It includes support for backup forking and various other mod's.

    Best wishes


  • Thanks Paul, this is great Stuff 🙂

  • Hi,

    I realise this post is very old but it could be just what ive spent the last 5 days trying to write.

    I think my issue is, on my TESTSERVER, I created a database TestRestoredDatabase. I want to be able to restore a backup from our data domain eg: \\ddomain1\score\full\score_17022020.bak ;  \\ddomain1\score\Logs\score_17022020_trn , move the files to M:\SQLData\2016 and the Logs to L:\SQLLogs\2016

    AND restore to a point in time. I don't want to overwrite any existing databases

    Darryl Wilson

  • Hi Darryl,

    The latest version of the script is here - sp_RestoreGene – Paul Brewer (wordpress.com)

    You should be able to achieve this restore objective using the @TargetDatabase parameter (to avoid overwriting), @WithMoveDataFiles and @WithMoveLogFile and @StopAt parameters.

    Best regards


Viewing 15 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic. Login to reply