Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Quick way to move a bunch of databases? Expand / Collapse
Author
Message
Posted Thursday, September 13, 2012 6:08 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, May 10, 2013 9:54 AM
Points: 283, Visits: 610
I'm looking to move the database file location from one drive to another on my machine for about 20-30 databases.

Besides a copy/paste of the files from location A to location B and then creating all the databases again and deleting the existing ones, is there any other option?

The file names and database names would be identical, just the path
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLEXP\MSSQL\DATA
becomes
R:\SQL Monthly Snapshots

I'm guessing I might have to edit the permissions of that folder too to allow Management Studio to write to it?
Post #1358500
Posted Thursday, September 13, 2012 6:34 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:58 PM
Points: 5,204, Visits: 11,161
Rob-350472 (9/13/2012)
I'm looking to move the database file location from one drive to another on my machine for about 20-30 databases.

Besides a copy/paste of the files from location A to location B and then creating all the databases again and deleting the existing ones, is there any other option?

The file names and database names would be identical, just the path
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLEXP\MSSQL\DATA
becomes
R:\SQL Monthly Snapshots

I'm guessing I might have to edit the permissions of that folder too to allow Management Studio to write to it?


Something like this for all user databases

SELECT 'ALTER DATABASE ' + QUOTENAME(DB_NAME(database_id)) + 
' MODIFY FILE(NAME=' + name + ', FILENAME=N''' +
REPLACE(physical_name,
'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLEXP\MSSQL\DATA\',
'R:\SQL Monthly Snapshots\') + ''')'
FROM sys.master_files
WHERE database_id > 4



-----------------------------------------------------------------------------------------------------------

"Ya can't make an omelette without breaking just a few eggs"
Post #1358513
Posted Thursday, September 13, 2012 7:52 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, May 10, 2013 9:54 AM
Points: 283, Visits: 610
Ahh, I see, that makes sense perfectly after delving into that data and seeing what's going on. I can try it with a couple of databases by changing the database_ID and see what happens.

Need to wait till I can actually close Management Studio in the first place (and SQLserver process) to move the files first of all though!

Thanks :)
Post #1358567
Posted Thursday, September 13, 2012 8:41 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:58 PM
Points: 5,204, Visits: 11,161
Rob-350472 (9/13/2012)
Need to wait till I can actually close Management Studio in the first place (and SQLserver process) to move the files first of all though!

Thanks :)

No, you don't

Issue the alter database commands and then take the databases offline. Copy the files to the new locations and then bring the databases online again. When they come online delete the old files from the source directory


-----------------------------------------------------------------------------------------------------------

"Ya can't make an omelette without breaking just a few eggs"
Post #1358619
Posted Thursday, September 13, 2012 8:46 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, May 10, 2013 9:54 AM
Points: 283, Visits: 610
Ah I see, run the command, right click on DB in question, tasks --> take offline.

Copy files in explorer from source to destination

right click --> 'bring online' (at a guess!)

Confirm it works then delete the source files.

Even better!
Post #1358621
Posted Thursday, September 13, 2012 8:57 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 8:44 AM
Points: 4,434, Visits: 7,218
I would recommend backing up the databases first, in case the worst happens, which it has done for me before. Given that you have taken a backup, you may want to consider doing the move by RESTOREing WITH MOVE, instead of OFFLINE -> ALTER -> ONLINE. This approach may be quicker if you have large database files with lots of empty space.

John
Post #1358630
Posted Thursday, September 13, 2012 9:01 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, May 10, 2013 9:54 AM
Points: 283, Visits: 610
Thanks John, I'm currently going through the databases and shrinking them actually. These are monthly archives which are dipped into from time to time and prove very useful. They're not used in production or anything (otherwise I'd not bother shrinking them).

I was just thinking actually that I should get a decent backup routine sorted for these archives, ideally creating a .bak file and compressing then deleting hte .bak file. 7zip perhaps, if not RAR.

The shrink may buy me some more time before I have to move, perhaps I should think of going:

shrink --> backup routine --> move...
Post #1358634
Posted Thursday, September 13, 2012 9:14 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:58 PM
Points: 5,204, Visits: 11,161
Rob-350472 (9/13/2012)
Ah I see, run the command, right click on DB in question, tasks --> take offline.

Copy files in explorer from source to destination

right click --> 'bring online' (at a guess!)

Confirm it works then delete the source files.

Even better!



-----------------------------------------------------------------------------------------------------------

"Ya can't make an omelette without breaking just a few eggs"
Post #1358646
Posted Thursday, September 13, 2012 9:16 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:58 PM
Points: 5,204, Visits: 11,161
Rob-350472 (9/13/2012)
perhaps I should think of going:

shrink --> backup routine --> move...

no, use the method i showed you. backup and restore is a complete waste of time.
I use this method all the time even when moving system databases, which by the way is the supported way of moving them.


-----------------------------------------------------------------------------------------------------------

"Ya can't make an omelette without breaking just a few eggs"
Post #1358648
Posted Thursday, September 13, 2012 9:18 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:58 PM
Points: 5,204, Visits: 11,161
John Mitchell-245523 (9/13/2012)
I would recommend backing up the databases first, in case the worst happens, which it has done for me before. Given that you have taken a backup, you may want to consider doing the move by RESTOREing WITH MOVE,

Are you serious? Backup and restore to move file paths for databases on the same instance!


Actually its

ALTER -> OFFLINE -> COPY files -> ONLINE


-----------------------------------------------------------------------------------------------------------

"Ya can't make an omelette without breaking just a few eggs"
Post #1358649
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse