Have you ever had to move databases and their files? A database migration involves some downtime and mistakes made can certainly ruin your day. You don’t want to be in the middle of a migration and be uncertain about what to do.
Read on to learn how to move SQL Server database files and also see some demos.
Why Move Database Files
Why would we need / want to move database files? There are some scenarios where a migration is done.
Disk is out of space
Sometimes when an admin is sleeping on the job full disks go unnoticed until it it too late.
In these cases some databases can be moved to a different disk, volume, mount point, etc.
Moving to new storage
New storage for the database comes in and we need to move some (or all) databases need to be moved.
Separate database files
Following best practices to separate database files – data file (mdf), transaction log (ldf), tempdb, backups, trace files, etc.
I wrote about this here and more specifically here under the section called “Disk Setup”.
Pre Move Actions
The database will be unavailable during this operation so we need to notify our end users. Consider the ramifications if an application is using the database – we might want to stop application services or take some other custom action during the move.
Plan ahead before starting the job. Know what you are going to do before doing it. If you can test your method against a lab or development database that will help too.
Methods to Move Database Files
Once we have the game plan we can start to implement it. Let’s look at some details.
Migration – General Ways to Move Database Files
There are a few methods we can employ to get this job done:
Detach Database / Move Files / Attach Database
Set Database Offline / Move Files / Alter DATABASE MODIFY FILE / Set Online
Backup / Restore Elsewhere
Here are some Microsoft KBs to help along the process.
Now we do some preliminary things before moving files. We need to specify which databases we want to move. This is a very simplistic way to do it – hard coded in a temp table.
--DBs are gone now! that is what detach does and how it is different from setting offline
Now comes the time for moving the database files. You can use your favorite technique for moving files. I like to use robocopy in a .bat file but there are many others such as PowerShell, XCopy, or right click cut / paste.
The “/L” flag is how I first run things. It won’t actually copy anything but rather will list the files. It is a good pre-check before running a batch. Just remove the “/L” flag and run once verified.
Here is the first part of output from running a .bat file with robocopy statements:
Once the files are physically moved to the new location we can continue.
Our files used to be pointing to C: but are now on G: – the migration is complete.
Method 2: Set Database Offline / Move Files / Alter Database / Set Online
Continuing along, let’s move the 3 example databases we created and just moved back from the G: to C: drives. Here we start by defining our databases and setting a new path.
At this point we are free to detach the databases and dispose of the database files. It might be good to hold onto the files until after we have verified the restore worked. Since this is a contrived example let’s move on to the restore.
Plug in the new path for the MOVE TO clause and the restore will put the files there.
Post Move Actions
After migrating some databases we want to be confident that all is well and the system is up and running. A few simple checks help put your stakeholders at ease.
It is good to do the following things after migrating database files:
Check sys.database_files to make sure they are pointed to the right place
Run DBCC CHECKDB on the moved databases
Moving database files can introduce concerns that a quick consistency check can alleviate
Final Thoughts
There are pros and cons to each of the methods presented above to move database files. Make sure that whatever technique you choose to employ that you first test it on a non-production server. This is not the point you want to encounter unknown surprises.
The safest method is probably #2 – setting offline, move files, alter modify file, set online. Detaching runs the risk of allowing the files to be deleted but is that is what you want then detach is the way. The backup method is probably best kept for the warm / cold standby but could work depending on the system usage and constraints.
[…] we need to loop through all the user databases and take some action. For example – moving database files or migrating databases. The basic logic can be used to loop through a list of databases on a SQL instance and perform some […]
[…] Jeff Mlakar gives us three methods for migrating database files from one location to another: […]
[…] we need to loop through all the user databases and take some action. For example – moving database files or migrating databases. The basic logic can be used to loop through a list of databases on a SQL instance and perform some […]