From 66ec0a068ef8b2c436427f1a8108f0fe71525387 Mon Sep 17 00:00:00 2001 From: James Pattinson Date: Mon, 8 Sep 2025 13:42:31 +0100 Subject: [PATCH] Add logging and dryRun --- monthlySnap.ps1 | 54 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/monthlySnap.ps1 b/monthlySnap.ps1 index a97db60..ae35cfc 100644 --- a/monthlySnap.ps1 +++ b/monthlySnap.ps1 @@ -25,10 +25,15 @@ param ( [Parameter(Mandatory=$True, HelpMessage="Name of SLA to trigger")] - [string]$triggerSla + [string]$triggerSla, + + [Parameter(Mandatory=$False, + HelpMessage="If set, only print objects that would be backed up")] + [switch]$dryrun ) $GlobalSAFile = "C:\Rubrik\james.xml" +$logFile = "C:\Rubrik\monthlySnap.log" ########################### # Script begins @@ -36,25 +41,34 @@ $GlobalSAFile = "C:\Rubrik\james.xml" $ErrorActionPreference = 'Stop' +function Write-Log($message) { + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + $logEntry = "$timestamp $message" + Add-Content -Path "C:\Rubrik\monthlySnap.log" -Value $logEntry + Write-Host $logEntry +} + Import-Module RubrikSecurityCloud Connect-Rsc -ServiceAccountFile $GlobalSAFile -Write-Host "INFO: Getting Tenant details" +Write-Log "Connected to Rubrik Security Cloud." $srcSla = Get-RscSla -Name $sourceSla if (-not $srcSla) { - Write-Error "ERROR: No source SLA found with name '$sourceSla'. Exiting script." + Write-Log "ERROR: No SLA found with name '$sourceSla'. Exiting script." Disconnect-Rsc exit 1 } +Write-Log "Found SLA: $($srcSla.Name) ($($srcSla.Id))" $monthlySla = Get-RscSla -Name $triggerSla if (-not $monthlySla) { - Write-Error "ERROR: No monthly SLA found with name '$triggerSla'. Exiting script." + Write-Log "ERROR: No monthly SLA found with name '$triggerSla'. Exiting script." Disconnect-Rsc exit 1 } +Write-Log "Found monthly SLA: $($monthlySla.Name) ($($monthlySla.Id))" $query = New-RscQuery -GqlQuery protectedObjectsConnection $query.Var.slaIds = @($srcSla.Id) @@ -64,22 +78,24 @@ $query.var.filter[0].Texts = "false" $protectedObjects = $query.invoke() -$protectedObjects.Nodes | ForEach-Object { - - Write-Host "Triggering Backup for Name: $($_.Name), Id: $($_.Id)" - - $oracleDb = Get-RscOracleDatabase -name "example" - - $query = New-RscMutation -GqlMutation takeOnDemandOracleDatabaseSnapshot - $query.Var.input = Get-RscType -Name TakeOnDemandOracleDatabaseSnapshotInput -InitialProperties config.baseOnDemandSnapshotConfig - $query.Var.input.id = $_.Id - $query.Var.input.Config.forceFullSnapshot = $false - $query.Var.input.Config.baseOnDemandSnapshotConfig.slaId = $monthlySla.id - $query.Field = Get-RscType -Name AsyncRequestStatus -InitialProperties id - $result = $query.Invoke() - - Write-Host "Backup triggered for Name: $($_.Name), Id: $result.Id" +Write-Log "INFO: Printing protected objects:" +$protectedObjects.Nodes | ForEach-Object { Write-Log "Name: $($_.Name), Id: $($_.Id)" } +if ($dryrun) { + Write-Log "Dry run mode: No backups will be triggered. The following objects would have been backed up:" + $protectedObjects.Nodes | ForEach-Object { Write-Log "[DRYRUN] Name: $($_.Name), Id: $($_.Id)" } +} else { + $protectedObjects.Nodes | ForEach-Object { + Write-Log "Triggering Backup for Name: $($_.Name), Id: $($_.Id)" + $query = New-RscMutation -GqlMutation takeOnDemandOracleDatabaseSnapshot + $query.Var.input = Get-RscType -Name TakeOnDemandOracleDatabaseSnapshotInput -InitialProperties config.baseOnDemandSnapshotConfig + $query.Var.input.id = $_.Id + $query.Var.input.Config.forceFullSnapshot = $false + $query.Var.input.Config.baseOnDemandSnapshotConfig.slaId = $monthlySla.id + $query.Field = Get-RscType -Name AsyncRequestStatus -InitialProperties id + $result = $query.Invoke() + Write-Log "Backup triggered for Name: $($_.Name), Id: $result.Id" + } } Disconnect-Rsc \ No newline at end of file