June 01, 2005 07:01 PM

Use T-SQL to Back Up and Restore SQL Server User Databases

Rating: (0)
SQL Server Magazine
InstantDoc ID #46560

I wrote a couple of T-SQL procedures—sp_backup_databases and sp_restore_databases—you can use to move all your user databases from one Microsoft SQL Server machine to another with ease. The sp_backup_databases procedure performs a complete database backup of all user databases to the specified directory. The sp_restore_databases procedure restores those databases.

Listing 1 shows the sp_backup_databases procedure. It should reside in the master database of the SQL Server machine on which all user databases exist. The sp_backup_databases procedure requires one parameter that specifies the backup destination directory. For example, if you run the command

 exec sp_backup_databases 'c:\sqlbackup' 

the sp_backup_databases procedure backs up all user databases to the C:\sqlbackup directory. The names of the backups will be in the format .bak.

Listing 2 shows the sp_restore_databases procedure. It should reside in the master database of the SQL Server machine on which you want the databases to be restored. The procedure requires two parameters. The first parameter specifies the location in which to restore all database files. If this location is null, the files are restored to the original position listed in the backup header (as shown using restore filelistOnly). The second parameter is the location of backup files. For example, if you run the command

 exec sp_restore_databases 'c:\data','c:\sqlbackup' 

the sp_backup_databases procedure restores the database files in the C:\sqlbackup directory to the C:\data directory.

Editor’s Note
Share your SQL Server discoveries, comments, problems, solutions, and experiences with products and reach out to other SQL Server Magazine readers. Email your contributions (400 words or less) to r2r@sqlmag.com. Please include your phone number. We edit submissions for style, grammar, and length. If we print your submission, you’ll get $50.

Add a Comment

Hi taersious!

I passed your question on to the article's author, Eli Leiba, who said "No, the restore will fail since its on the same server and therefore the 2000 and 2005 files reside on different locations. The procedure works for 2000 or 2005 servers that have the same directory structures and instance names and reside on different servers.

You'll have to use with move clause, use only backup proc, and then restore dbs one by one with restore TSQL statement.

Eli"

Please feel free to contact me directly if you have any more questions or feedback. Thanks for reading SQL Server Magazine!

Megan Keller

Associate Editor, SQL Server Magazine

mkeller@sqlmag.com

Megan11/18/2009 9:42:40 AM


Before I try to use your proc, I need to know more about the inner workings. Can you use this script to take a db backup from an instance of SQL 2000 and restore to an instance of SQL 2005 on the same server? If so, would this render the original (live) database on SQL 2000 unusable? I am looking for a way without detaching the live database to bring a copy online for test in SQL 2005.

Drew11/12/2009 9:13:42 AM


You must log on before posting a comment.

Are you a new visitor? Register Here

Windows Event 333->

We have a VM that has 8GB memory and is running Windows 2003 Enterprise Server 32bit and SQL Server 2000 SP4.Making no changes the system runs fine an...222-96226

GOOGLE LINKS
SPONSORED LINKS
FEATURED LINKS