Discussion:
Help with SQL Database Recovery Script
(too old to reply)
ba_baracus
2009-07-09 15:26:03 UTC
Permalink
Hi,

I have a powershell script that can recover the latest DPM SQL recovery
point to a folder. I got the script from:
http://blogs.technet.com/dpm/archive/2007/09/04/cli-script-copy-latest-point-in-time-of-sql-db-to-a-folder.aspx

Our DPM server protects our SQL server with 200 databases.
I need to recover all of the SQL databases from one of our SQL servers to a
folder via DPM. Using this script is it possible to specify a wildcard to
select all of the SQL databases to recover or a loop to go through and
recover each database one after another?
Preferably without having to run the script 200 times or type in the name
for each database?

I am unfamiliar with Powershell and so am struggling to find a solution?
ba_baracus
2009-07-20 14:12:43 UTC
Permalink
Seeing as though there was no reply I thought to post back an update that I
have now successfully modified the script to restore all SQL datasources from
a protection group to a network folder without having to type in each
database into the script: -


$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 (!$continue)
{
write-host "Please Type Y to continue or N to exit"
$continue = Read-Host "Continue ? :"

if (!$continue)
{
Write-Error "Nothing entered exiting program"
exit 1
}
}

IF ($Continue -ne "Y")
{
Exit 1
}



if (!$pgName)
{
$pgName = Read-Host "Enter Protection Group Name:"

if (!$pgName)
{
Write-Error "Protection Group not entered"
exit 1
}
}

if (!$DPMServerName)
{
$DPMServerName = Read-Host "Enter DPM server name"

if (!$DPMServerName)
{
Write-Error "Dpm server name not specified."
exit 1
}
}

if (!$DestinationServerName)
{
$DestinationServerName = Read-Host "Enter Destination server name"

if (!$DestinationServerName)
{
Write-Error "Destination server not specified."
exit 1
}
}

if (!$DestinationLocation)
{
$DestinationLocation = Read-Host "Enter Location on the destination
server"

if (!$DestinationLocation)
{
Write-Error "Destination location not specified"
exit 1
}
}

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 -eq
$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
}

#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)"
}

}

}
Post by ba_baracus
Hi,
I have a powershell script that can recover the latest DPM SQL recovery
http://blogs.technet.com/dpm/archive/2007/09/04/cli-script-copy-latest-point-in-time-of-sql-db-to-a-folder.aspx
Our DPM server protects our SQL server with 200 databases.
I need to recover all of the SQL databases from one of our SQL servers to a
folder via DPM. Using this script is it possible to specify a wildcard to
select all of the SQL databases to recover or a loop to go through and
recover each database one after another?
Preferably without having to run the script 200 times or type in the name
for each database?
I am unfamiliar with Powershell and so am struggling to find a solution?
p***@gmail.com
2013-09-08 16:25:27 UTC
Permalink
Apply next solution fix sql server 2008 r2 mdf file

http://www.fixmdfsqlserver2008file.sqlmdffix.com/

Loading...