Solved

RESTORE DATABASE with MOVE TO option

Posted on 2009-06-30
6
799 Views
Last Modified: 2012-05-07
I have a problem with restoring databases from backups.  I'll try to explain.

The situation is that I will get a BAK file (full backup) performed programatically with the following T-SQL statement
BACKUP DATABASE [dbNameGoesHere] TO DISK = 'somepath\BackupFile.bak'
The file name (BackupFile.bak) is invariant however the name of the database is not necessarily known to me nor is the original file path of the backup or database.

I want to be able to restore to a specific location on my system. eg.
RESTORE DATABASE [foo] FROM DISK = 'x:\BackupFile.BAK' MOVE xxx_Data TO 'D:\foo\mydata.mdf', MOVE xxx_Log To 'D:\foo\mydata_log.ldf'

The problem is that I don't know what the logical names for the data and the log are inside the .bak file.
How do I find that information OR is there a way around without requiring the logical names?

In a nutshell I would like the T-SQL statement to achieve what I want.
0
Comment
Question by:AndyAinscow
  • 3
  • 3
6 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
run this to get the logical file info

restore filelistonly FROM DISK = 'x:\BackupFile.BAK'
0
 
LVL 44

Author Comment

by:AndyAinscow
Comment Utility
OK, but then what ?  
Do you mean this will do the job?

restore filelistonly FROM DISK = 'x:\BackupFile.BAK' ;  RESTORE DATABASE [foo] FROM DISK = 'x:\BackupFile.BAK' MOVE xxx_Data TO 'D:\foo\mydata.mdf', MOVE xxx_Log To 'D:\foo\mydata_log.ldf'
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
once you run the filelist only, you will get the contents of the backup file including the logical file name
 then you can use that logicall name to modify your 'WITH MOVE ' statement
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
Comment Utility
How to restore a database with a new name (Transact-SQL)
To restore a database with a new name

Optionally, execute the RESTORE FILELISTONLY statement to determine the number and names of the files in the database backup.


Execute the RESTORE DATABASE statement to restore the database backup, specifying:
The new name for the database.


The backup device from where the database backup will be restored.


The NORECOVERY clause if you have transaction log backups to apply after the file backups are restored. Otherwise, specify the RECOVERY clause.
The transaction log backups, if applied, must cover the time when the files were backed up.

The MOVE clause for each file to restore to a new location if the file names already exist. For example, creating a copy of an existing database on the same server for testing purposes may be necessary. In this case, the database files for the original database already exist, and so different file names need to be specified when the database copy is created during the restore operation.
Examples
This example creates a new database called MyNwind2_Test. MyNwind2_Test is a copy of the existing MyNwind2 database that comprises two files: MyNwind2_data and MyNwind2_log. Because the MyNwind2 database already exists, the files in the backup need to be moved during the restore operation. The RESTORE FILELISTONLY statement is used to determine the number and names of the files in the database being restored.

USE master
GO
-- First determine the number and names of the files in the backup.
-- MyNwind_2 is the name of the backup device.
RESTORE FILELISTONLY
   FROM MyNwind_2
-- Restore the files for MyNwind2_Test.
RESTORE DATABASE MyNwind2_Test
   FROM MyNwind_2
   WITH RECOVERY,
   MOVE 'MyNwind2_data' TO 'D:\MyData\MyNwind2_Test_data.mdf',
   MOVE 'MyNwind2_log' TO 'D:\MyData\MyNwind2_Test_log.ldf'
GO
0
 
LVL 44

Author Comment

by:AndyAinscow
Comment Utility
Thanks.
I'll give that a try tomorrow (late evening here now) and get back to you with the results.
0
 
LVL 44

Author Comment

by:AndyAinscow
Comment Utility
I've not been able to get the thing to run with that as one SQL statement.  :-(

However I have used the FILELISTONLY part to build a recordset and in code get the contents of the first field (logical name) in the returned set.  I can then use this to build the T-SQL command without the FILELISTONLY part.  That does work, maybe not quite as efficiently but it works.  Mission accomplished.

Thanks for your help.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

772 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

10 Experts available now in Live!

Get 1:1 Help Now