Restore a Database and give the filenames like the database name?
HenrikS
Posts: 8 New member
in SQL Backup
Hi,
i have to restore some databases for development reasons on a regular base.
i dont want to script the "with move" for every database. i search for a generic script. Actual i have:
EXECUTE master..sqlbackup '-SQL "RESTORE DATABASE [Henrik_Test_Ticket_6175_Dest2]
FROM DISK = [D:\MSSQL\MSSQL13.MSSQLSERVER\MSSQL\Backup\FULL_(local)_Henrik_Test_Ticket_6175_*.sqb]
SOURCE = [Henrik_Test_Ticket_6175] LATEST_ALL
with move DATAFiles to [D:\MSSQL\MSSQL13.MSSQLSERVER\MSSQL\DATA\]
,move logfiles to [E:\MSSQL\MSSQL13.MSSQLSERVER\MSSQL\Logs\]
,Replace
"'
But this brings me an error message:
SQL error 3156: File 'Henrik_Test_Ticket_6175_log' cannot be restored to 'E:\MSSQL\MSSQL13.MSSQLSERVER\MSSQL\Logs\Henrik_Test_Ticket_6175_log.ldf'. Use WITH MOVE to identify a valid location for the file.
SQL error 1834: The file 'E:\MSSQL\MSSQL13.MSSQLSERVER\MSSQL\Logs\Henrik_Test_Ticket_6175_log.ldf' cannot be overwritten. It is being used by database 'Henrik_Test_Ticket_6175'.
What can i do, to create or replace a destination database with filenames like:
E:\MSSQL\MSSQL13.MSSQLSERVER\MSSQL\Logs\Henrik_Test_Ticket_6175_Dest2_log.ldf
and
\MSSQL\MSSQL13.MSSQLSERVER\MSSQL\DATA\Henrik_Test_Ticket_6175_Dest2.mdf
i have to restore some databases for development reasons on a regular base.
i dont want to script the "with move" for every database. i search for a generic script. Actual i have:
EXECUTE master..sqlbackup '-SQL "RESTORE DATABASE [Henrik_Test_Ticket_6175_Dest2]
FROM DISK = [D:\MSSQL\MSSQL13.MSSQLSERVER\MSSQL\Backup\FULL_(local)_Henrik_Test_Ticket_6175_*.sqb]
SOURCE = [Henrik_Test_Ticket_6175] LATEST_ALL
with move DATAFiles to [D:\MSSQL\MSSQL13.MSSQLSERVER\MSSQL\DATA\]
,move logfiles to [E:\MSSQL\MSSQL13.MSSQLSERVER\MSSQL\Logs\]
,Replace
"'
But this brings me an error message:
SQL error 3156: File 'Henrik_Test_Ticket_6175_log' cannot be restored to 'E:\MSSQL\MSSQL13.MSSQLSERVER\MSSQL\Logs\Henrik_Test_Ticket_6175_log.ldf'. Use WITH MOVE to identify a valid location for the file.
SQL error 1834: The file 'E:\MSSQL\MSSQL13.MSSQLSERVER\MSSQL\Logs\Henrik_Test_Ticket_6175_log.ldf' cannot be overwritten. It is being used by database 'Henrik_Test_Ticket_6175'.
What can i do, to create or replace a destination database with filenames like:
E:\MSSQL\MSSQL13.MSSQLSERVER\MSSQL\Logs\Henrik_Test_Ticket_6175_Dest2_log.ldf
and
\MSSQL\MSSQL13.MSSQLSERVER\MSSQL\DATA\Henrik_Test_Ticket_6175_Dest2.mdf
Tagged:
Answers
The filegroup paths are handled by first calling SQL Backup with `RESTORE HEADERLISTONLY`, which will list the files without doing an actual restore. We use the result of this command to craft a `RESTORE WITH MOVE` script.
Unfortunately the code is difficult to post here because it's baked into the C# test framework, with loads of special cases for escaping filenames, multipart backups, SQB versus native, Linux versus Windows, network flakiness, caching layers and so on.
I'd be really interested to know if anyone has found a succinct way to do it. Here's a quick stab I made at doing it in SQL:
Redgate Software