RESTORE DATABASE with MOVE TO option
Posted on 2009-06-30
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.