![]() |
|
|
By: Ken Simmons | Read Comments (14) | Print Ken is a database administrator, developer, SQL Server book author and Microsoft SQL Server MVP. Related Tips: 1 | 2 | 3 | 4 | More |
|
Problem
There are a lot of scripts out there that will allow you to use the msdb to auto-generate restore scripts, but what if you cannot access the msdb database. What is the easiest way to make sure I have the proper restore scripts on hand without relying on the msdb database?
Solution
The best way to make sure you have the proper restore scripts when the time comes is to make use of PRINT statements and OUTPUT files. Every time your backup job runs, whether it is a Full or Log, place a step at the end of the job to read the new data from the msdb database and write it to an output file. I generally place the restore script in the root of my backup folders and make sure the script is backed up as well.
First, create a new SQL Agent Job step called "Generate Restore Script" at the end of your backup job and insert the script listed below.

(note: to select all text, triple click in the table below)
SET NOCOUNT ON |

Here is sample of what the output looks like:

So if I want to restore the "test2" database, I would just use the three lines highlighted above.
That's all there is to it. The next time your backup runs it will generate a script in the output file that can be used to restore any of the databases for the instance. Since the script runs when the backup runs, you will always have a current restore script on hand.
Next Steps
| Share: | Share | Tweet |
|
![]() |
|
|
Free SQL Server Learning |
| Friday, November 07, 2008 - 3:11:36 PM - pmk003 | Read The Tip |
|
I like this concept, but I do have a question... Why would you need these scripts instead of using the GUI to restore? When wouldn't you have access to msdb to run your restore - an entire server crash??? But in that case, we store the backups on the box itself and wouldn't have access to the most current backups anyways... What case(s) would there be where the server would be available (such that you could get to the backup files) but that SQL Server msdb wouldn't be available? |
|
| Friday, November 07, 2008 - 3:41:42 PM - ppcx | Read The Tip |
| I'm new to MSSQL so this may be a simple question, but I can't get the generated script to run. If I take each line separately copy/paste into a sqlcmd prompt and type "go" then that line will work. But I can't figure out how to make the whole script run. This seems like it would be a great disaster recovery script if I can figure out how to make it work. | |
| Friday, November 07, 2008 - 7:10:34 PM - KenSimmons | Read The Tip |
|
A lot of times as soon as a backup is complete Full or Transaction log it is copied to some other source. If you have to pull the backups from another source and restore them to another server that is already running this could help. I generally don't rely on the GUI to perform everything. If you had to manually write the script for some reason this would be a big time saver in a disaster scenario. |
|
| Friday, November 07, 2008 - 7:12:34 PM - KenSimmons | Read The Tip |
|
[quote user="ppcx"]I'm new to MSSQL so this may be a simple question, but I can't get the generated script to run. If I take each line separately copy/paste into a sqlcmd prompt and type "go" then that line will work. But I can't figure out how to make the whole script run. This seems like it would be a great disaster recovery script if I can figure out how to make it work.[/quote] What are you having an issue with? Do you get an error message? |
|
| Monday, November 10, 2008 - 7:43:40 AM - ppcx | Read The Tip |
|
When I try to run the script in sqlcmd it seems to want a "go" between each line and doesn't recognize that each line is its own separate command to be run. If I copy/paste the lines for the "master" and "msdb" databases
RESTORE DATABASE master FROM DISK = 'S:\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\master_backup_200811070200.bak' WITH NORECOVERY -- [SQLSTATE 01000] this is what sqlcmd looks like:
S:\Microsoft SQL Server\MSSQL.1\MSSQL\Backup>sqlcmd -S sqlcluster01 -E It doesn't see each line as a command to run and just spools them all up together and then complains that the whole huge line isn't valid. |
|
| Monday, November 10, 2008 - 8:33:48 AM - Dr DBA | Read The Tip |
|
Hello everone, From the discussion here I decided to modify the orginal code with the following 1) Added the GO statements so you can now execute the complete output 2) Added STAT=10 meaning that you will get an indication of the restore as show as 10% increaments 3) No restore scripts generated for any system databases (master, model, msdb) as you need to place the server in single user mode to restore the master and then you should manually restore the msdb and model 4) No restore script generated for tempdb This script works fine with SQL 2000 (x32) and SQL 2005 (x32 and x64) Enjoy
SET NOCOUNT ON
|
|
| Monday, November 10, 2008 - 10:00:09 AM - ppcx | Read The Tip |
|
Ok. Here's a probably dumb question: If I'm building scripts for disaster recovery of the database, even though I would need to recover the master in single-user mode, wouldn't it be nice to have the commands already generated? For example, "model" is not simple-recovery so it has transaction logs. It would be nice to have the commands to apply all of the transaction logs to the database. |
|
| Monday, November 10, 2008 - 1:09:33 PM - KenSimmons | Read The Tip |
|
That depends on your situation. Most of the times you do not have to worry about the model database because it is just used as a model to create new databases, but if you make changes to the model database that get applied to any new database, you should back it up. Generally the main system databases I worry about is master and msdb. You can modify the script to include whatever databases you may need. |
|
| Thursday, November 20, 2008 - 1:19:40 PM - hansontd | Read The Tip |
|
Been using the script for a couple of weeks and it has been working great....listing the last full backup and log backups per the articles description...However I checked the file today and found the following, no changes have been made to the server that I'm aware of. The output below is with the newest version of the code listed in this thread but I was using the original version prior with success and both produced a similar result...
-- Script to Restore the INOW6 Database -- [SQLSTATE 01000] GO [SQLSTATE 01000]RESTORE DATABASE INOW6 FROM DISK = 'HTSQLServerINOW6' WITH NORECOVERY, STATS =10 [SQLSTATE 01000]GO [SQLSTATE 01000] RESTORE LOG INOW6 FROM DISK = 'F:\BACKUPS\INOW6_backup_200811201200.trn' WITH FILE = 1, NORECOVERY, STATS =10 [SQLSTATE 01000]GO [SQLSTATE 01000] RESTORE DATABASE INOW6 WITH RECOVERY, STATS =10 [SQLSTATE 01000]GO [SQLSTATE 01000] I'm just curious as to why the restore database segment points to the HTSQLServerINOW6 versus the F:\ location that the restore log statement points to? Guess I should clarify that both files ARE in the same location. Thanks!
|
|
| Thursday, November 20, 2008 - 6:49:55 PM - KenSimmons | Read The Tip |
|
Just by looking at the results, it looks like HTSQLServerINOW6 may be the name of a backup device. Do you know if anyone may have changed the backup to use a backup device? |
|
| Friday, November 21, 2008 - 7:20:39 AM - hansontd | Read The Tip |
|
Not that I'm aware of....all backups write to the same drive on the server (SAN spaced assigned to the box) here is a snapshot of that: Not sure but it's just strange that it's referencing the log files corrrectly but not the backups.... |
|
| Friday, November 21, 2008 - 8:15:09 AM - KenSimmons | Read The Tip |
|
It is getting the information from the backupmediafamily table. Run the following query and see if you see anything like that in the physical_device_name column. Select * from msdb.dbo.backupmediafamily |
|
| Friday, November 21, 2008 - 8:28:34 AM - hansontd | Read The Tip |
|
There sure is....it has to be this Bridgehead agent that's creating these other backups.... |
|
| Wednesday, July 22, 2009 - 9:10:42 AM - Tripp | Read The Tip |
|
First let me say thank you for another, excellent, time-saving tip. I love this script and have been using it on my SQL 2005 servers without issue for several months now. I'm now trying to use it with three, legacy SQL Server 2000 instances but am getting mixed results. In two of the three SQL 2000 (sp4) environments, I get the following error when the script is parsed: Msg 137, Level 15, State 1, Line 82Must declare the variable '@DatabaseName'.
The point of failure is the last @DatabaseName; the fetch from dbnames_cursor into @DatabaseName. The DatabaseName variable was declared at the beginning of the script and used 9 times by this point. What’s stranger still, is that the exact SQL executes without an error on a third SQL Server 2000 (sp4) database instance. I'm in the process of combing through the configurations of the database servers for any differences, but since these three instances are the database backend for three completely autonomous application environments there will be numerous intentional configuration differences. Any ideas as to the cause of this error?
|
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |