|
| 1 | +function Start-BackupRetentionCleanup { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Start the Backup Retention Cleanup Timer |
| 5 | + .DESCRIPTION |
| 6 | + This function cleans up old CIPP and Tenant backups based on the retention policy |
| 7 | + #> |
| 8 | + [CmdletBinding(SupportsShouldProcess = $true)] |
| 9 | + param() |
| 10 | + |
| 11 | + try { |
| 12 | + # Get retention settings |
| 13 | + $ConfigTable = Get-CippTable -tablename Config |
| 14 | + $Filter = "PartitionKey eq 'BackupRetention' and RowKey eq 'Settings'" |
| 15 | + $RetentionSettings = Get-CIPPAzDataTableEntity @ConfigTable -Filter $Filter |
| 16 | + |
| 17 | + # Default to 30 days if not set |
| 18 | + $RetentionDays = if ($RetentionSettings.RetentionDays) { |
| 19 | + [int]$RetentionSettings.RetentionDays |
| 20 | + } else { |
| 21 | + 30 |
| 22 | + } |
| 23 | + |
| 24 | + # Ensure minimum retention of 7 days |
| 25 | + if ($RetentionDays -lt 7) { |
| 26 | + $RetentionDays = 7 |
| 27 | + } |
| 28 | + |
| 29 | + Write-Host "Starting backup cleanup with retention of $RetentionDays days" |
| 30 | + |
| 31 | + # Calculate cutoff date |
| 32 | + $CutoffDate = (Get-Date).AddDays(-$RetentionDays).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ') |
| 33 | + |
| 34 | + $DeletedCounts = [System.Collections.Generic.List[int]]::new() |
| 35 | + |
| 36 | + # Clean up CIPP Backups |
| 37 | + if ($PSCmdlet.ShouldProcess('CIPPBackup', 'Cleaning up old backups')) { |
| 38 | + $CIPPBackupTable = Get-CippTable -tablename 'CIPPBackup' |
| 39 | + $Filter = "PartitionKey eq 'CIPPBackup' and Timestamp lt datetime'$CutoffDate'" |
| 40 | + |
| 41 | + $OldCIPPBackups = Get-AzDataTableEntity @CIPPBackupTable -Filter $Filter -Property @('PartitionKey', 'RowKey', 'ETag') |
| 42 | + |
| 43 | + if ($OldCIPPBackups) { |
| 44 | + Write-Host "Found $($OldCIPPBackups.Count) old CIPP backups to delete" |
| 45 | + Remove-AzDataTableEntity @CIPPBackupTable -Entity $OldCIPPBackups -Force |
| 46 | + $DeletedCounts.Add($OldCIPPBackups.Count) |
| 47 | + Write-LogMessage -API 'BackupRetentionCleanup' -message "Deleted $($OldCIPPBackups.Count) old CIPP backups" -Sev 'Info' |
| 48 | + } else { |
| 49 | + Write-Host 'No old CIPP backups found' |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + # Clean up Scheduled/Tenant Backups |
| 54 | + if ($PSCmdlet.ShouldProcess('ScheduledBackup', 'Cleaning up old backups')) { |
| 55 | + $ScheduledBackupTable = Get-CippTable -tablename 'ScheduledBackup' |
| 56 | + $Filter = "PartitionKey eq 'ScheduledBackup' and Timestamp lt datetime'$CutoffDate'" |
| 57 | + |
| 58 | + $OldScheduledBackups = Get-AzDataTableEntity @ScheduledBackupTable -Filter $Filter -Property @('PartitionKey', 'RowKey', 'ETag') |
| 59 | + |
| 60 | + if ($OldScheduledBackups) { |
| 61 | + Write-Host "Found $($OldScheduledBackups.Count) old tenant backups to delete" |
| 62 | + Remove-AzDataTableEntity @ScheduledBackupTable -Entity $OldScheduledBackups -Force |
| 63 | + $DeletedCounts.Add($OldScheduledBackups.Count) |
| 64 | + Write-LogMessage -API 'BackupRetentionCleanup' -message "Deleted $($OldScheduledBackups.Count) old tenant backups" -Sev 'Info' |
| 65 | + } else { |
| 66 | + Write-Host 'No old tenant backups found' |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + $TotalDeleted = ($DeletedCounts | Measure-Object -Sum).Sum |
| 71 | + Write-LogMessage -API 'BackupRetentionCleanup' -message "Backup cleanup completed. Total backups deleted: $TotalDeleted (retention: $RetentionDays days)" -Sev 'Info' |
| 72 | + |
| 73 | + } catch { |
| 74 | + $ErrorMessage = Get-CippException -Exception $_ |
| 75 | + Write-LogMessage -API 'BackupRetentionCleanup' -message "Failed to run backup cleanup: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage |
| 76 | + throw |
| 77 | + } |
| 78 | +} |
0 commit comments