Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I'm a newbie when it comes to SQL Server, it would be helpful if someone could point me in the right direction.

We have a database file that is called main-db.bak.

This is the database file that we have cloned as it contains all the main things we need in the process of constructing new websites for our clients. Is it possible to do the following:

  1. Have a copy of the database on the same SQL Server Instance
  2. Create a new empty database
  3. Restore the database from main-db.bak file and not overrite anything else that may be using the same data MDF and LDF files?

Or would it be easier to reproduce this database from a live running database copy so that it is not needed to restore if from backup?

Is it mandatory to rename the LDF and MDF files when the database is restored, can I script to automate the majority of the work?

share|improve this question

2 Answers 2

If you only need to create the structure of the database the easiest method is to script out the current database. This will allow you to create a new one, providing a new database name and MDF/LDF file names.

If you need particular static data to deploy as well, depending on how much, you could script the INSERT statements or restore from backup.

In order to restore a copy you will need to restore the database with a new database name and physical filenames (MDF/LDF). You do this using the WITH MOVE clause within the restore command. You can find that reference here on TechNet.

share|improve this answer

Lets take your questions one by one :

Is it possible to do the following:Have a copy of the database on the same SQL Server Instance Yes it is possible. You just have to restore the database main-db.bak with move.

e.g. Provided you dont have secondary data files, below will work

-- find logical names

restore filelistonly from disk = 'path\main-db.bak'
go
restore database main_db_copy
from disk = 'path\main-db.bak'
with recovery, stats = 10,
-- below logical data and log file names will be found from the filelistonly mentioned above.
move 'logicalDataFileName' to 'PhysicalPath\main-db_data.mdf',
move 'logicalLogFileName' to 'PhysicalPath\main-db_log.ldf'

Create a new empty database

No need to first create an empty database, as the restore with move will create it.

Restore the database from main-db.bak file and not overrite anything else that may be using the same data MDF and LDF files?

Same as above. As restore with move will not over write unless you use restore with REPLACE explicitly to overwrite.

Or would it be easier to reproduce this database from a live running database copy so that it is not needed to restore if from backup?

Cannot understand what you mean by this ! If you want to create a new database or copy of the current database , you have to take a backup and then use restore database to restore it.

Is it mandatory to rename the LDF and MDF files when the database is restored, can I script to automate the majority of the work?

If you want to overwrite the existing one, then no. If not and you want to create a copy of existing one, then Yes - you have to rename the mdf and ldf files.

You can fully automate the restore process. Some examples can be found here and here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.