Skip to content

Commit ec32fd4

Browse files
Enhance MSSQL_Backup_Purge.ps1: Add IncludeDatabases parameter for selective purging and update documentation
1 parent ee9e332 commit ec32fd4

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

DBA/Backup/MSSQL_Backup_Purge.ps1

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
.DESCRIPTION
55
Removes backup files older than a specified retention period for all databases of an instance.
66
Only deletes files matching the specified backup type extension (.bak for Full/Diff, .trn for Log).
7-
Supports exclusion of databases by exact name or SQL LIKE patterns (e.g. %_NOBACKUP, TEST%).
7+
Supports inclusion and exclusion of databases by exact name or SQL LIKE patterns (e.g. %_NOBACKUP, TEST%).
88
99
.PARAMETER SqlInstance
1010
The SQL Server instance whose backup files should be purged.
@@ -23,6 +23,12 @@
2323
.PARAMETER RetentionHours
2424
Retention period in hours. Files older than this will be deleted.
2525
26+
.PARAMETER IncludeDatabases
27+
Array of database names or SQL LIKE patterns to include in purge.
28+
If specified, ONLY databases matching these patterns will be purged.
29+
Exact names (e.g. "MyDB") and patterns using % and _ wildcards are supported.
30+
Example: @("PROD%", "MyDB", "%_CRITICAL")
31+
2632
.PARAMETER ExcludeDatabases
2733
Array of database names or SQL LIKE patterns to exclude from purge.
2834
Exact names (e.g. "MyDB") and patterns using % and _ wildcards are supported.
@@ -66,6 +72,7 @@
6672
[Parameter(Mandatory)] [ValidateSet('Full','Diff','Log','All')] [string] $BackupType,
6773
[Parameter(Mandatory)] [string] $BackupDirectory,
6874
[Parameter(Mandatory)] [Int32] $RetentionHours,
75+
[Parameter()] [string[]] $IncludeDatabases = @(),
6976
[Parameter()] [string[]] $ExcludeDatabases = @(),
7077
[Parameter()] [switch] $WhatIf,
7178
[Parameter()] [string] $LogLevel = "INFO",
@@ -83,12 +90,12 @@
8390
return "^${escaped}$"
8491
}
8592

86-
function Test-DatabaseExcluded {
93+
function Test-DatabaseMatchesPattern {
8794
param(
8895
[string]$DatabaseName,
89-
[string[]]$ExcludePatterns
96+
[string[]]$Patterns
9097
)
91-
foreach ($pattern in $ExcludePatterns) {
98+
foreach ($pattern in $Patterns) {
9299
if ($pattern -match '[%_]') {
93100
$regex = Convert-SqlLikeToRegex -Pattern $pattern
94101
if ($DatabaseName -match $regex) { return $true }
@@ -127,6 +134,7 @@
127134
Write-Log -Level INFO -Message "Parameter BackupType : ${BackupType}"
128135
Write-Log -Level INFO -Message "Parameter BackupDirectory : ${BackupDirectory}"
129136
Write-Log -Level INFO -Message "Parameter RetentionHours : ${RetentionHours}"
137+
Write-Log -Level INFO -Message "Parameter IncludeDatabases : $($IncludeDatabases -join ', ')"
130138
Write-Log -Level INFO -Message "Parameter ExcludeDatabases : $($ExcludeDatabases -join ', ')"
131139

132140
# Determine which extensions and subfolder patterns to target
@@ -180,8 +188,16 @@
180188
foreach ($group in $groupedFiles) {
181189
$dbName = $group.Name
182190

191+
# Check inclusion filter
192+
if ($IncludeDatabases.Count -gt 0 -and -not (Test-DatabaseMatchesPattern -DatabaseName $dbName -Patterns $IncludeDatabases)) {
193+
$skippedSizeMB = [math]::Round(($group.Group | Measure-Object -Property Length -Sum).Sum / 1MB, 2)
194+
Write-Log -Level INFO -Message "SKIPPED (not included): ${dbName} - $($group.Count) file(s), ${skippedSizeMB} MB"
195+
$totalSkippedCount += $group.Count
196+
continue
197+
}
198+
183199
# Check exclusion
184-
if ($ExcludeDatabases.Count -gt 0 -and (Test-DatabaseExcluded -DatabaseName $dbName -ExcludePatterns $ExcludeDatabases)) {
200+
if ($ExcludeDatabases.Count -gt 0 -and (Test-DatabaseMatchesPattern -DatabaseName $dbName -Patterns $ExcludeDatabases)) {
185201
$skippedSizeMB = [math]::Round(($group.Group | Measure-Object -Property Length -Sum).Sum / 1MB, 2)
186202
Write-Log -Level INFO -Message "SKIPPED (excluded): ${dbName} - $($group.Count) file(s), ${skippedSizeMB} MB"
187203
$totalSkippedCount += $group.Count
@@ -217,12 +233,16 @@
217233
$wouldDeleteCount = ($allFiles | Where-Object {
218234
$parts = $_.FullName.Replace($BackupDirectory, '').TrimStart('\').Split('\')
219235
$dbName = if ($parts.Count -ge 3) { $parts[2] } else { "UNKNOWN" }
220-
-not (Test-DatabaseExcluded -DatabaseName $dbName -ExcludePatterns $ExcludeDatabases)
236+
$included = ($IncludeDatabases.Count -eq 0) -or (Test-DatabaseMatchesPattern -DatabaseName $dbName -Patterns $IncludeDatabases)
237+
$excluded = ($ExcludeDatabases.Count -gt 0) -and (Test-DatabaseMatchesPattern -DatabaseName $dbName -Patterns $ExcludeDatabases)
238+
$included -and -not $excluded
221239
}).Count
222240
$wouldDeleteSizeMB = [math]::Round(($allFiles | Where-Object {
223241
$parts = $_.FullName.Replace($BackupDirectory, '').TrimStart('\').Split('\')
224242
$dbName = if ($parts.Count -ge 3) { $parts[2] } else { "UNKNOWN" }
225-
-not (Test-DatabaseExcluded -DatabaseName $dbName -ExcludePatterns $ExcludeDatabases)
243+
$included = ($IncludeDatabases.Count -eq 0) -or (Test-DatabaseMatchesPattern -DatabaseName $dbName -Patterns $IncludeDatabases)
244+
$excluded = ($ExcludeDatabases.Count -gt 0) -and (Test-DatabaseMatchesPattern -DatabaseName $dbName -Patterns $ExcludeDatabases)
245+
$included -and -not $excluded
226246
} | Measure-Object -Property Length -Sum).Sum / 1MB, 2)
227247
Write-Host ""
228248
Write-Host "=== WhatIf Summary ===" -ForegroundColor Cyan

0 commit comments

Comments
 (0)