Saturday, January 10, 2009

Automating a DPM Recovery

OK, so we're using Microsoft Data Protection Manager 2007 (DPM) as our backup solution for several of our SQL DB and Windows file servers. It's pretty straight forward stuff, but like most new software, documentation for DPM is limited and "googling" for answers, yields limited help. Let me describe one problem we faced, then give you an overview of the solution I engineered.
The DB system runs on two clustered Live servers, and is required (by the vendor) to be replicated to a Report server for resource consuming queries. This is a SQL2005 implementation.
The vendor put a backup job in place which basically does a DB dump to a .bak file every night at 1AM, and hourly log dumps after that. We have implemented DPM with VSS to backup that .bak file nightly, as soon as the dump finishes.
Now, because the DB must be replicated to the report server nightly, we will be using that to validate our backups by restoring, nightly, as well.
So what we needed to do, looks like this:
1.SQL Dump @1am
2.DPM Backup of .bak file @2am
3.DPM Restore .bak to RPT @3am
4.SQL Restore on RPT @4am
The problem is, there is no way to automate restores in DPM's GUI, it must be done through powershell. Here's the script that did it.

$from = Get-Date –f "dd-MMMM-yyyy 00:01:00"
$to = Get-Date –f "dd-MMMM-yyyy HH:mm:ss"
-->>these two lines define the time parameters for the "searchoption"
$pg = Get-ProtectionGroup DPMGROUPNAME
$ds = Get-Datasource $pg[8]
-->>this sets the $pg variable, and $pg[8] uses ninth line from its output to set the $ds variable
-->>it gets a little complex below this line
$so = New-SearchOption -SearchString db_dump_file.BAK -FromRecoveryPoint "$from" -ToRecoveryPoint "$to" -SearchDetail filesfolders -SearchType contains -Recursive -Location "F:\"
-->>with the search option variable set, you can now choose which item to recover
$ri = Get-RecoverableItem -Datasource $ds[2] -SearchOption $so
$ro = New-RecoveryOption -TargetServer rptserver.domain.com -RecoveryLocation copytofolder -FileSystem -AlternateLocation "F:\MSSQL\DailyLiveRestore\" -OverwriteType overwrite -RecoveryType Restore
-->>now comes the actual recovery command, much simpler command, only 2 options
Recover-RecoverableItem -RecoverableItem $ri -RecoveryOption $ro

Here's all the commands without my comments:
$from = Get-Date –f "dd-MMMM-yyyy 00:01:00"
$to = Get-Date –f "dd-MMMM-yyyy HH:mm:ss"
$pg = Get-ProtectionGroup DPMGROUPNAME
$ds = Get-Datasource $pg[8]
$so = New-SearchOption -SearchString db_dump_file.BAK -FromRecoveryPoint "$from" -ToRecoveryPoint "$to" -SearchDetail filesfolders -SearchType contains -Recursive -Location "F:\"
$ri = Get-RecoverableItem -Datasource $ds[2] -SearchOption $so
$ro = New-RecoveryOption -TargetServer rptserver.domain.com -RecoveryLocation copytofolder -FileSystem -AlternateLocation "F:\MSSQL\DailyLiveRestore\" -OverwriteType overwrite -RecoveryType Restore
Recover-RecoverableItem -RecoverableItem $ri -RecoveryOption $ro

Working backwards, it should become clear what parameters are required, before the next commandlet will work. Remember, that in a list of items, the first item is 0, not 1. Run the commands like "Get-ProtectionGroup DPMGROUPNAME" so you will know what to use for $pg[#] when setting a datasource. I used the command "Get-Date –f "dd-MMMM-yyyy HH:mm:ss"" because this runs at 3am, and I needed the the SearchOption to limit itself to recovery points between 1am and 3am, on the current day. As you can see, Powershell experience is a must.

OK, on to the automation side of things: PowerShell scripts should be saved with file extension .ps. Now, you can't just double click the file and it executes; you have to tell windows powershell to load the DPM commandlets, to interpret the commands in the .ps file. Only then, can you make this a scheduled task.
Here's how I did it. First, I customized the commands above for my environment, and saved them in a .ps file (C:\Scripts\AutoRecover\SQL-DB-RECOVER.ps1). I then saved a batch file with the following command:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft DPM\DPM\bin\dpmshell.psc1" -command ".'C:\Scripts\AutoRecover\SQL-DB-RECOVER.ps1'"

THEN I created the scheduled task to kick off the batch file, which kicks off the powershell script.
Because of the "-OverwriteType overwrite" option, the recovered .bak file will replace the previous one each night on the report server, and then SQL on the report server will recover it with its own scheduled job. NOTE: a DPM agent must be installed on the target server for DPM to recover to it.
And there it is. I know my post became a little vague towards the end, but I'm getting a little sleepy here. Please, feel free to email me if you need help getting this to work in your environment, or if you just have any questions.
If you took the time to read this, I really hope this has helped you out in your implementation of DPM.
Finally, I'd like to thank my good friend, Sassan K. for getting me started with Microsoft DPM last year.
THANKS SASSAN!
-Rick Estrada

3 comments:

  1. I was glad to stumble across this - I had been fighting with something similar for a while.

    I had used a different technique to execute the powershell script than you... I had come up with something different when scripting a different task (automatically adding dbs to a protection group, a la a script on the ctrl p blog).. anyway, instead of creating a batch file and running it from the task scheduler, I set up my task with this horribly complex command line.

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft DPM\DPM\bin\dpmshell.psc1" -command ".'C:\Program Files\Microsoft DPM\scripts\myScript.ps1' param1 'parameter 2'"

    note that param1 has no whitespace, but parameter 2 does (in my script, parameter 2 is a file path containing white space).

    If you are interested, the reason I need to create a scheduled recovery is so I can store a copy of the DPM database backup file produced by running dpmbackup -db. I created a protection job to protect the output file (located on the DPM server), and wanted to stash a copy on another server, to aid in the event that my DPM server dies... I don't want to have to recover the catalog from tape.

    Thanks for the post!

    ReplyDelete
  2. First thanks for this. I would never have figured it out.

    I am having problems when trying to recover a Hyper-V Item. I get the the search and specifiy what I believe is the correct "location" information. The #so looks good.

    But when I try to Get-RecoverableItem, powershell terminates and closes itself.

    The location is "\Backup Using Saved State\MalibuEX2010" and that does exist.

    Any ideas? I have gotten normal dadtasets to recover with no problems so I think I have th general idea.

    Thanks.

    Doug

    ReplyDelete
  3. Thanks for posting this.

    Will give this a try

    ReplyDelete

COMMENT GOES HERE: