From 9d0e2f7fde6df0bbf315fe079fa35fd485c798e9 Mon Sep 17 00:00:00 2001 From: James Pattinson Date: Thu, 9 Oct 2025 10:09:16 +0100 Subject: [PATCH] Parameterise instance name for backup script --- backup.ps1 | 37 ++++++++++++++++++++++++++++--------- createSAcreds.ps1 | 14 +++++++------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/backup.ps1 b/backup.ps1 index 3795f01..abe7f20 100644 --- a/backup.ps1 +++ b/backup.ps1 @@ -1,14 +1,19 @@ +param( + [Parameter(Mandatory=$true)] + [string]$SqlInstance +) + # # backup.ps1 # # TODO: Update cleanup time based on backup type -$sqlInstance = "sqlfcsql\TESTINST" -#$directory = "H:\Backup" -$directory = "C:\Rubrik\mount" +$instanceName = $SqlInstance.Split('\')[1] + +$directory = "C:\Rubrik\$instanceName" $fullBackupDay = 'Thursday' $checkCluster = $false -$logFile = "C:\Rubrik\backup.log" +$logFile = "C:\Rubrik\backup-$instanceName.log" $fullFlag = $directory + "\last_full.flag" $diffFlag = $directory + "\last_diff.flag" @@ -29,21 +34,35 @@ function Write-Log($message) { Write-Host $logEntry } +# Check if directory exists and is a symbolic link +if (-not (Test-Path $directory)) { + Write-Log "ERROR: Directory '$directory' does not exist. Exiting script." + exit 1 +} + +$directoryInfo = Get-Item $directory +if (-not ($directoryInfo.Attributes -band [System.IO.FileAttributes]::ReparsePoint)) { + Write-Log "ERROR: Directory '$directory' is not a symbolic link. Exiting script." + exit 1 +} + +Write-Log "INFO: Directory '$directory' exists and is a symbolic link. Target: $($directoryInfo.Target). Proceeding." + if ($checkCluster) { # Check if SQL instance is running locally $localNode = $env:COMPUTERNAME - $instanceName = $sqlInstance.Split('\')[1] + $clusterInstance = Get-ClusterResource | Where-Object { $_.ResourceType -eq "SQL Server" -and $_.Name -eq "SQL Server ($instanceName)" } if ($clusterInstance) { $ownerNode = $clusterInstance.OwnerNode.Name if ($ownerNode -ne $localNode) { - Write-Log "SQL instance '$sqlInstance' is not running on local node '$localNode'. Exiting script." + Write-Log "SQL instance '$SqlInstance' is not running on local node '$localNode'. Exiting script." exit 1 } else { - Write-Log "SQL instance '$sqlInstance' is running on local node '$localNode'. Proceeding." + Write-Log "SQL instance '$SqlInstance' is running on local node '$localNode'. Proceeding." } } else { - Write-Log "ERROR: SQL instance '$sqlInstance' not found in cluster resources." + Write-Log "ERROR: SQL instance '$SqlInstance' not found in cluster resources." exit 1 } } else { @@ -77,7 +96,7 @@ if ((Get-Date).DayOfWeek -eq $fullBackupDay) { $query = "EXECUTE [dbo].[DatabaseBackup] @Databases = 'ALL_DATABASES', @Directory = '$directory', @BackupType = '$backupType', @Verify = 'N', @CleanupTime = $cleanupTime, @CheckSum = 'Y', @LogToTable = 'Y'" Write-Log "Executing backup type: $backupType" -$sqlcmdOutput = & sqlcmd -S $sqlInstance -Q $query 2>&1 +$sqlcmdOutput = & sqlcmd -S $SqlInstance -Q $query 2>&1 $sqlcmdExitCode = $LASTEXITCODE if ($sqlcmdExitCode -eq 0) { diff --git a/createSAcreds.ps1 b/createSAcreds.ps1 index a433536..ab9654e 100644 --- a/createSAcreds.ps1 +++ b/createSAcreds.ps1 @@ -32,8 +32,8 @@ If $true, keep the temporary script and task after completion. Default $false = cleanup. .EXAMPLE - # Using gMSA - .\createSAcreds.ps1 -Domain AD -AccountName rubrikgmsa -AccountType gMSA -SaJsonPath C:\temp\sa.json -OutputXmlPath C:\temp\sa-rbksql.xml + # Using gMSA (no need to user $ at end of name) + .\createSAcreds.ps1 -Domain AD -AccountName rubrikgmsa -AccountType gMSA -SaJsonPath C:\Rubrik\scripts\sa.json -OutputXmlPath C:\Rubrik\scripts\sa-rbksql.xml .EXAMPLE # Using regular service account with password prompt @@ -77,7 +77,7 @@ try { # ---- Create the one-shot script that will run under the service account ---- $oneShotContent = @" -# One-shot script created by create-and-run-one-shot-via-gMSA.ps1 +# One-shot script created by createSAcreds.ps1 # Runs RubrikSecurityCloud command to create service-account file # Start transcript for detailed logging @@ -134,7 +134,7 @@ Try { Exit 4 } } Catch { - Write-Error "Error creating RBK service-account file: `$(`$_.Exception.Message)" + Write-Error "Error creating Rubrik service-account file: `$(`$_.Exception.Message)" Write-Error "Full exception: `$(`$_.Exception | Format-List * | Out-String)" Write-Error "Stack trace: `$(`$_.ScriptStackTrace)" Stop-Transcript @@ -156,10 +156,10 @@ Try { # Construct the UserId based on account type if ($AccountType -eq 'gMSA') { $userId = if ([string]::IsNullOrWhiteSpace($Domain)) { "$AccountName`$" } else { "$Domain\$AccountName`$" } - $logonType = 'Password' # For gMSA, use Password logon type + $logonType = 'Password' # needed even though gMSA uses AD for auth } else { $userId = if ([string]::IsNullOrWhiteSpace($Domain)) { $AccountName } else { "$Domain\$AccountName" } - $logonType = 'Password' # For regular service accounts, use Password logon type + $logonType = 'Password' } # Action: run PowerShell to execute the one-shot script with output redirection @@ -255,7 +255,7 @@ Try { Write-Warning "Log file not found at: $logFile" } - throw "Scheduled task finished with non-zero LastTaskResult: $lastResult. Check Event Viewer > Applications and Services Logs > Microsoft > Windows > TaskScheduler for details, or review the log output above." + throw "Scheduled task finished with non-zero LastTaskResult: $lastResult." } # ---- Cleanup ----