tags:

views:

56

answers:

7

I need to write a tool to clone a database, instead of detaching it and copying the .mdf and .ldf, wouldn't it be better to just back it up and restore a newly created db?

Is there a way using SQL to create a database from a .bak?

+1  A: 

May be it will be something like this:

RESTORE DATABASE [NewDB] 
    FROM  DISK = N'Path\file.bak' 
    WITH  FILE = 1,  
    MOVE N'NewDB_Data' TO N'C:\Data\MSSQL.1\MSSQL\Data\NewDB_Data.mdf',  
    MOVE N'NewDB_Log' TO N'C:\Data\MSSQL.1\MSSQL\Data\NewDB_Log.ldf',  
    NOUNLOAD,  STATS = 10
Pavel Belousov
+1  A: 

Easiest way if you are new to this I would say is to

a) Create a database in SQL Management Studio but instead of clicking "OK" to create it, script out the action as an SQL script and run it.

b) Restore the .bak file in management studio making sure you get the backup file locations pointing in the correct place and again script out the action instead of clicking OK.

c) Merge two files into 1 to give you a "clone database" sql script.

Justin Wignall
+4  A: 

Yes, you can use backup as a method of cloning a database. For this, you can simply use the RESTORE command.

For example:

RESTORE DATABASE    DatabaseName
FROM DISK = N'C:\Path\To\Your\File.bak'

For further reference about parameters for the RESTORE command, have a look at the MSDN reference for it: Click!

Maximilian Mayerl
You'll still need to create the clone database and move the files, not sure how this is more helpful than the other responses here...
Justin Wignall
You dopn't need to create anything, RESTORE DATABASE will create the database for you if it doesn't exist.
Maximilian Mayerl
Msg 3234, Level 16, State 2, Line 2Logical file 'clonebak' is not part of database 'clonebak'. Use RESTORE FILELISTONLY to list the logical file names.I need to restore to a db with a different name is there a flag I need to have?
Jreeter
+1  A: 

Use c#.net to achieve functionality you want here is line of the article which talks about creating application to back and restore database : http://www.geekpedia.com/tutorial180_Backup-and-restore-SQL-databases.html

Pranay Rana
+1  A: 

We use SQL Server 2005 and are continually copying and swapping databases for testing purposes. We ALWAYS now use backup and restore. Detaching, reattaching .mdf and .ldf files has very frequently caused SQL Server to become corrupt to the point of requiring re-installation, and a very ugly cleanup before that is even possible. As several others have suggested, using a .bak file is pretty straightforward.

mickeyf
+2  A: 

You can do this with backup as already answered. No one has mentioned yet though that it is important to use the COPY ONLY option when doing these ad hoc backups to avoid mullering your main backups

BACKUP DATABASE YourDB
TO DISK = 'C:\Backups\YourDB.bak'
WITH COPY_ONLY; 
Martin Smith
+3  A: 

To extend on Maximilian Mayerl's answer, I would recommend using MSBuild and the ExecuteDDL task of the MSBuild Community Tasks library to automate this process.

You would start with a SQL script like this one (perhaps called CloneDb.sql):

USE master
GO

RESTORE DATABASE dbname
   FROM DISK = 'SOURCEDIR\dbname.bak'
   WITH REPLACE, FILE = 1,  
    MOVE N'dbname' TO N'DBDEVICEDIR\dbname.mdf',  
    MOVE N'dbname_log' TO N'DBDEVICEDIR\dbname_log.LDF',  
    NOUNLOAD,  
    STATS = 10
GO

In the MSBuild script, you'd create a target that includes a sequence like this:

<FileUpdate Files="$(BuildDir)\CloneDb.sql"
                Regex="SOURCEDIR"
                ReplacementText="$(SqlSafeActualBuildDir)\dbdeploy" />
    <FileUpdate Files="$(BuildDir)\CloneDb.sql"
                Regex="DBDEVICEDIR"
                ReplacementText="$(SqlSafeActualBuildDir)\dbdevices" />
    <ExecuteDDL Files="$(BuildDir)\CloneDb.sql" ConnectionString="Server=$(LocalDbServer);Database=master;Trusted_Connection=True;" />

With this in place, running "msbuild.exe CloneDb.proj /t:" from a Visual Studio command-line will clone your database in one step. You can put the command in a batch file for convenience.

I created a more elaborate version of this for my current project, where a team of over a dozen developers uses it to create local versions of the databases our project uses for their own development needs.

Scott A. Lawrence
Msg 3234, Level 16, State 2, Line 2 Logical file 'clonebak' is not part of database 'clonebak'. Use RESTORE FILELISTONLY to list the logical file names. I need to restore to a db with a different name is there a flag I need to have?
Jreeter
nevermind got it... duh
Jreeter