135 lines
4.6 KiB
PowerShell
135 lines
4.6 KiB
PowerShell
# 从生产服务器同步缺失的视频文件到本地
|
|
|
|
param(
|
|
[string]$ServerIP = '192.168.58.20',
|
|
[string]$ServerUser = 'root',
|
|
[string]$UploadsPath = '/mnt/PoolOne/apps/youyidian-3d/uploads',
|
|
[string]$LocalUploadsPath = './uploads',
|
|
[switch]$DryRun,
|
|
[int]$Limit = 0 # 0 表示检查所有文件
|
|
)
|
|
|
|
Write-Host "==== 从生产服务器同步缺失的视频文件 ====" -ForegroundColor Cyan
|
|
Write-Host "服务器: $ServerIP" -ForegroundColor Yellow
|
|
Write-Host "远程路径: $UploadsPath" -ForegroundColor Yellow
|
|
Write-Host "本地路径: $LocalUploadsPath" -ForegroundColor Yellow
|
|
if ($DryRun) {
|
|
Write-Host "模式: 演练模式(不会实际下载文件)" -ForegroundColor Magenta
|
|
}
|
|
Write-Host ""
|
|
|
|
# 读取缺失文件列表
|
|
$missingFileList = "missing-video-files.txt"
|
|
if (-not (Test-Path $missingFileList)) {
|
|
Write-Host "❌ 文件列表不存在: $missingFileList" -ForegroundColor Red
|
|
Write-Host " 请先运行: npx ts-node --transpile-only scripts/generate-missing-file-list.ts" -ForegroundColor Yellow
|
|
exit 1
|
|
}
|
|
|
|
$files = Get-Content $missingFileList
|
|
$totalFiles = $files.Count
|
|
|
|
if ($Limit -gt 0) {
|
|
$files = $files[0..($Limit - 1)]
|
|
Write-Host "限制检查前 $Limit 个文件(共 $totalFiles 个)" -ForegroundColor Yellow
|
|
} else {
|
|
Write-Host "将检查所有 $totalFiles 个文件" -ForegroundColor Yellow
|
|
}
|
|
|
|
Write-Host ""
|
|
|
|
# 确保本地 uploads 目录存在
|
|
if (-not (Test-Path $LocalUploadsPath)) {
|
|
Write-Host "创建本地 uploads 目录: $LocalUploadsPath" -ForegroundColor Yellow
|
|
New-Item -ItemType Directory -Path $LocalUploadsPath -Force | Out-Null
|
|
}
|
|
|
|
# 检查每个文件
|
|
$foundCount = 0
|
|
$notFoundCount = 0
|
|
$alreadyExistsCount = 0
|
|
$filesToSync = @()
|
|
|
|
foreach ($fileName in $files) {
|
|
$localFile = Join-Path $LocalUploadsPath $fileName
|
|
$remoteFile = "$UploadsPath/$fileName"
|
|
|
|
# 检查本地是否已存在
|
|
if (Test-Path $localFile) {
|
|
Write-Host "⏭️ $fileName (本地已存在)" -ForegroundColor Gray
|
|
$alreadyExistsCount++
|
|
continue
|
|
}
|
|
|
|
# 检查远程是否存在
|
|
$checkCmd = "test -f '$remoteFile' && echo 'EXISTS' || echo 'NOT_EXISTS'"
|
|
$result = ssh "${ServerUser}@${ServerIP}" $checkCmd
|
|
|
|
if ($result -match 'EXISTS') {
|
|
$filesToSync += $fileName
|
|
Write-Host "✅ $fileName (在生产服务器上)" -ForegroundColor Green
|
|
$foundCount++
|
|
} else {
|
|
Write-Host "❌ $fileName (不在生产服务器上)" -ForegroundColor Red
|
|
$notFoundCount++
|
|
}
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "==== 检查结果 ====" -ForegroundColor Cyan
|
|
Write-Host " 在生产服务器上找到: $foundCount 个" -ForegroundColor Green
|
|
Write-Host " 本地已存在: $alreadyExistsCount 个" -ForegroundColor Gray
|
|
Write-Host " 不在生产服务器上: $notFoundCount 个" -ForegroundColor Red
|
|
Write-Host " 需要同步: $($filesToSync.Count) 个" -ForegroundColor Yellow
|
|
|
|
if ($filesToSync.Count -eq 0) {
|
|
Write-Host ""
|
|
Write-Host "✅ 没有需要同步的文件" -ForegroundColor Green
|
|
exit 0
|
|
}
|
|
|
|
Write-Host ""
|
|
if ($DryRun) {
|
|
Write-Host "==== 演练模式:将同步以下文件 ====" -ForegroundColor Magenta
|
|
$filesToSync | ForEach-Object { Write-Host " - $_" -ForegroundColor White }
|
|
Write-Host ""
|
|
Write-Host "要实际同步,请运行(去掉 -DryRun 参数):" -ForegroundColor Yellow
|
|
Write-Host " .\scripts\sync-missing-files-from-prod.ps1" -ForegroundColor White
|
|
} else {
|
|
Write-Host "==== 开始同步文件 ====" -ForegroundColor Cyan
|
|
Write-Host ""
|
|
|
|
$successCount = 0
|
|
$failCount = 0
|
|
|
|
foreach ($fileName in $filesToSync) {
|
|
$localFile = Join-Path $LocalUploadsPath $fileName
|
|
$remoteFile = "$UploadsPath/$fileName"
|
|
|
|
Write-Host "📥 正在下载: $fileName" -ForegroundColor Yellow
|
|
|
|
try {
|
|
# 使用 scp 下载文件
|
|
scp "${ServerUser}@${ServerIP}:$remoteFile" $localFile 2>&1 | Out-Null
|
|
|
|
if (Test-Path $localFile) {
|
|
$fileSize = (Get-Item $localFile).Length / 1MB
|
|
Write-Host " ✅ 下载成功 (${fileSize:N2} MB)" -ForegroundColor Green
|
|
$successCount++
|
|
} else {
|
|
Write-Host " ❌ 下载失败:文件不存在" -ForegroundColor Red
|
|
$failCount++
|
|
}
|
|
} catch {
|
|
Write-Host " ❌ 下载失败: $_" -ForegroundColor Red
|
|
$failCount++
|
|
}
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "==== 同步完成 ====" -ForegroundColor Cyan
|
|
Write-Host " 成功: $successCount 个" -ForegroundColor Green
|
|
Write-Host " 失败: $failCount 个" -ForegroundColor Red
|
|
}
|
|
|