DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account - Jon Galloway

DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

I've found the most reliable method of  moving SQL Server databases between servers is a simple backup / restore. The restore is a bit of a pain in the neck, though - it takes some unnecessary clicky clicky in the SQL Server Enterprise Manager (ugh).

Here's a simple DOS batch script that does away with most of the repetitive work. I put a copy of this in a \Project\Database directory, fill in the DBNAME variable, and from then on the restore is as simple as copying the SQL Backup file over and running the batch file. The granting permission to the ASPNET user thing is important if you're using trusted connections, which is a durn good idea. Barry Dorrans has some great info on that here.  

::SET VARIABLES
set DBNAME=NAMEOFDATABASEBEINGRESTORED
set DBDIRECTORY=C:\Program Files\Microsoft SQL Server\MSSQL\Data

TITLE Restoring 
%DBNAMEDatabase

::PUT DATABASE IN SINGLE USER MODE TO ALLOW RESTORE
osql -E -d master -Q "alter database %DBNAME% set single_user with rollback immediate"

::RESTORE DATABASE
osql --d master -Q "restore database %DBNAMEfrom disk='%~dp0\%DBNAME%.bak' WITH MOVE '%DBNAME%_Data' TO '%DBDIRECTORY%\%DBNAME%_Data.MDF' MOVE '%DBNAME%_Log' TO '%DBDIRECTORY%\%DBNAME%_Log.LDF'"

::GRANT PERMISSION TO ASPNET USER
osql --%DBNAME% -Q "sp_grantdbaccess '%COMPUTERNAME%\ASPNET'"
osql 
--%DBNAME% -Q "sp_addrolemember 'db_owner''%COMPUTERNAME%\ASPNET'"

::RESTORE TO MULTI USER
osql -E -d master -Q "alter database %DBNAME% set multi_user"

pause

Usage notes:

  1. Replace DBNAME variable with the actual database name.
  2. restore database osql command should all be on one line; it's just formatted as is to look pretty. (fixed)
  3. This assumes you're using some defaults - for instance, you're saving your SQL database backup as DBNAME.bak, your database file names are DBNAME_Data.MDF and DBNAME_Log.LDF, and your DBDIRECTORY is the default directory. If this isn't the case, it's pretty easy to change the script to suit your bizarre tastes.
  4. This is built to be run in the same directory as the backup file. If that's not the case, replace the %~dp0 silliness with the path to the backup file.
  5. You're running this on a server that can stand a SQL Server restart. I use this on my development server, so that's just fine. (fixed, see update note below)

UPDATE: Original version restarted SQL Server before performing the restore. Based on Sean's comment, I've changed this to use single user mode instead.

Published Friday, December 02, 2005 7:09 AM by Jon Galloway
Filed under: ,

Comments

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Instead of stopping the server, it's less intrusive to boot out the users by setting the database into SingleUser mode.

-- kill all users, and rollback any pending transactions
USE Master
go
alter database <dbName> set single_user with rollback immediate
GO

-- Put it back
alter database <dbName>set multi_user

Friday, December 02, 2005 11:42 AM by Sean

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Looks good. I always cut/paste a sql script to do this, so I'm glad you took the time to write a DOS file. This will come in handy for porting from dev to test on automated builds. Nice job....

Saturday, December 03, 2005 12:40 AM by Sean

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

I'm just a windows server system admin and trying to use this script. I modified the script to match my database location. I get this error,

C:\>osql -E -d master -Q "restore database mms_prod from disk='c:\mms_prod\mms_p

rod.bak' WITH MOVE 'mms_prod_data' TO 'c:\mms_prod\mms_prod_data.mdf',  MOVE 'mm

s_prod_log' TO 'c:\mms_prod\mms_prod_log.ldf'"

Msg 3234, Level 16, State 2, Server USA-APPSERVER, Line 1

Logical file 'mms_prod_data' is not part of database 'mms_prod'. Use RESTORE

FILELISTONLY to list the logical file names.

Msg 3013, Level 16, State 1, Server USA-APPSERVER, Line 1

RESTORE DATABASE is terminating abnormally.

What could be the problem?

Thanks

Ganesh.

Thursday, August 17, 2006 12:53 PM by Ganesh

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

It sounds like the logical name for the data file is incorrect. You can find out what that is in Enterprise Manager or SSMS by right clicking the database and checking the file name.

I would guess that it's mms_prod instead of mms_prod_data. You could try replacing that and seeing if it works.

Thursday, August 17, 2006 4:26 PM by Jon Galloway

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

You can use the RESTORE FILELISTONLY in your generic database restore script as follows:

SET FLOCMD=osql -E -Q restore filelistonly from disk='%FILE%'" -w 888

SET LPRI=

SET LLOG=

For /f "skip=2 delims=/" %%i in ('%FLOCMD%') do @CAll :GetNames "%%i"

Then do your restore with move '%LPRI%' to '%MDF%', move '%LLOG%' to '%LDF%', replace

And the :GetNam sub slooks like:

:Getnam

SET L1=%~1

If !==!%LPRI% set LPRI=%L1:~1,80%& goto :Eof

If !==!%LLOG% set LLOG=%L1:~1,80%& goto :Eof

Goto :Eof

The skip=2 skips teh first two lines from the output, the w=888 prevents too many lines, and the delms=/ is necessary because the LPRI and LLOG names may contain spaces.

Wednesday, November 15, 2006 7:32 PM by John

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

can you modify this to work with a named sql server instance please.

Thursday, February 15, 2007 2:56 AM by Saeed

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Absolutely GREAT find. Thank you for this site and thanks to the poster of the DOS logic. Exactly what I needed...

Thursday, July 19, 2007 10:11 PM by Phil

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

nice piece of script, saved me lot of time. couldnt thank you enough. :)

Thursday, July 26, 2007 9:28 AM by uttam

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Really Good Job. I made some adds for SQLEXPRESS (and to replace the database if exists). Cheers.

::SET VARIABLES

set DBNAME=DBNAME

set DBDIRECTORY=%PROGRAMFILES%\Microsoft SQL Server\MSSQL.1\MSSQL\Data

TITLE Restoring %DBNAME% Database

::PUT DATABASE IN SINGLE USER MODE TO ALLOW RESTORE

osql -S localhost\SQLEXPRESS -E -d master -Q "alter database %DBNAME% set single_user with rollback immediate"

::RESTORE DATABASE

osql -S localhost\SQLEXPRESS -E -d master -Q "restore database %DBNAME% from disk='%~dp0\%DBNAME%.bak' WITH MOVE '%DBNAME%' TO '%DBDIRECTORY%\%DBNAME%.MDF',  MOVE '%DBNAME%_Log' TO '%DBDIRECTORY%\%DBNAME%_Log.LDF', REPLACE "

::GRANT PERMISSION TO ASPNET USER

osql -S localhost\SQLEXPRESS -E -d %DBNAME% -Q "sp_grantdbaccess '%COMPUTERNAME%\ASPNET'"

osql -S localhost\SQLEXPRESS -E -d %DBNAME% -Q "sp_addrolemember 'db_owner', '%COMPUTERNAME%\ASPNET'"

::RESTORE TO MULTI USER

osql -S localhost\SQLEXPRESS -E -d master -Q "alter database %DBNAME% set multi_user"

PAUSE

exit

Wednesday, August 08, 2007 11:54 AM by Pablo Heredia

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Awesome script, thanks!

Thursday, August 23, 2007 1:23 PM by Daniel Flippance

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

how  can I combime the restore filelistonly of the dos restore script?

Tuesday, March 04, 2008 1:29 AM by Jeboy

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Thanks, this entry helped me write a systematic script to backup, copy, and restore a number of databases from an old, to-be-retired server to the new device.

Saturday, March 29, 2008 8:27 AM by Fred

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

When I try to put the master Database in Single-User Mode I get the message:

Option 'SINGLE_USER' cannot be set in database 'MASTER'

How can I RESTORE the MASTER Database with a Batch script?

Wednesday, May 21, 2008 10:01 AM by André Fampa

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

To be awkward I'd like to restore a database backed up from database1 over one called database2 and the restored over one still be called database2.

Is that possible?

Friday, May 23, 2008 8:10 AM by RR

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Thanks for your post! - This came in real handy for keeping my production db copied to another backup server for running reports/dr purposes.

Friday, October 10, 2008 7:25 PM by Slade Hornick

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Great examples.  Thank you.  

Monday, November 03, 2008 8:19 AM by Tony

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Hi,

I need to be able to do the following:

1) put a database in single user mode

2) restore the LATEST backup from a directory which contains many .bak's

3) put database back into multi users mode

Can anyone help please. Thanks.

Monday, April 20, 2009 4:37 AM by John

# how to backup and restore sql 2005 data base with bat file - RarityGuide.com Forums

Pingback from  how to backup and restore sql 2005 data base with bat file - RarityGuide.com Forums

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Nice script.

Just what i needed to backup / restore SQL2000 DB from one server to another.

Regards

Sko

Wednesday, March 23, 2011 5:19 PM by Heine Sko

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

@John :Microsoft and OSQL: "This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use the feature. Use sqlcmd instead. For more information, see sqlcmd Utility." (msdn.microsoft.com/.../ms162806%28v=sql.90%29.aspx)

But sqlcmd has an other result using  -w 888

Tuesday, January 22, 2013 12:02 PM by hafi

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Fantastic beat ! I would like to apprentice whilst you amend

your web site, how can i subscribe for a weblog website?

The account helped me a applicable deal. I were a little bit acquainted of this your broadcast provided vibrant transparent idea

Thursday, March 21, 2013 10:05 AM by Gore

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Excellent write-up. I absolutely appreciate this website.

Keep it up!

Wednesday, March 27, 2013 4:00 AM by Cheek

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

It's a pity you don't have a donate button! I'd definitely donate to this excellent blog! I suppose for now i'll settle for bookmarking

and adding your RSS feed to my Google account.

I look forward to fresh updates and will share this blog with my Facebook group.

Talk soon!

Wednesday, April 03, 2013 1:18 PM by Carlisle

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Attractive section of content. I just stumbled upon your website and in accession capital

to assert that I acquire in fact enjoyed account your

blog posts. Any way I'll be subscribing to your feeds and even I achievement you access consistently rapidly.

Wednesday, April 17, 2013 6:37 PM by Herrin

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Hi there mates, how is all, and what you wish for to say concerning this post,

in my view its actually amazing in favor of me.

Thursday, April 18, 2013 7:25 AM by Vogt

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

I'm really enjoying the design and layout of your blog. It's

a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often.

Did you hire out a designer to create your theme? Exceptional work!

Thursday, April 18, 2013 8:02 AM by Peak

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Hey there, You've done a fantastic job. I'll definitely digg it and personally recommend to my friends.

I'm sure they'll be benefited from this site.

Friday, April 19, 2013 3:01 AM by Goode

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Hello to all, how is all, I think every one is getting more from this web site, and your views are

good for new users.

Friday, April 19, 2013 1:50 PM by Gannon

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Hi to all, how is everything, I think every one

is getting more from this website, and your views are fastidious

for new visitors.

Friday, April 19, 2013 3:27 PM by Forsythe

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

This is my first time visit at here and i am in fact

pleassant to read all at one place.

Friday, April 19, 2013 4:46 PM by Leslie

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

I like the valuable information you provide in your articles.

I'll bookmark your blog and check again here regularly. I'm quite sure I'll learn lots of new stuff right here! Best of luck for the next!

Saturday, April 20, 2013 3:07 AM by Oakley

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Aw, this was an extremely good post. Taking the time and actual effort

to make a really good article… but what can I say… I procrastinate

a whole lot and never manage to get anything done.

Saturday, April 20, 2013 3:53 AM by Guay

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Aw, this was an extremely good post. Taking the time and actual effort

to make a really good article… but what can I say… I procrastinate

a whole lot and never manage to get anything done.

Saturday, April 20, 2013 5:11 AM by Guay

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Magnificent web site. Plenty of useful info here. I'm sending it to a few buddies ans also sharing in delicious. And certainly, thanks to your sweat!

Sunday, April 21, 2013 4:17 PM by Merriman

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

I'm amazed, I must say. Seldom do I encounter a blog that's both educative and entertaining, and without a doubt, you

have hit the nail on the head. The problem is something too few people are speaking intelligently about.

I am very happy that I found this in my search for something concerning this.

Thursday, May 09, 2013 3:27 AM by David

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

What's Going down i am new to this, I stumbled upon this I've discovered

It absolutely helpful and it has aided me out loads.

I'm hoping to contribute & aid other customers like its aided me. Great job.

Thursday, May 09, 2013 4:10 PM by Francis

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Couldn’t agree additional! at your succeeder

Thursday, May 09, 2013 6:46 PM by Babin

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

you are actually a just right webmaster. The site loading pace is incredible.

It kind of feels that you are doing any unique trick.

Moreover, The contents are masterwork. you've done a excellent job on this subject!

Friday, May 10, 2013 3:06 AM by Garber

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Incredible! This blog looks just like my old one!

It's on a entirely different subject but it has pretty much the same page layout and design. Great choice of colors!

Friday, May 10, 2013 3:18 AM by Trammell

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Do you have a spam issue on this website; I also am

a blogger, and I was wondering your situation; many

of us have developed some nice practices and we are looking to swap strategies with

others, be sure to shoot me an e-mail if interested.

Monday, May 13, 2013 6:33 PM by Trombley

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Excellent blog right here! Also your web site rather a lot up very fast!

What host are you the use of? Can I am getting your associate link on

your host? I desire my web site loaded up as quickly as yours

lol

Tuesday, May 14, 2013 1:51 AM by Hedgepeth

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

What a stuff of un-ambiguity and preserveness of precious familiarity on the

topic of unpredicted emotions.

Tuesday, May 14, 2013 5:19 PM by Bartels

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

My brother recommended I might like this blog. He was once entirely right.

This submit truly made my day. You cann't consider just how so much time I had spent for this information! Thanks!

Tuesday, May 14, 2013 6:44 PM by Mccullough

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Hi to every one, it's truly a pleasant for me to pay a visit this web page, it contains precious Information.

Tuesday, May 14, 2013 10:18 PM by Dillard

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

What's up to every one, the contents existing at this web site are really remarkable for people knowledge, well, keep up the nice work fellows.

Wednesday, May 15, 2013 1:39 AM by Dwyer

# re: DOS batch script to restore a SQL 2000 database and grant permission to ASPNET account

Wonderful goods from you, man. I have understand your stuff prior to and you

are just too fantastic. I really like what you have received here, really like what you're stating and the way by which you are saying it. You make it entertaining and you still care for to keep it smart. I can not wait to learn far more from you. This is actually a wonderful website.

Saturday, May 18, 2013 7:57 AM by Rickman

Leave a Comment

(required) 
(required) 
(optional)
(required)