Add logging and dryRun

This commit is contained in:
2025-09-08 13:42:31 +01:00
parent 96cfbdf422
commit 66ec0a068e

View File

@@ -25,10 +25,15 @@ param (
[Parameter(Mandatory=$True, [Parameter(Mandatory=$True,
HelpMessage="Name of SLA to trigger")] 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" $GlobalSAFile = "C:\Rubrik\james.xml"
$logFile = "C:\Rubrik\monthlySnap.log"
########################### ###########################
# Script begins # Script begins
@@ -36,25 +41,34 @@ $GlobalSAFile = "C:\Rubrik\james.xml"
$ErrorActionPreference = 'Stop' $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 Import-Module RubrikSecurityCloud
Connect-Rsc -ServiceAccountFile $GlobalSAFile Connect-Rsc -ServiceAccountFile $GlobalSAFile
Write-Host "INFO: Getting Tenant details" Write-Log "Connected to Rubrik Security Cloud."
$srcSla = Get-RscSla -Name $sourceSla $srcSla = Get-RscSla -Name $sourceSla
if (-not $srcSla) { 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 Disconnect-Rsc
exit 1 exit 1
} }
Write-Log "Found SLA: $($srcSla.Name) ($($srcSla.Id))"
$monthlySla = Get-RscSla -Name $triggerSla $monthlySla = Get-RscSla -Name $triggerSla
if (-not $monthlySla) { 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 Disconnect-Rsc
exit 1 exit 1
} }
Write-Log "Found monthly SLA: $($monthlySla.Name) ($($monthlySla.Id))"
$query = New-RscQuery -GqlQuery protectedObjectsConnection $query = New-RscQuery -GqlQuery protectedObjectsConnection
$query.Var.slaIds = @($srcSla.Id) $query.Var.slaIds = @($srcSla.Id)
@@ -64,22 +78,24 @@ $query.var.filter[0].Texts = "false"
$protectedObjects = $query.invoke() $protectedObjects = $query.invoke()
$protectedObjects.Nodes | ForEach-Object { Write-Log "INFO: Printing protected objects:"
$protectedObjects.Nodes | ForEach-Object { Write-Log "Name: $($_.Name), Id: $($_.Id)" }
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"
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 Disconnect-Rsc