Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

restoring original MDF file from bak file

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?

like image 908
Ali_dotNet Avatar asked Jul 29 '13 06:07

Ali_dotNet


2 Answers

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'
like image 112
Milena Petrovic Avatar answered Sep 28 '22 16:09

Milena Petrovic


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)

like image 20
steoleary Avatar answered Sep 28 '22 17:09

steoleary