# 服务器清理脚本 # 清理旧的 Docker 镜像和数据库残留数据,但保留用户数据 param( [switch]$SkipDocker = $false, [switch]$SkipDatabase = $false, [switch]$DryRun = $false ) $ErrorActionPreference = "Stop" Write-Host "========================================" -ForegroundColor Cyan Write-Host " 服务器清理脚本" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" if ($DryRun) { Write-Host "[INFO] 干运行模式 - 只显示将要执行的操作,不实际执行" -ForegroundColor Yellow Write-Host "" } # SSH 配置 $SSH_KEY = "$env:USERPROFILE\.ssh\youyidian_prod_ed25519" $SERVER = "root@45.249.208.118" # 检查 SSH 连接 Write-Host "[INFO] 检查 SSH 连接..." -ForegroundColor Cyan try { $testResult = ssh -i $SSH_KEY -o ConnectTimeout=5 $SERVER "echo 'connected'" 2>&1 if ($LASTEXITCODE -ne 0) { throw "SSH 连接失败" } Write-Host "[OK] SSH 连接成功" -ForegroundColor Green } catch { Write-Host "[ERROR] SSH 连接失败: $_" -ForegroundColor Red exit 1 } Write-Host "" # 1. 清理 Docker 镜像 if (-not $SkipDocker) { Write-Host "[*] Docker 镜像清理 (1/2)" -ForegroundColor Cyan Write-Host "[INFO] 检查 Docker 镜像..." -ForegroundColor Cyan if ($DryRun) { Write-Host "[DRY RUN] 将执行以下操作:" -ForegroundColor Yellow Write-Host " - 删除所有未使用的 flnxi-material 镜像(保留最新的 local-deploy)" -ForegroundColor Yellow Write-Host " - 清理 Docker 构建缓存" -ForegroundColor Yellow Write-Host " - 执行 docker system prune -f" -ForegroundColor Yellow } else { # 获取当前使用的镜像 ID $currentImageId = ssh -i $SSH_KEY $SERVER "docker inspect flnxi-material --format='{{.Image}}' 2>/dev/null || echo ''" if ($currentImageId) { Write-Host "[INFO] 当前使用的镜像: $currentImageId" -ForegroundColor Cyan # 删除所有未使用的 flnxi-material 镜像(保留当前使用的) Write-Host "[INFO] 删除未使用的 flnxi-material 镜像..." -ForegroundColor Cyan ssh -i $SSH_KEY $SERVER @" docker images --format '{{.ID}} {{.Repository}}:{{.Tag}}' | grep 'flnxi-material' | grep -v 'local-deploy' | awk '{print \$1}' | xargs -r docker rmi -f 2>/dev/null || true docker images --format '{{.ID}} {{.Repository}}:{{.Tag}}' | grep 'flnxi-material.*' | awk '{print \$1}' | xargs -r docker rmi -f 2>/dev/null || true "@ Write-Host "[OK] 未使用的镜像已删除" -ForegroundColor Green } else { Write-Host "[WARN] 无法获取当前使用的镜像 ID,跳过镜像清理" -ForegroundColor Yellow } # 清理构建缓存 Write-Host "[INFO] 清理 Docker 构建缓存..." -ForegroundColor Cyan ssh -i $SSH_KEY $SERVER "docker builder prune -f" # 清理未使用的资源 Write-Host "[INFO] 清理未使用的 Docker 资源..." -ForegroundColor Cyan ssh -i $SSH_KEY $SERVER "docker system prune -f --volumes=false" Write-Host "[OK] Docker 清理完成" -ForegroundColor Green } Write-Host "" } # 2. 清理数据库残留数据 if (-not $SkipDatabase) { Write-Host "[*] 数据库清理 (2/2)" -ForegroundColor Cyan Write-Host "[INFO] 准备清理数据库残留数据..." -ForegroundColor Cyan if ($DryRun) { Write-Host "[DRY RUN] 将执行以下操作:" -ForegroundColor Yellow Write-Host " - 清理过期的邀请码(已使用且过期超过30天)" -ForegroundColor Yellow Write-Host " - 清理旧的弹幕数据(超过90天)" -ForegroundColor Yellow Write-Host " - 清理无效的素材数据(如果有)" -ForegroundColor Yellow Write-Host " - 保留所有用户数据" -ForegroundColor Green Write-Host " - 保留所有素材数据" -ForegroundColor Green Write-Host " - 保留所有模板数据" -ForegroundColor Green } else { # 创建数据库清理 SQL $cleanupSql = @" -- 清理过期的邀请码(已使用且过期超过30天) DELETE FROM invitations WHERE used_at IS NOT NULL AND expires_at IS NOT NULL AND expires_at < NOW() - INTERVAL '30 days'; -- 清理旧的弹幕数据(超过90天) DELETE FROM danmakus WHERE created_at < NOW() - INTERVAL '90 days'; -- 清理无效的素材数据(previewUrl 为空或无效的素材,但保留用户数据) -- 注意:这个操作比较危险,先注释掉,需要时手动执行 -- DELETE FROM materials WHERE preview_url = '' OR preview_url IS NULL; -- 显示清理统计 SELECT 'invitations' as table_name, COUNT(*) as remaining_count FROM invitations UNION ALL SELECT 'danmakus' as table_name, COUNT(*) as remaining_count FROM danmakus; "@ Write-Host "[INFO] 执行数据库清理..." -ForegroundColor Cyan # 将 SQL 保存到临时文件并执行 $tempFile = "/tmp/cleanup-db-$(Get-Date -Format 'yyyyMMdd-HHmmss').sql" $cleanupSql | ssh -i $SSH_KEY $SERVER "cat > $tempFile" # 在容器内执行 SQL $result = ssh -i $SSH_KEY $SERVER @" docker exec flnxi-material sh -c 'cd /app && cat $tempFile | PGPASSWORD=\$DATABASE_PASSWORD psql -h \$DATABASE_HOST -U \$DATABASE_USER -d \$DATABASE_NAME 2>&1' || echo 'SQL execution completed' "@ Write-Host $result # 删除临时文件 ssh -i $SSH_KEY $SERVER "rm -f $tempFile" Write-Host "[OK] 数据库清理完成" -ForegroundColor Green } Write-Host "" } # 显示清理后的空间使用情况 Write-Host "[INFO] 清理后的空间使用情况:" -ForegroundColor Cyan ssh -i $SSH_KEY $SERVER "docker system df" Write-Host "" Write-Host "========================================" -ForegroundColor Cyan Write-Host " 清理完成!" -ForegroundColor Green Write-Host "========================================" -ForegroundColor Cyan