flnxi-Wu/scripts/tools/download-all-backups.ps1
WuYongJie f646b71813
Some checks failed
CI Check / check (push) Has been cancelled
chore: 初始化本地仓库并同步最近改动
2025-11-18 17:50:16 +08:00

66 lines
1.8 KiB
PowerShell

# 批量下载所有备份文件
param(
[string]$ServerIP = "192.168.58.20",
[string]$ServerUser = "root",
[string]$RemoteDir = "/tmp/db_backups",
[string]$LocalDir = ".\backups"
)
Write-Host "=========================================="
Write-Host " 批量下载备份文件"
Write-Host "=========================================="
Write-Host ""
# 创建本地目录
if (!(Test-Path $LocalDir)) {
New-Item -ItemType Directory -Path $LocalDir | Out-Null
}
Write-Host "[1/3] 获取远程备份文件列表..."
$ListCmd = "ssh ${ServerUser}@${ServerIP} 'ls -1 $RemoteDir/*.sql* 2>/dev/null || ls -1 /tmp/*.sql* 2>/dev/null'"
$RemoteFiles = Invoke-Expression $ListCmd
if (!$RemoteFiles) {
Write-Host "❌ 未找到备份文件"
exit 1
}
$FileCount = ($RemoteFiles | Measure-Object).Count
Write-Host "✅ 找到 $FileCount 个备份文件"
Write-Host ""
Write-Host "[2/3] 下载文件..."
$Downloaded = 0
foreach ($RemoteFile in $RemoteFiles) {
$FileName = Split-Path $RemoteFile -Leaf
$LocalFile = Join-Path $LocalDir $FileName
Write-Host " 下载: $FileName"
scp "${ServerUser}@${ServerIP}:${RemoteFile}" $LocalFile 2>$null
if ($LASTEXITCODE -eq 0) {
$Downloaded++
Write-Host " ✅ 成功"
} else {
Write-Host " ❌ 失败"
}
}
Write-Host ""
Write-Host "[3/3] 统计..."
Write-Host "✅ 成功下载: $Downloaded / $FileCount"
Write-Host ""
Write-Host "=========================================="
Write-Host " 完成!"
Write-Host "=========================================="
Write-Host ""
Write-Host "📁 本地备份文件:"
Get-ChildItem $LocalDir -Filter "*.sql*" | ForEach-Object {
$size = "{0:N2} MB" -f ($_.Length / 1MB)
Write-Host " $($_.Name) ($size)"
}
Write-Host ""