fix(Update-KHDB): cap KHDB backup retention
Every successful update created a new khdb.txt.bak-<timestamp> with no cleanup anywhere, so a recurring scheduled job accumulated one full-size backup per run indefinitely. Now keeps only the 5 most recent backups, pruning older ones after each new backup is created. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -773,6 +773,15 @@ function Update-KHDB {
|
|||||||
$backupPath = Join-Path -Path $installPath -ChildPath ("$khdbName.bak-$ts")
|
$backupPath = Join-Path -Path $installPath -ChildPath ("$khdbName.bak-$ts")
|
||||||
Copy-Item -LiteralPath $combinedTarget -Destination $backupPath -Force
|
Copy-Item -LiteralPath $combinedTarget -Destination $backupPath -Force
|
||||||
Write-Host ("Existing KHDB backed up to {0}" -f $backupPath)
|
Write-Host ("Existing KHDB backed up to {0}" -f $backupPath)
|
||||||
|
|
||||||
|
# Each run adds one full-size backup; keep only the most recent few so a recurring
|
||||||
|
# scheduled job doesn't silently accumulate backups until the disk fills up.
|
||||||
|
$backupRetentionCount = 5
|
||||||
|
$staleBackups = Get-ChildItem -LiteralPath $installPath -Filter "$khdbName.bak-*" -File -ErrorAction SilentlyContinue |
|
||||||
|
Sort-Object Name -Descending | Select-Object -Skip $backupRetentionCount
|
||||||
|
foreach ($stale in $staleBackups) {
|
||||||
|
try { Remove-Item -LiteralPath $stale.FullName -Force } catch { Write-Warning "Could not remove old backup '$($stale.FullName)': $($_.Exception.Message)" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Move-Item -LiteralPath $combinedTemp -Destination $combinedTarget -Force
|
Move-Item -LiteralPath $combinedTemp -Destination $combinedTarget -Force
|
||||||
|
|||||||
Reference in New Issue
Block a user