restoring original MDF file from bak file

Ali_dotNet picture Ali_dotNet · Jul 29, 2013 · Viewed 19.6k times · Source

I've got a bak file (which is a backup database file for a SQL server express 2005 mdf file) and I should obtain the MDF file so that I can work on its tables, how can I get the original MDF file from this bak file? I'm using Visual Studio 2012, is it necessary to have management studio? I've tried to restore this bak file to an empty database in another system containing Sql server express management studio 2008, but it says databases don't match, what is going wrong?

Answer

Milena Petrovic picture Milena Petrovic · Jul 30, 2013

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 it You 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 FILELISTONLY
FROM 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 YourDB
FROM DISK = 'E:\Test\TestFull.bak'
WITH MOVE 'test1' TO 'E:\test\TestMDFFile.mdf',
MOVE 'test1_log' TO 'E:\test\TestLDFFile.ldf'