Private/Add-BackupToManifest.ps1
function Add-BackupToManifest { <# .SYNOPSIS Adds backup information to a consolidated daily manifest file. .DESCRIPTION Creates or updates a daily backup manifest (backup-manifest.json) containing metadata for all backups created on a specific date. This consolidated approach reduces file clutter while maintaining full restoration capabilities. .PARAMETER SourcePath The original path that was backed up. .PARAMETER BackupPath The path to the created backup archive (without .zip extension). .PARAMETER PathType The type of the source path ('File' or 'Directory'). .PARAMETER DatePath The date-organized backup directory path (e.g., /backups/2025-09-15). .OUTPUTS None. Creates or updates backup-manifest.json in the date directory. .NOTES This function replaces individual .metadata.json files with a single consolidated manifest per backup date, dramatically reducing file clutter. .EXAMPLE PS > Add-BackupToManifest -SourcePath 'C:\Documents\report.pdf' -BackupPath 'C:\Backups\2025-09-15\Documents__report.pdf' -PathType 'File' -DatePath 'C:\Backups\2025-09-15' Adds backup entry to C:\Backups\2025-09-15\backup-manifest.json #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [string] $SourcePath, [Parameter(Mandatory = $true)] [string] $BackupPath, [Parameter(Mandatory = $true)] [ValidateSet('File', 'Directory')] [string] $PathType, [Parameter(Mandatory = $true)] [string] $DatePath ) try { $manifestPath = Join-Path $DatePath 'backup-manifest.json' $archiveName = [System.IO.Path]::GetFileName($BackupPath) + '.zip' # Get module version dynamically $moduleInfo = Get-Module -Name DailyBackup $moduleVersion = if ($moduleInfo) { $moduleInfo.Version.ToString() } else { '1.5.3' } # Create backup entry $backupEntry = @{ ArchiveName = $archiveName SourcePath = $SourcePath PathType = $PathType BackupCreated = Get-Date -Format 'yyyy-MM-ddTHH:mm:ss.fffZ' } # Add file/directory specific metadata if (Test-Path -Path $SourcePath) { $item = Get-Item -Path $SourcePath $backupEntry.OriginalName = $item.Name $backupEntry.LastWriteTime = $item.LastWriteTime.ToString('yyyy-MM-ddTHH:mm:ss.fffZ') $backupEntry.Attributes = $item.Attributes.ToString() if ($PathType -eq 'File') { $backupEntry.Size = $item.Length $backupEntry.Extension = $item.Extension } } # Load existing manifest or create new one $manifest = if (Test-Path $manifestPath) { try { Get-Content $manifestPath -Raw | ConvertFrom-Json } catch { Write-Warning "New-DailyBackup:Add-BackupToManifest> Failed to read existing manifest, creating new one: $_" $null } } else { $null } # Create new manifest structure if needed if (-not $manifest) { $backupDate = Split-Path $DatePath -Leaf $manifest = @{ BackupDate = $backupDate BackupVersion = '1.0' ModuleVersion = $moduleVersion Backups = @() } } # Add new backup entry $manifest.Backups += $backupEntry # Save updated manifest $manifest | ConvertTo-Json -Depth 4 | Out-File -FilePath $manifestPath -Encoding UTF8 Write-Verbose "New-DailyBackup:Add-BackupToManifest> Added backup to manifest: $manifestPath" } catch { Write-Warning "New-DailyBackup:Add-BackupToManifest> Failed to update manifest for $SourcePath : $_" } } |