July 24, 2014 at 2:45 am
I do a restore with move, as suggested above, and rather than setting to single user mode, I set to restricted_user mode;
use master
go
alter database destinationdbname
set restricted_user with rollback immediate
(then do the restore with replace, move)
July 24, 2014 at 12:52 pm
Also, you can use the sp_who2 proc to see IF another user has a connection to the DB.
You can either kill xxx (where xxx is the SPID of the user connected to the DB) OR I use the following script and un-comment the ALTER DATABASE statement to place the DB in single user mode to kick out any users then do the restore (you will have to fill in the correct paths/names etc...):
USE master
RESTORE FILELISTONLY
FROM DISK = '' --Disk Backup File
--sp_helpdb TEST
USE master
--ALTER DATABASE [TEST] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE [TEST] -- DB Name
FROM DISK = '' --Disk Backup File
WITH STATS, REPLACE,
MOVE '' TO '.mdf',
MOVE '' TO '.ldf'
July 25, 2014 at 2:56 am
This is why you are getting the error
RESTORE DATABASE TEST FROM DISK = 'E:\<path>\FULL.BAK'
WITH REPLACE, RECOVERY,
MOVE 'PROD' TO 'E:\<path>\TEST.MDF';
You've only specified a move for the primary data file, you need to specify a move command for each log file and secondary data file too. If you do not specify a file it will try to use the source databases file and that will produce an error.
Don't set anything offline or single user or anything else, just use the restore command and specify a move command for each file in the database. If you want to get a list of the files run this first
RESTORE FILELISTONLY FROM DISK = 'E:\<path>\FULL.BAK'
-----------------------------------------------------------------------------------------------------------
"Ya can't make an omelette without breaking just a few eggs" 😉
July 25, 2014 at 6:29 am
Perry Whittle (7/25/2014)
This is why you are getting the error
RESTORE DATABASE TEST FROM DISK = 'E:\<path>\FULL.BAK'
WITH REPLACE, RECOVERY,
MOVE 'PROD' TO 'E:\<path>\TEST.MDF';
You've only specified a move for the primary data file, you need to specify a move command for each log file and secondary data file too. If you do not specify a file it will try to use the source databases file and that will produce an error.
Don't set anything offline or single user or anything else, just use the restore command and specify a move command for each file in the database. If you want to get a list of the files run this first
RESTORE FILELISTONLY FROM DISK = 'E:\<path>\FULL.BAK'
And i can do this during business hours without affecting the Prod DB?
July 25, 2014 at 6:41 am
Also, do i use the logical name or physical name?
July 25, 2014 at 6:50 am
Paul Morris-1011726 (7/25/2014)
And i can do this during business hours without affecting the Prod DB?
Should be perfectly safe as long as you set the move clauses correctly. The logical name is used to refer to the file within the database. For each logical filename you move the path and\or physical name.
Make sense?
-----------------------------------------------------------------------------------------------------------
"Ya can't make an omelette without breaking just a few eggs" 😉
July 25, 2014 at 6:55 am
So it should look like this?
RESTORE DATABASE TEST FROM DISK = 'E:\<path>\FULL.BAK'
WITH REPLACE, RECOVERY,
MOVE 'PROD' TO 'E:\<path>\TEST.MDF',
MOVE 'PROD_log' TO 'E:\<path>\TEST_log.LDF';
Do i need the RECOVERY?
July 25, 2014 at 6:58 am
Paul Morris-1011726 (7/25/2014)
So it should look like this?
RESTORE DATABASE TEST FROM DISK = 'E:\<path>\FULL.BAK'
WITH REPLACE, RECOVERY,
MOVE 'PROD' TO 'E:\<path>\TEST.MDF',
MOVE 'PROD_log' TO 'E:\<path>\TEST_log.LDF';
Do i need the RECOVERY?
You don't need REPLACE since the database doesn't exist, you don't need RECOVERY since the default action is to recover the database.
-----------------------------------------------------------------------------------------------------------
"Ya can't make an omelette without breaking just a few eggs" 😉
July 25, 2014 at 7:06 am
So this is what it should look like:
RESTORE DATABASE TEST FROM DISK = 'E:\<path>\FULL.BAK'
WITH
MOVE 'PROD' TO 'E:\<path>\TEST.MDF',
MOVE 'PROD_log' TO 'E:\<path>\TEST_log.LDF';
Sorry for asking a lot of questions, never created test DB's this way.
In the past, I would detach production, make a copy, rename, and reattached. Now I am learning that is not a good way of doing it.
July 25, 2014 at 7:12 am
I have an example here which should help.
I have a Litespeed database called Litespeedlocal which i have a backup of, I want to restore it as bob.
The logical filenames are
LiteSpeedLocal
LiteSpeedLocal_log
I use the following query
RESTORE DATABASE [BOB] FROM DISK = N'P:\Backups\LiteSpeedLocal\LiteSpeed.BAK'
WITH MOVE 'LiteSpeedLocal' TO 'F:\MSSQL11.MSSQLSERVER\MSSQL\DATA\bobsmith_data.mdf',
MOVE 'LiteSpeedLocal_log' TO 'F:\MSSQL11.MSSQLSERVER\MSSQL\Logs\bobsmith_log.ldf'
-----------------------------------------------------------------------------------------------------------
"Ya can't make an omelette without breaking just a few eggs" 😉
July 25, 2014 at 7:16 am
Once you restored the database as a new name i would also then change the logical filenames as shown below (and based on my example)
ALTER DATABASE [bob] MODIFY FILE (NAME = LiteSpeedLocal, NEWNAME = Bob_Data)
ALTER DATABASE [bob] MODIFY FILE (NAME = LiteSpeedLocal_log, NEWNAME = Bob_log)[/code]
-----------------------------------------------------------------------------------------------------------
"Ya can't make an omelette without breaking just a few eggs" 😉
July 25, 2014 at 7:19 am
Very cool, I will do this off hours just because i am paranoid. Let you know how it goes. Thanks for the help!
July 25, 2014 at 7:24 am
As long as you do not have the "REPLACE" parm in the restore command any current DB can not be overwritten as you will get an error message.
July 25, 2014 at 9:52 am
Paul Morris-1011726 (7/25/2014)
Very cool, I will do this off hours just because i am paranoid. Let you know how it goes. Thanks for the help!
no need to be paranoid, just ensure your script is correct before you run it.
Ensure there is a MOVE clause for every file returned in the RESTORE FILELISTONLY output 😎
-----------------------------------------------------------------------------------------------------------
"Ya can't make an omelette without breaking just a few eggs" 😉
August 11, 2014 at 8:58 am
Just wanted to follow up to let you know that it worked fine... thanks for all the info/help.
Viewing 15 posts - 16 through 30 (of 30 total)
You must be logged in to reply to this topic. Login to reply