restoring original MDF file from bak file restoring original MDF file from bak file sql-server sql-server

restoring original MDF file from bak file


Keep in mind that restoring a database backup file will not give the original MDF (and LDF) files. The only way to get the original MDF file is to copy itYou can execute the T-SQL suggested by steoleary in Visual Studio, see more here: How to: Run SQL Scripts in Solution Explorer. You can also do that in SQL Server management Studio.

The blank database you created doesn't help much, unless you want to synchronize the backup to it. But for that you would need a 3rd party tool

First, execute the following to find out the logical file names in your backup. This example is for the backup named TestFull.bak stored in E:\Test

RESTORE FILELISTONLYFROM DISK = 'E:\Test\TestFull.bak'GO

enter image description here

The logical names should be used in the next script. Also, update the paths and names used

RESTORE DATABASE YourDBFROM DISK = 'E:\Test\TestFull.bak'WITH MOVE 'test1' TO 'E:\test\TestMDFFile.mdf',MOVE 'test1_log' TO 'E:\test\TestLDFFile.ldf'


If you have created a blank database, to overwrite this with the backup you will need to specify the WITH REPLACE parameter.

Also, you may have to specify the MOVE parameter to put the files into the correct locations.

You should be able to quite easily find these options in the GUI when doing the restore, or alternatively you can script it out by using the reference here:

How to: Restore a Database to a New Location and Name (Transact-SQL)