Solved

Automated DB Restore for SQL Server 2008 R2

Posted on 2013-01-22
5
307 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach 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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

932 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

13 Experts available now in Live!

Get 1:1 Help Now