Sql-server – Restore Database SQL Server 2012

sql serversql-server-2012

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?

Best 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.