Sql-server – Restore Database with Multiple Restore commands

sql serversql-server-2008sql-server-2008-r2t-sql

For automation, I have to dynamically construct the restore commands and thus I have created a script. A .bak file is created on a remote server and I receive it daily. The script builds the restore string and executes it.

Only hindrance so far is I'm unable to restore the db on multiple steps, because the back up file comes from an external source and fails with "Directory lookup for the file "xxxxxx.ndf" failed with the operating system error 3 (The system cannot find the path specified.)." It is the folder where the backup resides on the remote server.

Basically the script looks like:

RESTORE DATABASE x FROM DISK = 'E:x.bak'  WITH MOVE 'a' TO 'E:\x.mdf', NORECOVERY    
RESTORE DATABASE x FROM DISK = 'E:x.bak'  WITH MOVE 'b' TO 'E:\x1.ndf', NORECOVERY
RESTORE LOG x      FROM DISK = 'E:\x.bak' WITH MOVE 'c_log' TO 'E:\x.ldf',NORECOVERY
RESTORE DATABASE x WITH RECOVERY

How can I achieve such a scenario?

Best Answer

The following code will restore the database to the desired location:

RESTORE DATABASE x
FROM DISK = 'Filepathofbackupfile.bak'
WITH RECOVERY,
MOVE 'logicalDatafilename' TO 'Newfilepath.mdf',
MOVE 'logicalLogfilename' TO 'Newfilepath.ldf'