Solved

Automated DB Restore for SQL Server 2008 R2

Posted on 2013-01-22
5
306 Views
Last Modified: 2013-01-23
We have a production system on Server1 running a SQL Server 2008 R2 database. Currently we have a maintenance plan to do full backups of selected databases nightly. We have a test server, Server2, which is also running SQL Server 2008 R2. We'd like some sort of script or way to copy the fresh backup from Server1 to Server2 then do a full overwrite of the database on Server2. This way each day Server2 has a restoration of Server1.

Any changes to Server2 DB would be overwritten that night with the new Server1 database.

I'm having challenges in being able to figure out how to pick the correct backup file name, copy it to Server2 and then restore it. I can do all of this manually, however that's a hassle.

Anyone have a script or a method to assist this? Would be greatly appreciated as I've beat my head against a wall for the past two days trying to figure it out without much results.

Thanks!
0
Comment
Question by:dobbshelp
  • 3
  • 2
5 Comments
 
LVL 26

Accepted Solution

by:
Chris Luttrell earned 500 total points
ID: 38808210
To find the latest backup file name for your database try this:
SELECT msdb.dbo.backupset.name 
FROM msdb..backupset
WHERE msdb.dbo.backupset.backup_set_id = (select max(backup_set_id) from msdb..backupset where database_name=N'YourDatabaseName')

Open in new window


The restore scripts are pretty easy once you get the file to a known location on the destination server, but getting it copied over is tricky and will depend on what tools and permissions you have on each server and the network.  I always have to play the hand that is dealt when it comes to that.
The restore scripts look something like this:
ALTER DATABASE DatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

RESTORE DATABASE [DatabaseName] FROM  DISK = N'C:\Temp\DatabaseName_backup_2013_01_13_203002_3003367.bak' WITH  FILE = 1,  
MOVE N'DatabaseName' TO N'M:\Sql_Data\DatabaseName1.mdf',   -- If Needed
MOVE N'DatabaseName_log' TO N'L:\Sql_TLogs\DatabaseName1_1.ldf',    -- If Needed
NOUNLOAD,  REPLACE,  STATS = 10

ALTER DATABASE [DatabaseName] MODIFY FILE (NAME=N'DatabaseName', NEWNAME=N'DatabaseName')  -- If Needed

ALTER DATABASE [DatabaseName] MODIFY FILE (NAME=N'DatabaseName_log', NEWNAME=N'DatabaseName_log')  -- If Needed


ALTER DATABASE DatabaseName SET MULTI_USER;

Open in new window

0
 
LVL 3

Author Comment

by:dobbshelp
ID: 38811157
CGLuttrell

Thanks for the response. Your first script to select the backup job name is returning results (well a single result), however the filename is from a week ago. I double checked there are backups for the past 3 days for sure that have been written by a maintenance plan. SO I'm not sure why it's returning results for a week ago rather than last nights backup.

As for the restore script, By manually copying over the backup file and changing the name manually within the script, it appears to working correctly. Thank you!
0
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 38811418
That script is based on what MS does when you tell it to Verify the backup you just ran.  It grabs the latest one to know which file to run the "RESTORE VERIFYONLY..." on.  Everytime you run a full backup (unless you tell it to do a Copy Only Backup) it adds an entry in that table with the next Identity value.  Is it possible the backups since last week are either Transaction Log, incremental or Copy Only backups?
0
 
LVL 3

Author Comment

by:dobbshelp
ID: 38812054
CGLuttrell

Got all of this working correctly. I must have been running the script initially on the wrong server or something. It's working so far.

Thanks for all of the help and quick responses!
0
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 38812061
Your welcome, glad to have helped.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now