Art of the DBA Rotating Header Image

The Moving Van

A problem DBAs commonly face is moving database files around, especially when we’re trying to manage space on a drive.  I know that I’ve had to do it and, when you have a database instance with hundreds of databases, you need an automated way to manage this.  Recently, someone on DBA Stackexchange had this specific problem and I was able to provide a Powershell script that I’ve used in the past to manage this problem.  And when I haven’t blogged for a while, a good way back into it is to share a Powershell script.  Mostly because it’s easy.  :)

First, let me talk about a pattern I commonly use.  A lot of times, I could write a single script to take an action, but instead I build a script to write a script.  The main reason I do this is that it’s very hard, especially with many of our administrative tasks, to write a one-size-fits-all process.  So instead, I have a tool that gets me 90% of the way which I can then tweak or alter that script to address that final 10% to fit that specific need.

That’s what this script is doing.  It doesn’t perform the actual file moves itself, but writes out a script that will execute those files moves.  It’s fairly generic, creating a script that moves all the database files from their current location (wherever that may be) to two common directories: data and log. It’s rare that you would move all your database files in this fashion, but you can edit the generated script to suit your specific situation.

This script makes use of the techniques detailed here by Ben Miller(@dbaduck) with one significant exception. I’ve found that the .Offline() method for databases is very unreliable because you’re basically executing your normal “ALTER DATABASE [foo] SET OFFLINE;”. The main problem with this command is it has to wait for all connections to close gracefully before it can proceed, something that doesn’t happen to frequently. I prefer to use “ALTER DATABASE [foo] SET OFFLINE WITH ROLLBACK IMMEDIATE;” because it forces all connections to close and open transactions to rollback, no waiting involved. Since the SMO methods don’t have an option for that,  I call the SQL using the Invoke-SQLCmd cmdlet. I find this is a much cleaner option for my script.

Specifically, the script accepts 4 parameters:

  • $newdata – New data file path

  • $newlog – New log file path

  • $instance – SQL instance you want to move files for (defaults to local)

  • $outputfile – Output file name (defaults to Documents, FileMover.ps1)

When you run it, it will spit out a FileMover.ps1 script to you Documents folder (though, using that last parameter, you can change the output location and name).  This FileMover.ps1 will be the script you can then edit to suit your specific needs.  The script is below, but you can also download it here.

   Builds a Powershell script for moving many database files from one
	 set of directories to another.
   Used to generate a Powershell script for moving many database files
	 from one location to another.  This will typically be used with a
	 large number of databases that need to be relocated.  Caveats for
	 this script include:
	 		-Destination for all files will be the same.
			-User that runs the script must have access to source and destination locations
			-This uses the 2008 R2 version of the SMO.

	The script generates a FileMover.ps1 script (by default in My
	Documents).  The reason for generating a separate script is so
	specific configurations can be edited/updated before actually
	execyting the move.

.PARAMETER <paramName>
   instance - Instance owning the databases to be moved
	 newdata - New data file location, no trailing '\'.  example: "C:\DBFiles\Data"
	 newlog - New log file location, no trailing '\'.  example: "C:\DBFiles\Log"
	 $outputfile - Full path and name of output file.  By default, FileMover.ps1 in My Documents.

   .\Build-FileMover.ps1 -newdata "C:\DBFiles\Data" -newlog "C:\DBFiles\Log"

param([parameter(Mandatory=$true)][string] $newdata,
			[parameter(Mandatory=$true)][string] $newlog,
			[string] $instance="localhost",
      [string] $outputfile=([Environment]::GetFolderPath(&quot;MyDocuments&quot;))+&quot;<code>\FileMover.ps1&quot;)

#load SMO
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null

#Create server object and output filename
$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server $instance

#get your databases

#build initial script components
&quot;Add-PSSnapin SqlServerCmdletSnapin100&quot; &gt; $outputfile
&quot;Add-PSSnapin SqlServerProviderSnapin100&quot; &gt;&gt; $outputfile
&quot;[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') </code>&quot;$instance<code>&quot; | out-null&quot; &gt;&gt; $outputfile
&quot;</code>$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server &quot; &gt;&gt; $outputfile

foreach($db_build in $db_list)
	#only process user databases
		#script out all the file moves
		&quot;#----------------------------------------------------------------------&quot; &gt;&gt; $outputfile
		&quot;<code>$db=</code>$server.Databases[<code>&quot;&quot;+$db_build.Name+&quot;</code>&quot;]&quot; &gt;&gt; $outputfile

		$dbchange = @()
		$robocpy =@()
		foreach ($fg in $db_build.Filegroups)
			foreach($file in $fg.Files)
				$robocpy+=&quot;ROBOCOPY <code>&quot;$oldloc</code>&quot; <code>&quot;$newdata</code>&quot; $shortfile /copyall /mov&quot;


		foreach($logfile in $db_build.LogFiles)
			$robocpy+=&quot;ROBOCOPY <code>&quot;$oldloc</code>&quot; <code>&quot;$newlog</code>&quot; $shortfile&quot;

		$dbchange+=&quot;Invoke-Sqlcmd -Query </code>&quot;ALTER DATABASE [&quot;+$db_build.Name+&quot;] SET OFFLINE WITH ROLLBACK IMMEDIATE;<code>&quot; -ServerInstance </code>&quot;$instance<code>&quot; -Database </code>&quot;master<code>&quot;&quot;

		$dbchange &gt;&gt; $outputfile
		$robocpy &gt;&gt; $outputfile

		&quot;Invoke-Sqlcmd -Query </code>&quot;ALTER DATABASE [&quot;+$db_build.Name+&quot;] SET ONLINE;<code>&quot; -ServerInstance </code>&quot;$instance<code>&quot; -Database </code>&quot;master`&quot;&quot;  &gt;&gt; $outputfile

Leave a Reply

Your email address will not be published. Required fields are marked *