Skip to content

Commit

Permalink
Modified restore oracle function to get the latest job run id, if not…
Browse files Browse the repository at this point in the history
… specified.
  • Loading branch information
KavishreeShanmugam11 committed Feb 7, 2024
1 parent 3a1ee4b commit cb90246
Showing 1 changed file with 66 additions and 24 deletions.
Expand Up @@ -9,8 +9,11 @@ function Restore-CohesityOracleDatabase {
.LINK
https://cohesity.github.io/cohesity-powershell-module/#/README
.EXAMPLE
Restore-CohesityOracleDatabase -SourceName 10.2.14.31 -TargetSourceId 1277 -JobId 31520 -TargetHostId 770 -NewDatabaseName CohesityDB_r1
Restore Oracle database from cluster with database id 1279 , database instance id 1277 and job id as 31520
Restore-CohesityOracleDatabase -SourceName "x.x.x.x" -TargetSourceId 123 -JobId 456 -SourceDatabaseName "database_1" -OracleHome "/u01/app/oracle/product/19c/db_1" -OracleBase "/u01/app/oracle" -DatabaseFileDestination "/u01/app/oracle/product" -NewDatabaseName "database_new"
Restore Oracle database "database_1" with latest snapshot in specified database file destination in the target oracle source with an id 123
.EXAMPLE
Restore-CohesityOracleDatabase -SourceName "x.x.x.x" -TargetSourceId 123 -JobId 456 -SourceDatabaseName "database_1" -OracleHome "/u01/app/oracle/product/19c/db_1" -OracleBase "/u01/app/oracle" -NewDatabaseName "database_new" -JobRunId 789
Restore Oracle database "database_1" with mentioned job run id, in the target oracle source with an id 123
#>

[CmdletBinding(DefaultParameterSetName = "Default", SupportsShouldProcess = $True, ConfirmImpact = "High")]
Expand Down Expand Up @@ -75,49 +78,85 @@ function Restore-CohesityOracleDatabase {
}

Process {
$resp = Get-CohesityProtectionSource -Name $SourceName
if ($null -eq $resp) {
# Validate provided oracle source
$psObject = Get-CohesityProtectionSource -Name $SourceName
if ($null -eq $psObject) {
write-output ("Source " + $SourceName + " is not available")
return
}

$SourceId = $resp.rootNode.id
# Collect the oracle source id
$SourceId = $psObject.rootNode.id
if ($PSCmdlet.ShouldProcess($SourceId)) {

# Validate provided protection job
$job = Get-CohesityProtectionJob -Ids $JobId
if (-not $job) {
Write-Output "Cannot proceed, the job id '$JobId' is invalid"
return
}

$protectionSourceObject = Get-CohesityProtectionSource -Id $TargetSourceId
if ($protectionSourceObject.id -ne $TargetSourceId) {
# Validate provided target oracle source
$psTargetObject = Get-CohesityProtectionSource -Id $TargetSourceId
if ($psTargetObject.id -ne $TargetSourceId) {
Write-Output "Cannot proceed, the target host id '$TargetSourceId' is invalid"
return
}
$ORACLE_OBJECT_RESTORE_TYPE = 19
$searchedVMDetails = $null

# Collect the oracle database details required for restore
$searchDatabaseDetails = $null
$searchURL = '/irisservices/api/v1/searchvms?entityTypes=kOracle&jobIds=' + $JobId + '&vmName=' + $SourceDatabaseName
$searchResult = Invoke-RestApi -Method Get -Uri $searchURL

if ($Global:CohesityAPIStatus.StatusCode -ne 200) {
Write-Output "Could not search Oracle objects with the job id $JobId"
Write-Output "Could not search oracle database with the job id $JobId"
return
}
$searchedVMDetails = $searchResult.vms | Where-Object { ($_.vmDocument.objectAliases -contains $SourceName) }

if ($null -eq $searchedVMDetails) {
write-output "Failed to fetch VM details of source"
$searchDatabaseDetails = $searchResult.vms | Where-Object { ($_.vmDocument.objectAliases -contains $SourceName) }
if ($null -eq $searchDatabaseDetails) {
write-output "Failed to fetch oracle database details"
return
}

# If snapshot details(JobRunId & StartTime) not provided, then fetch the latest snapshot details of provided job id
if (-not $JobRunId -or -not $StartTime) {
# $runs = Get-CohesityProtectionJobRun -JobId $JobId -ExcludeErrorRuns:$true
# foreach ($run in $runs) {
# if ($run.backupRun.status -eq "kSuccess") {
# $JobRunId = $run.backupRun.jobRunId
# $StartTime = $run.backupRun.stats.startTimeUsecs
# }
# }
$snapshotURL = '/irisservices/api/v1/public/restore/objects?search=' + $SourceDatabaseName + '&jobIds=' + $JobId
$snapshotResult = Invoke-RestApi -Method Get -Uri $snapshotURL

if ($Global:CohesityAPIStatus.StatusCode -ne 200) {
Write-Output "Could not search snapshot information for oracle database $SourceDatabaseName"
return
}

if ($snapshotResult -and $snapshotResult.totalCount -ne 0) {
$snapshotDetail = $null
$snapshotDetail = $snapshotResult.objectSnapshotInfo | Where-Object {$_.SnapshottedSource.ParentId -eq $SourceId -and $_.SnapshottedSource.name -eq $SourceDatabaseName}

if (-not $JobRunId) {
$runs = Get-CohesityProtectionJobRun -JobId $JobId -ExcludeErrorRuns:$true
foreach ($run in $runs) {
if ($run.backupRun.status -eq "kSuccess") {
$JobRunId = $run.backupRun.jobRunId
$StartTime = $run.backupRun.stats.startTimeUsecs
if ($null -ne $snapshotDetail){
if (-not $JobRunId){
$JobRunId = $snapshotDetail.versions[0].jobRunId
$StartTime = $snapshotDetail.versions[0].startedTimeUsecs
}
if (-not $StartTime){
$versionDetail = $snapshotDetail.versions | Where-Object {$_.jobRunId -eq $JobRunId}

if ($null -ne $versionDetail){
$StartTime = $versionDetail.startedTimeUsecs
} else {
Write-Output "Could not find the snapshot details for the database $SourceDatabaseName with job run id $JobRunId"
return
}
}
} else {
Write-Output "Could not find the snapshot details for the database $SourceDatabaseName"
return
}
}
}
Expand All @@ -138,7 +177,7 @@ function Restore-CohesityOracleDatabase {
if (-not $NewDatabaseName) {
$NewDatabaseName = $SourceDatabaseName
}
$jobUid = $searchedVMDetails.vmDocument.objectId.jobUid
$jobUid = $searchDatabaseDetails.vmDocument.objectId.jobUid

$oracleRestoreParams = [PSCustomObject]@{
captureTailLogs = $true
Expand All @@ -153,7 +192,7 @@ function Restore-CohesityOracleDatabase {
$alternateLocationParams | add-member -type noteproperty -name homeDir -value $OracleHome
$alternateLocationParams | add-member -type noteproperty -name baseDir -value $OracleBase
$alternateLocationParams | add-member -type noteproperty -name oracleDbConfig -value $OracleDbConfig
$alternateLocationParams | add-member -type noteproperty -name databaseFileDestination -value $DatabaseFileDestination
$alternateLocationParams | add-member -type noteproperty -name databaseFileDestination -value $dbfileDest

#$oracleRestoreParams | Add-Member -Name alternateLocationParams -value $alternateLocationParams -memberType NoteProperty

Expand All @@ -174,13 +213,16 @@ function Restore-CohesityOracleDatabase {
# write-output (($SourceId -ne $TargetSourceId) -or ($NewDatabaseName -ne $SourceDatabaseName))

$restoreAppObject = [PSCustomObject]@{
appEntity = [PSCustomObject]$searchedVMDetails.vmDocument.objectId.entity
appEntity = [PSCustomObject]$searchDatabaseDetails.vmDocument.objectId.entity
restoreParams = [PSCustomObject] $restoreParams

}
# write-output $restoreAppObject | ConvertTo-Json -Depth 100
# write-output $restoreParams | ConvertTo-Json -Depth 100

# Initialize variable required for restore
$ORACLE_OBJECT_RESTORE_TYPE = 19

$payload = [PSCustomObject]@{
action = "kRecoverApp"
name = $TaskName
Expand Down

0 comments on commit cb90246

Please sign in to comment.