Darren Ferguon - Weblog

Wednesday, October 01, 2008

Umbraco tip: Quick SQL server 2005 backup/restore

Filed under: Umbraco - by Darren Ferguson @ 12:44 PM

This only works if you are using a version of SQL server and have access to a command prompt on your server. I was looking for a way to source control DB backups and quickly copy my database between development, QA and production environments.

Start with the following in a file called backup.sql - obviously change the database name.

use master
   go
   sp_detach_db 'databasename'
   go

In the same folder create a file backup2.sql - again the database name and paths to your SQL server install are key.

use master
  go
  sp_attach_db 'databasename',
'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\databasename.mdf',
'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\databasename_log.ldf'
  go

Next create a file called backup.bat - In this file replace the target of the copy with a path of your choice. I am just copying the files to my SVN repository for a later commit.

sqlcmd -ic:backup.sql

copy "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\databasename*.*"
C:\repositories\df_com\trunk\db

sqlcmd -ic:backup2.sql

For sqlcmd to execute, you will need to have the SQL server bin directory in your PATH. You may also want to stop IIS prior to the backup and start it again after using net stop|start w3svc.

Now you have your SQL server data file detached and copied to a new location. To restore simply create restore.bat.

sqlcmd -ic:backup.sql

copy C:\repositories\df_com\trunk\db\databasename*.* 
"C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\"

sqlcmd -ic:backup2.sql

Exactly the same process but copying the data files in the opposite direction.

So why is this useful? Well, so long as your database names and SQL server paths are the same across servers you can simply copy your data files between servers and restore the database in any environment.

This all came about as part of a larger project to put umbraco under source control which I'll write a better article on at some point later on. .

6 comment(s) for Umbraco tip: Quick SQL server 2005 backup/restoreComments RSS

  1. Jay Greasley says:

    Jay Greasleyuseful tip, I also look forward with anticipation to the source control post...

  2. Wouter Boevink says:

    Wouter BoevinkAnd why is this better than creating a backup and restoring it? That way you can also copy your data between servers. And it can also be done from the command prompt. And your server stays only.

  3. Darren Ferguson says:

    Darren Ferguson@wouter: I assume you are talking about using the 'backup database' command. I've had numerous issues moving db's across machines with this.

    I guess you mean server 'stays online'?

  4. David Conlisk says:

    David ConliskDarren - works a treat! Been using subversion with unfuddle.com for my own projects for a while now after our conversations at the level two course and I spend a bit of time manually attaching/detaching the db on each commit/update. This saves me doing that each time and also ensures that I have versioned backups of my database just like my code.
    I wonder is there a way of creating a start-of-day and end-of-day wonder script that will include this step and also kick off your svn checkout or update...
    Thanks for the tip!

    David

  5. Darren Ferguson says:

    Darren FergusonHi David, No reason why not.

    Have a look at:

    http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-automation.html

    I presume you are using Tortoise SVN??

    My next step was to set this up on this blog so my DB is backed up every night.

  6. Darren Ferguson says:

    Darren Ferguson@wouter: I had a play around using 'backup database' and got it working. I'll do another post on how to use it as there are a couple of things you need to know!

Leave a comment