# ============================================ # 数据库备份测试脚本 (PowerShell) # ============================================ $ErrorActionPreference = "Stop" Write-Host "========================================" Write-Host " 测试数据库备份功能" Write-Host "========================================" Write-Host "" # 测试目录 $TestBackupDir = ".\backups\test" # 清理测试目录 if (Test-Path $TestBackupDir) { Write-Host "清理旧的测试目录..." Remove-Item -Path $TestBackupDir -Recurse -Force } # 执行备份 Write-Host "执行备份测试..." & .\scripts\backup-db.ps1 -BackupDir $TestBackupDir # 验证结果 Write-Host "" Write-Host "========================================" Write-Host " 验证测试结果" Write-Host "========================================" Write-Host "" # 检查备份文件 $backupFiles = Get-ChildItem -Path $TestBackupDir -Filter "db-backup-*.sql.gz" -ErrorAction SilentlyContinue if ($backupFiles.Count -eq 1) { Write-Host "✅ 备份文件已创建" -ForegroundColor Green } else { Write-Host "❌ 备份文件创建失败" -ForegroundColor Red exit 1 } # 检查 checksum 文件 $checksumFiles = Get-ChildItem -Path $TestBackupDir -Filter "db-backup-*.sql.gz.sha256" -ErrorAction SilentlyContinue if ($checksumFiles.Count -eq 1) { Write-Host "✅ Checksum 文件已创建" -ForegroundColor Green } else { Write-Host "❌ Checksum 文件创建失败" -ForegroundColor Red exit 1 } # 检查摘要文件 $summaryFiles = Get-ChildItem -Path $TestBackupDir -Filter "backup-summary-*.json" -ErrorAction SilentlyContinue if ($summaryFiles.Count -eq 1) { Write-Host "✅ 摘要文件已创建" -ForegroundColor Green } else { Write-Host "❌ 摘要文件创建失败" -ForegroundColor Red exit 1 } # 检查日志文件 $logFiles = Get-ChildItem -Path $TestBackupDir -Filter "backup-db-*.log" -ErrorAction SilentlyContinue if ($logFiles.Count -eq 1) { Write-Host "✅ 日志文件已创建" -ForegroundColor Green } else { Write-Host "❌ 日志文件创建失败" -ForegroundColor Red exit 1 } Write-Host "" Write-Host "========================================" Write-Host " 🎉 所有测试通过!" -ForegroundColor Green Write-Host "========================================" Write-Host "" # 显示备份文件信息 Write-Host "备份文件列表:" Get-ChildItem -Path $TestBackupDir -Filter "db-backup-*.sql.gz" | Format-Table Name, Length, LastWriteTime Write-Host "" Write-Host "提示:测试完成后可以删除测试目录:" Write-Host " Remove-Item -Path $TestBackupDir -Recurse -Force" Write-Host ""