SQL Recovery Point Script for DPM 2007

  • RSS
  • Subscribe

Statistics

  • Entries (6)
  • Comments (0)
Posted by pzerger Wednesday, September 02, 2009 5:31:22 PM
Rate this Content 0 Votes

#
#
# Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2009
#
# NAME: Data Protection Manager 2007 - SQL Recovery Point Script
#
# COMMENT: This Script will Restore the latest recovery point for all SQL databases
#          within specified protection group to a network folder
#
# Add-PSSnapin Microsoft.DataProtectionManager.PowerShell

# ==================
#Variables
# ==================
$pgName = "PG_SQL-Data_M2M_Daily"

#DPM server name, destination server and local restore path
$DPMServerName = "MYDPMSERVER"
$DestinationServerName = "RESTORESERVER"
$DestinationLocation = "D:\DAILY\MYBACKUP"

$objtype = "SQL Server 2005 database"

write-host "This Script will Restore the latest recovery point for all SQL databases within specified protection group to a network folder"

if (!(Connect-DPMServer $DPMServerName))
{
Write-Error "Failed to connect To DPM server $DPMServerName"
exit 1
}

#Get the protection group for variable $PgName

$pg = Get-ProtectionGroup $DPMServerName | where { $_.FriendlyName -like $pgName }

if(!$pg)
{
Write-Host "Protection group " $pgName " not found"
exit 1
}

#Get a list of all Datasources with a type of "$ObjType"

$ds = Get-Datasource $pg #|

if(!$ds)
{
Write-Host "error"
exit 1
}

# =========================================================
#BEGIN DEBUG CODE: Writes datasource and type to the screen
# =========================================================

#foreach ($datasource in $ds)
#{
#Write-Host $datasource.type.name " : " $datasource.name
#}
#if (!$Objtype)
#{
# $Objtype = Read-Host "Object type ie : SQL Server 2005 database : "

#if (!$DestinationLocation)
#{
# Write-Error "Object type not specified"
# exit 1
# }
#}

#Display a list of "SqlObjects"

# Restore the latest recovery point of each SQL datasource.
foreach ($datasource in $ds)
{
if ($datasource.type.name -eq $objtype)
{
    Write-Host $datasource.name
    Write-Host $datasource.type.name

# Restore the latest recovery point of each SQL datasource.

# Select the latest recovery point that exists on disk and trigger the restore job.
foreach ($rp in @(Get-RecoveryPoint -Datasource $datasource | sort -Property RepresentedPointInTime -Descending))
{
foreach ($rsl in $rp.RecoverySourceLocations)
{
if ($rsl -is [Microsoft.Internal.EnterpriseStorage.Dls.UI.ObjectModel.OMCommon.ReplicaDataset])
{
$recoveryOption = New-RecoveryOption -TargetServer $DestinationServerName -TargetLocation $DestinationLocation -RecoveryLocation CopyToFolder -SQL -RecoveryType Restore

$restoreJob = Recover-RecoverableItem -RecoverableItem $rp -RecoveryOption $recoveryOption -RecoveryPointLocation $rsl

break
}
}

if ($restoreJob)
{
break
}
}

if ($restoreJob)
{
Write-Host "`nRunning restore of $($datasource.LogicalPath) from
$($rp.RepresentedPointInTime) to $DestinationServerName\$DestinationLocation"
# Comment out the next 7 lines to not wait for one restore job to finish before triggering the next one.
while (!$restoreJob.HasCompleted)
{
Write-Host "." -NoNewLine
sleep 3
}

#Write-Host "`nJob status: $($restoreJob.Status)"
}
else
{
Write-Error "Could not find a recovery point on disk for
$($datasource.LogicalPath)"
}

}

}

Copyright 2010 SCDPMOnline.org
Share This Using Popular Bookmarking Services
Comments are closed on this post.
Site Map | Printable View | © 2008 - 2012 SCDPM Online |

Locations of visitors to this page

Powered by mojoPortal | HTML 5 | CSS | design by dcarter