Commvault automated restores

We recently switched to Commvault for our SQL Server backup solution, and it slowed down a lot of our restores, because it took a long time to click through the GUI.  It was also error prone and dangerous, since it defaults to overriding production.  And we have to change the file paths, and change some names, and click different options.  So I built a new GUI with drop downs, to choose the source and target locations, and database names, and then you just hit Submit, and it runs.  Then I built another utility that shows the results of the restore.

The most challenging part was to use the Commvault command line, and build a Powershell process to call it, and to receive output from it.  To learn how to do this, I used the Save As Script functionality of the restore.  This will produce an XML file and a .bat file.   What you want to do is have your Powershell generate an XML file, and then start the .bat file.

Not all of the XML is needed.  I took out the unnecessary values.  I won’t post an XML sample here, but I can answer any questions in the comments section.  One tip though, is that you may need UTF8 encoding when you create the file.

$XMLOutput | Out-File “RestoreParameters.xml” -Encoding “UTF8”;

Here is the code to start the batch process.

$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = “\\fileserver\RestoreDatabaseFromXMLv3.bat”
$pinfo.UseShellExecute = $false
$pinfo.RedirectStandardOutput = $true
$process = New-Object System.Diagnostics.Process

$process.StartInfo = $pinfo
$process.Start() | Out-Null; # ignore return value with Out-Null
while (!$process.HasExited )
{        $process.Refresh();
#custom function to update database with progress
UpdateProgress $RestoreFileNumber $RestoreProgressResults.PercentComplete                    Start-Sleep -s 10
$stdout = $process.StandardOutput.ReadToEnd();
Write-Host $stdout;

Notes – these lines help redirect the output, so that you can capture it in your automation.

  1. $pinfo.UseShellExecute = $false Setting this property to false enables you to redirect input, output, and error streams.  When UseShellExecute is false, you can start only executables by using the Process object.
  2. $pinfo.RedirectStandardOutput = $true.  When a Process writes text to its standard stream, that text is typically displayed on the console. By setting RedirectStandardOutput to true to redirect the StandardOutput stream, you can manipulate or suppress the output of a process
  3. $stdout = $process.StandardOutput.ReadToEnd(); You can then update your database with the value from $stdout.

Let me know if there is any interest on this topic, and I can flesh out the details more.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s