flnxi-Wu/scripts/tools/download-backup.ps1
WuYongJie d1c6efc79d
Some checks failed
CI Check / check (push) Has been cancelled
feat: NAS 部署与 Harbor 推送流程
2025-12-03 14:39:06 +08:00

98 lines
2.9 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# PostgreSQL 备份下载脚本(Windows PowerShell)
# 使用方法:.\download-backup.ps1
param(
[string]$ServerIP = "192.168.58.20",
[string]$ServerUser = "root",
[string]$RemoteFile = "/tmp/backup.sql",
[string]$LocalDir = ".\backups"
)
Write-Host "=========================================="
Write-Host " PostgreSQL 备份下载工具"
Write-Host "=========================================="
Write-Host ""
# 创建本地备份目录
if (!(Test-Path $LocalDir)) {
New-Item -ItemType Directory -Path $LocalDir | Out-Null
Write-Host "✅ 创建本地目录: $LocalDir"
}
# 生成带时间戳的文件名
$Timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$LocalFile = Join-Path $LocalDir "lobechat_backup_$Timestamp.sql"
Write-Host "[1/4] 检查远程文件..."
$CheckCmd = "ssh ${ServerUser}@${ServerIP} 'test -f $RemoteFile && echo EXISTS || echo NOT_FOUND'"
$FileExists = Invoke-Expression $CheckCmd 2>$null
if ($FileExists -match "NOT_FOUND") {
Write-Host "❌ 远程文件不存在: $RemoteFile"
Write-Host ""
Write-Host "请先在服务器上导出数据:"
Write-Host " ssh ${ServerUser}@${ServerIP}"
Write-Host " docker exec -u root -e PGPASSWORD='gongxiangpan123' flnxi-material \"
Write-Host " pg_dump -h 192.168.58.20 -U gongxiangpan -d lobechat > /tmp/backup.sql"
exit 1
}
Write-Host "✅ 远程文件存在"
Write-Host ""
Write-Host "[2/4] 获取文件大小..."
$SizeCmd = "ssh ${ServerUser}@${ServerIP} 'ls -lh $RemoteFile | awk ''{print `$5}''"
$FileSize = Invoke-Expression $SizeCmd
Write-Host "✅ 文件大小: $FileSize"
Write-Host ""
Write-Host "[3/4] 下载文件..."
Write-Host " 从: ${ServerUser}@${ServerIP}:${RemoteFile}"
Write-Host " 到: $LocalFile"
scp "${ServerUser}@${ServerIP}:${RemoteFile}" $LocalFile
if ($LASTEXITCODE -eq 0) {
Write-Host "✅ 下载成功"
} else {
Write-Host "❌ 下载失败"
exit 1
}
Write-Host ""
Write-Host "[4/4] 验证文件..."
if (Test-Path $LocalFile) {
$LocalSize = (Get-Item $LocalFile).Length
$LocalSizeStr = "{0:N2} MB" -f ($LocalSize / 1MB)
Write-Host "✅ 文件已保存"
Write-Host " 路径: $LocalFile"
Write-Host " 大小: $LocalSizeStr"
} else {
Write-Host "❌ 文件验证失败"
exit 1
}
Write-Host ""
Write-Host "=========================================="
Write-Host " ✅ 下载完成!"
Write-Host "=========================================="
Write-Host ""
Write-Host "📁 备份文件: $LocalFile"
Write-Host ""
Write-Host "📊 查看备份内容:"
Write-Host " Get-Content $LocalFile -Head 50"
Write-Host ""
Write-Host "🔄 恢复数据库:"
Write-Host " psql -h 服务器IP -U gongxiangpan -d lobechat -f $LocalFile"
Write-Host ""
# 列出所有备份文件
Write-Host "📋 本地备份文件:"
Get-ChildItem $LocalDir -Filter "*.sql" | ForEach-Object {
$size = "{0:N2} MB" -f ($_.Length / 1MB)
Write-Host " $($_.Name) ($size)"
}
Write-Host ""