|
113 | 113 | adds them as @ExcludeDatabases to the Ola DatabaseBackup command in the job step. |
114 | 114 | If the table does not exist or is empty, the Databases parameter is used unchanged. |
115 | 115 |
|
| 116 | +.PARAMETER CreateSyncJob |
| 117 | + When -UseExcludeTable is set, automatically creates a SQL Agent job that runs |
| 118 | + Sync-sqmBackupExcludeTable every 30 minutes via pwsh CmdExec step. |
| 119 | + Ensures IsActive changes (made via Show-sqmBackupExcludeForm) are propagated |
| 120 | + to all AG secondaries without manual intervention. |
| 121 | + Default: $true. Set to $false to suppress job creation. |
| 122 | +
|
116 | 123 | .PARAMETER EnableException |
117 | 124 | Throw exceptions immediately. |
118 | 125 |
|
@@ -262,6 +269,8 @@ function New-sqmOlaUsrDbBackupJob |
262 | 269 | [Parameter(Mandatory = $false)] |
263 | 270 | [switch]$UseExcludeTable, |
264 | 271 | [Parameter(Mandatory = $false)] |
| 272 | + [bool]$CreateSyncJob = $true, |
| 273 | + [Parameter(Mandatory = $false)] |
265 | 274 | [switch]$SkipAlwaysOnPropagation, |
266 | 275 | [Parameter(Mandatory = $false)] |
267 | 276 | [switch]$EnableException |
@@ -291,6 +300,7 @@ function New-sqmOlaUsrDbBackupJob |
291 | 300 | $effFullJobName = if ($FullJobName) { $FullJobName } elseif ($cfg['OlaJobNameFull']) { $cfg['OlaJobNameFull'] } else { 'OlaHH-UserDatabases-FULL' } |
292 | 301 | $effDiffJobName = if ($DiffJobName) { $DiffJobName } elseif ($cfg['OlaJobNameDiff']) { $cfg['OlaJobNameDiff'] } else { 'OlaHH-UserDatabases-DIFF' } |
293 | 302 | $effLogJobName = if ($LogJobName) { $LogJobName } elseif ($cfg['OlaJobNameLog']) { $cfg['OlaJobNameLog'] } else { 'OlaHH-UserDatabases-LOG' } |
| 303 | + $syncJobName = if ($effFullJobName -like 'FITS *') { 'FITS BackupExclude - SYNC' } else { 'sqm BackupExclude - SYNC' } |
294 | 304 |
|
295 | 305 | $connParams = @{ SqlInstance = $SqlInstance } |
296 | 306 | if ($SqlCredential) { $connParams['SqlCredential'] = $SqlCredential } |
@@ -729,8 +739,92 @@ EXECUTE master.dbo.DatabaseBackup |
729 | 739 |
|
730 | 740 | $results.Add($result) |
731 | 741 | } |
732 | | - |
733 | | - # 7. Konfigurationsbericht schreiben |
| 742 | + |
| 743 | + # 7. Sync-Job fuer sqm_BackupExclude anlegen (nur bei -UseExcludeTable -CreateSyncJob) |
| 744 | + if ($UseExcludeTable -and $CreateSyncJob) |
| 745 | + { |
| 746 | + try |
| 747 | + { |
| 748 | + $syncJobExists = Get-DbaAgentJob @connParams -Job $syncJobName -ErrorAction SilentlyContinue |
| 749 | + if ($syncJobExists -and -not $Update) |
| 750 | + { |
| 751 | + Invoke-sqmLogging -Message "Sync-Job '$syncJobName' existiert bereits (kein -Update — unveraendert)." -FunctionName $functionName -Level "INFO" |
| 752 | + } |
| 753 | + elseif ($PSCmdlet.ShouldProcess($SqlInstance, "Erstelle Sync-Job '$syncJobName' (alle 30 Min.)")) |
| 754 | + { |
| 755 | + $syncStepCmd = "pwsh -NonInteractive -NoProfile -Command `"& { Import-Module sqmSQLTool; Sync-sqmBackupExcludeTable -SqlInstance '.' }`"" |
| 756 | + |
| 757 | + if (-not $syncJobExists) |
| 758 | + { |
| 759 | + New-DbaAgentJob @connParams ` |
| 760 | + -Job $syncJobName ` |
| 761 | + -Category $JobCategory ` |
| 762 | + -OwnerLogin $saLogin ` |
| 763 | + -Description "sqmSQLTool: Synchronisiert master.dbo.sqm_BackupExclude und propagiert Aenderungen auf AG-Secondaries." ` |
| 764 | + -ErrorAction Stop | Out-Null |
| 765 | + } |
| 766 | + |
| 767 | + $existingStep = if ($syncJobExists) { $syncJobExists.JobSteps | Where-Object { $_.ID -eq 1 } } else { $null } |
| 768 | + if ($existingStep) |
| 769 | + { |
| 770 | + Set-DbaAgentJobStep @connParams -Job $syncJobName -StepId 1 -Command $syncStepCmd -SubSystem CmdExec -ErrorAction Stop | Out-Null |
| 771 | + } |
| 772 | + else |
| 773 | + { |
| 774 | + New-DbaAgentJobStep @connParams -Job $syncJobName -StepId 1 -StepName 'Sync sqm_BackupExclude' -Command $syncStepCmd -SubSystem CmdExec -ErrorAction Stop | Out-Null |
| 775 | + } |
| 776 | + |
| 777 | + if (-not $syncJobExists) |
| 778 | + { |
| 779 | + New-DbaAgentSchedule @connParams ` |
| 780 | + -Job $syncJobName ` |
| 781 | + -Schedule "${syncJobName}_30min" ` |
| 782 | + -FrequencyType Daily ` |
| 783 | + -FrequencyInterval 1 ` |
| 784 | + -FrequencySubdayType Minutes ` |
| 785 | + -FrequencySubdayInterval 30 ` |
| 786 | + -StartTime '000000' ` |
| 787 | + -Force ` |
| 788 | + -ErrorAction Stop | Out-Null |
| 789 | + } |
| 790 | + |
| 791 | + $syncAction = if ($syncJobExists) { 'aktualisiert' } else { 'erstellt' } |
| 792 | + $syncMsg = "Sync-Job '$syncJobName' $syncAction — laeuft taeglich alle 30 Minuten." |
| 793 | + Invoke-sqmLogging -Message $syncMsg -FunctionName $functionName -Level "INFO" |
| 794 | + $results.Add([PSCustomObject]@{ |
| 795 | + SqlInstance = $SqlInstance |
| 796 | + BackupType = 'SYNC' |
| 797 | + JobName = $syncJobName |
| 798 | + BackupDirectory = $null |
| 799 | + Databases = $null |
| 800 | + ScheduleTime = '00:00' |
| 801 | + ScheduleDays = 'EveryDay (alle 30 Min.)' |
| 802 | + JobStatus = if ($syncJobExists) { 'Updated' } else { 'Created' } |
| 803 | + OverallStatus = 'Success' |
| 804 | + Message = $syncMsg |
| 805 | + }) |
| 806 | + } |
| 807 | + } |
| 808 | + catch |
| 809 | + { |
| 810 | + $errMsg = "Sync-Job '$syncJobName' konnte nicht erstellt werden: $($_.Exception.Message)" |
| 811 | + Invoke-sqmLogging -Message $errMsg -FunctionName $functionName -Level "WARNING" |
| 812 | + $results.Add([PSCustomObject]@{ |
| 813 | + SqlInstance = $SqlInstance |
| 814 | + BackupType = 'SYNC' |
| 815 | + JobName = $syncJobName |
| 816 | + BackupDirectory = $null |
| 817 | + Databases = $null |
| 818 | + ScheduleTime = $null |
| 819 | + ScheduleDays = $null |
| 820 | + JobStatus = 'Failed' |
| 821 | + OverallStatus = 'Warning' |
| 822 | + Message = $errMsg |
| 823 | + }) |
| 824 | + } |
| 825 | + } |
| 826 | + |
| 827 | + # 8. Konfigurationsbericht schreiben |
734 | 828 | if (-not (Test-Path $maintenanceLogDir)) { New-Item -ItemType Directory -Path $maintenanceLogDir -Force | Out-Null } |
735 | 829 | $safeInst = $SqlInstance -replace '[\\/:*?"<>|]', '_' |
736 | 830 | $datestamp = Get-Date -Format 'yyyy-MM-dd' |
@@ -799,6 +893,7 @@ EXECUTE master.dbo.DatabaseBackup |
799 | 893 | } |
800 | 894 | if ($SqlCredential) { $secParams['SqlCredential'] = $SqlCredential } |
801 | 895 | if ($UseExcludeTable) { $secParams['UseExcludeTable'] = $true } |
| 896 | + $secParams['CreateSyncJob'] = $CreateSyncJob |
802 | 897 | if ($OperatorName) { $secParams['OperatorName'] = $OperatorName } |
803 | 898 | if ($BackupDirectory) { $secParams['BackupDirectory'] = $BackupDirectory } |
804 | 899 |
|
|
0 commit comments