# ============================================ # 完整系统备份脚本 (PowerShell) # ============================================ # 功能: # - 备份 PostgreSQL 数据库 # - 备份 MinIO 文件存储 # - 备份关键配置文件 (.env, docker-compose*.yml, prisma/schema.prisma) # - 生成本次完整备份的清单 (manifest) # - 自动清理过期备份(默认保留 7 天) # 使用: # .\scripts\backup-all.ps1 [-BackupRoot ".\backups\full"] [-RetentionDays 7] # ============================================ param( [string]$BackupRoot = ".\backups\full", [int]$RetentionDays = 5 ) $ErrorActionPreference = "Stop" $Timestamp = Get-Date -Format "yyyyMMdd-HHmmss" $BackupDir = Join-Path $BackupRoot $Timestamp $databaseDir = Join-Path $BackupDir "database" $filesDir = Join-Path $BackupDir "files" $configDir = Join-Path $BackupDir "config" # 创建目录结构 New-Item -ItemType Directory -Path $databaseDir -Force | Out-Null New-Item -ItemType Directory -Path $filesDir -Force | Out-Null New-Item -ItemType Directory -Path $configDir -Force | Out-Null Write-Host "========================================" Write-Host " 完整系统备份脚本" Write-Host "========================================" Write-Host "备份根目录: $BackupRoot" Write-Host "本次备份目录: $BackupDir" Write-Host "" # 1. 备份数据库 & .\scripts\backup-db.ps1 -BackupDir $databaseDir # 2. 备份文件存储 (MinIO) & .\scripts\backup-files.ps1 -BackupDir $filesDir # 3. 备份关键配置文件 # .env if (Test-Path ".env") { Copy-Item ".env" (Join-Path $configDir ".env") -Force } # docker-compose*.yml Get-ChildItem -Path . -Filter "docker-compose*.yml" -ErrorAction SilentlyContinue | ForEach-Object { Copy-Item $_.FullName (Join-Path $configDir $_.Name) -Force } # prisma/schema.prisma if (Test-Path "prisma\schema.prisma") { $prismaTarget = Join-Path $configDir "prisma" New-Item -ItemType Directory -Path $prismaTarget -Force | Out-Null Copy-Item "prisma\schema.prisma" (Join-Path $prismaTarget "schema.prisma") -Force } # 4. 生成清单文件 $dbBackup = Get-ChildItem -Path $databaseDir -Filter "db-backup-*.sql.gz" -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -First 1 $filesBackup = Get-ChildItem -Path $filesDir -Filter "files-backup-*.tar.gz" -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -First 1 $manifest = [ordered]@{ timestamp = (Get-Date -Format "o") root = $BackupDir components = [ordered]@{ database = $dbBackup.Name files = $filesBackup.Name config = @( ".env", "docker-compose*.yml", "prisma/schema.prisma" ) } } $manifestPath = Join-Path $BackupDir "backup-manifest.json" $manifest | ConvertTo-Json -Depth 4 | Out-File -FilePath $manifestPath -Encoding UTF8 Write-Host "" Write-Host "========================================" Write-Host " 🎉 完整备份完成" Write-Host "========================================" Write-Host "备份目录: $BackupDir" Write-Host "清单文件: $manifestPath" Write-Host "========================================" Write-Host "" # 5. 清理过期备份 Write-Host "检查过期备份(保留 $RetentionDays 天)..." $cutoffDate = (Get-Date).AddDays(-$RetentionDays) $deletedCount = 0 if (Test-Path $BackupRoot) { Get-ChildItem -Path $BackupRoot -Directory | ForEach-Object { # 解析目录名中的时间戳 (格式: yyyyMMdd-HHmmss) $dirName = $_.Name if ($dirName -match "^(\d{8})-(\d{6})$") { try { $dirDate = [DateTime]::ParseExact($dirName, "yyyyMMdd-HHmmss", $null) if ($dirDate -lt $cutoffDate) { Write-Host " 删除过期备份: $dirName" Remove-Item -Path $_.FullName -Recurse -Force $deletedCount++ } } catch { # 无法解析日期,跳过 } } } } if ($deletedCount -gt 0) { Write-Host "已删除 $deletedCount 个过期备份" } else { Write-Host "没有过期备份需要删除" } # 6. 记录备份日志 $logDir = Join-Path $BackupRoot "logs" if (-not (Test-Path $logDir)) { New-Item -ItemType Directory -Path $logDir -Force | Out-Null } $logEntry = [ordered]@{ timestamp = (Get-Date -Format "o") backupDir = $BackupDir retentionDays = $RetentionDays deletedBackups = $deletedCount status = "success" } $logFile = Join-Path $logDir "backup-log.jsonl" $logEntry | ConvertTo-Json -Compress | Add-Content -Path $logFile -Encoding UTF8 Write-Host "" Write-Host "备份日志已记录到: $logFile" Write-Host ""