Scripts/ProtectionJobRun/update-cohesityprotectionjobrun.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
function Update-CohesityProtectionJobRun { <# .SYNOPSIS Update the protection job run to extend on local, archive and replication servers. .DESCRIPTION The Update-CohesityProtectionJobRun function is used to update the existing protection job run with to extend on local, archive and replication servers. Piping can also be used with this cmdlet. .EXAMPLE # Extend the retention for 10 days on local server for the selected job runs Update-CohesityProtectionJobRun -ProtectionJobName viewJob -JobRunIds 65675,65163 -ExtendRetention 10 .EXAMPLE Update-CohesityProtectionJobRun -ProtectionJobName viewJob -JobRunIds 65675 -ExtendRetention 10 (Extend by 10 days) .EXAMPLE Update-CohesityProtectionJobRun -ProtectionJobName viewJob -JobRunIds 65675 -ExtendRetention -3 (Reduce by 3 days) .EXAMPLE Update-CohesityProtectionJobRun -ProtectionJobName viewJob -JobRunIds 65675 -ExtendRetention 0 (Mark the snapshot for deletion) .EXAMPLE # Extend the retention for 10 days for all job runs with the given job name Update-CohesityProtectionJobRun -ProtectionJobName viewJob -ExtendRetention 10 .EXAMPLE # Extend the retention by providing start time and end time Update-CohesityProtectionJobRun -ProtectionJobName viewJob -StartTimeUsecs 1573929000000000 -EndTimeUsecs 1574101799999000 -ExtendRetention 10 .EXAMPLE # Piping the job runs Get-CohesityProtectionJobRun -JobName viewJob | Update-CohesityProtectionJobRun -ExtendRetention 10 .EXAMPLE Get-CohesityProtectionJobRun -JobName viewJob -StartTime 1573929000000000 -EndTime 1574101799999000 | Update-CohesityProtectionJobRun -ExtendRetention 10 .EXAMPLE # Extend retention for archive Update-CohesityProtectionJobRun -ArchiveNames nas-archive-3,nas-archive-2,nas-archive-4 -ArchiveRetention 20 -ArchivePartialJobRun:$false -JobRunIds 583 -ProtectionJobName job-small-vms .EXAMPLE # Extend retention for replication Update-CohesityProtectionJobRun -ReplicationNames replication-server1,replication-server2 -ReplicationRetention 10 -ReplicationPartialJobRun:$false -JobRunIds 651 -ProtectionJobName job-small-vms #> [CmdletBinding(DefaultParameterSetName = "Local", SupportsShouldProcess = $True, ConfirmImpact = "High")] param( [Parameter(Mandatory = $False)] $ProtectionJobName = $null, [Parameter(Mandatory = $False)] [string[]]$JobRunIds = $null, [Parameter(Mandatory = $False)] [Uint64]$StartTimeUsecs = $null, [Parameter(Mandatory = $False)] [Uint64]$EndTimeUsecs = $null, [Parameter(Mandatory = $True, ParameterSetName = "Local")] [Parameter(ParameterSetName = "Archive")] [Parameter(ParameterSetName = "Replication")] [Int64]$ExtendRetention = $null, [Parameter(Mandatory = $True, ParameterSetName = "Archive")] [string[]]$ArchiveNames = $null, [Parameter(Mandatory = $True, ParameterSetName = "Archive")] [Int64]$ArchiveRetention = $null, [Parameter(Mandatory = $True, ParameterSetName = "Archive")] [switch]$ArchivePartialJobRun, [Parameter(Mandatory = $True, ParameterSetName = "Replication")] [string[]]$ReplicationNames = $null, [Parameter(Mandatory = $True, ParameterSetName = "Replication")] [Int64]$ReplicationRetention = $null, [Parameter(Mandatory = $True, ParameterSetName = "Replication")] [switch]$ReplicationPartialJobRun, [Parameter(ValueFromPipeline = $True, DontShow = $True)] [object[]]$BackupJobRuns = $null ) begin { if (-not (Test-Path -Path "$HOME/.cohesity")) { throw "Failed to authenticate. Please connect to the Cohesity Cluster using 'Connect-CohesityCluster'" } $session = Get-Content -Path $HOME/.cohesity | ConvertFrom-Json $server = $session.ClusterUri $token = $session.Accesstoken.Accesstoken $global:updatedJobRundIds = @() } process { if ($PSCmdlet.ShouldProcess($ProtectionJobName)) { #Headers $token = 'Bearer ' + $session.Accesstoken.Accesstoken $headers = @{ "Authorization" = $token } $jobUpdated = 0 $failedJobRunIds = @() $succeedJobRunIds = @() #Collect all the job run ids from the backup run details fetched through pipeline, if the user doesn't provide any run ids if ($null -ne $BackupJobRuns) { $JobRunIds = $BackupJobRuns.backupRun.jobRunId $ProtectionJobName = $BackupJobRuns.jobName } if ($null -eq $ProtectionJobName) { Write-Output "Please provide protection job name" return } if ($null -eq $BackupJobRuns) { #Fetch the job id for the specified job try { $jobUrl = $server + '/irisservices/api/v1/public/protectionJobs' $jobResp = Invoke-RestApi -Method 'Get' -Uri $jobUrl -Headers $headers if ($null -eq $jobResp) { Write-Warning "No Protection Jobs available in the connected cluster" return } foreach ($job in $jobResp) { if ($job.Name -eq $ProtectionJobName) { $JobId = $job.id break } } } catch { Write-Error $_.Exception.Message return } #If job exists then collect the job run details for the specific job if ($JobId) { $jobRunUrl = $server + '/irisservices/api/v1/public/protectionRuns?jobId=' + $JobId if ($StartTimeUsecs) { $jobRunUrl = $jobRunUrl + '&startTimeUsecs=' + $StartTimeUsecs } if ($EndTimeUsecs) { $jobRunUrl = $jobRunUrl + '&endTimeUsecs=' + $EndTimeUsecs } $BackupJobRuns = Invoke-RestApi -Method 'Get' -Uri $jobRunUrl -Headers $headers } else { Write-Output "Protection job '$ProtectionJobName' doesn't exist." return } } if ($ReplicationNames) { $remoteClusters = Get-CohesityRemoteCluster if ($null -eq $remoteClusters) { Write-Output "No replication cluster found" return } $replicationClusters = $null foreach ($item in $ReplicationNames) { if ($remoteClusters.name -contains $item) { $remoteCluster = $remoteClusters | where-object { $_.name -eq $item } if ($null -eq $replicationClusters) { $replicationClusters = @() } $replicationObject = @{ replicationTarget = @{ clusterId = $remoteCluster.ClusterId clusterName = $remoteCluster.Name } daysToKeep = $ReplicationRetention type = "kRemote" copyPartial = $ReplicationPartialJobRun } $replicationClusters += $replicationObject } else { Write-Output "The replication cluster '$item' not found" } } } if ($ArchiveNames) { $vaults = Get-CohesityVault if ($null -eq $vaults) { Write-Output "No archives found" return } $archives = $null foreach ($item in $ArchiveNames) { if ($vaults.name -contains $item) { $vault = $vaults | where-object { $_.name -eq $item } if ($null -eq $archives) { $archives = @() } $archiveObject = @{ archivalTarget = @{ vaultId = $vault.id vaultName = $vault.name vaultType = "kCloud" } daysToKeep = $ArchiveRetention type = "kArchival" copyPartial = $ArchivePartialJobRun } $archives += $archiveObject } else { Write-Output "The archive '$item' not found" } } } if ($BackupJobRuns) { #collect all job run ids, if the user doesn't provide any specific job run id if ($null -eq $JobRunIds) { $JobRunIds += $BackupJobRuns.backupRun.jobRunId } foreach ($JobRun in $BackupJobRuns) { if ($JobRunIds -contains $JobRun.backupRun.jobRunId) { [bool]$snapshotDeleted = $JobRun.backupRun.snapshotsDeleted if ($snapshotDeleted -eq $false) { if ($ExtendRetention) { $copyRunTarget = @{ daysToKeep = $ExtendRetention type = "kLocal" } } $jobRunObj = @{ copyRunTargets = @() jobUid = @{ clusterId = $JobRun.jobUid.clusterId clusterIncarnationId = $JobRun.jobUid.clusterIncarnationId id = $JobRun.jobUid.id } runStartTimeUsecs = $JobRun.copyRun[0].runStartTimeUsecs } if ($replicationClusters) { $jobRunObj.copyRunTargets += $replicationClusters } if ($archives) { $jobRunObj.copyRunTargets += $archives } if ($copyRunTarget) { $jobRunObj.copyRunTargets += $copyRunTarget } $payload = @{ jobRuns = @($jobRunObj) } $payloadJson = $payload | ConvertTo-Json -Depth 100 # Write-Output $payloadJson try { $url = $server + '/irisservices/api/v1/public/protectionRuns' Invoke-RestApi -Method 'Put' -Uri $url -Headers $headers -Body $payloadJson | Out-Null $jobUpdated += 1 $succeedJobRunIds += $JobRun.backupRun.jobRunId $global:updatedJobRundIds += $JobRun.backupRun.jobRunId } catch { $failedJobRunIds += $JobRun.backupRun.jobRunId Write-Error $_ } } } } if ($failedJobRunIds.length -ne 0) { Write-Warning "Some of the snapshot's retention is not updated, with job run id(s) $failedJobrunIds" } if ($succeedJobRunIds.length -ne 0) { Write-Output "Updated the snapshot retention for job run id(s) $succeedJobRunIds, successfully for the job '$ProtectionJobName'" } } else { Write-Output "Backup job run details are unavilable" } } } End { if ($ProtectionJobName) { Get-CohesityProtectionJobRun -JobName $ProtectionJobName | Where-Object { $global:updatedJobRundIds -contains $_.backupRun.jobRunId } } } } |