001project_wildgrowth/backend/deploy/scripts/server-cleanup-safe.sh

66 lines
3.0 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.

#!/bin/bash
# ============================================================
# 服务器安全清理:只删冗余与可再生的,不动跑服务的代码
# 用法cd /var/www/wildgrowth-backend/backend && bash deploy/scripts/server-cleanup-safe.sh
# 迁移后:无 backend/backend无 sync应用根 = backend/
# ============================================================
set -e
GIT_ROOT="/var/www/wildgrowth-backend"
BACKEND="$GIT_ROOT/backend"
cd "$BACKEND" || { echo "❌ 目录不存在: $BACKEND"; exit 1; }
echo "═══════════════════════════════════════════════════════════"
echo " 服务器安全清理(不动 src、prisma、deploy、.env、dist"
echo "═══════════════════════════════════════════════════════════"
# 1. 迁移后冗余:嵌套 backend/ 或 backend/backend 整目录删除(已无 sync不再需要
echo ""
echo "1⃣ 清理冗余嵌套 backend..."
removed=
if [ -d "backend/backend" ]; then rm -rf backend/backend && removed=1; fi
if [ -d "backend" ]; then rm -rf backend && removed=1; fi # 应用根下的 backend 子目录
[ -n "$removed" ] && echo " ✅ 已删冗余 backend/" || echo " (无冗余 backend跳过)"
# 2. 应用根下本机冗余:.DS_Store、一次性脚本
echo ""
echo "2⃣ 清理应用根下冗余文件..."
for f in .DS_Store compile_content_service.sh deploy-fix-next-lesson.sh; do
[ -f "$f" ] && rm -f "$f" && echo " 已删 $f"
done
# 3. 旧备份(若存在)
if ls "$GIT_ROOT"/backend-backup-* 1>/dev/null 2>&1; then
rm -rf "$GIT_ROOT"/backend-backup-*
echo " ✅ 已删旧备份 backend-backup-*"
fi
# 4. 应用日志截断
echo ""
echo "4⃣ 截断 logs..."
if [ -f "logs/combined.log" ]; then
s=$(du -sh logs/combined.log 2>/dev/null | cut -f1)
> logs/combined.log
echo " 已截断 logs/combined.log (原 $s)"
fi
if [ -f "logs/error.log" ]; then
> logs/error.log
echo " 已截断 logs/error.log"
fi
# 5. PM2 日志
echo ""
echo "5⃣ 清空 PM2 日志..."
pm2 flush 2>/dev/null && echo " ✅ pm2 flush 完成" || echo " ⚠️ pm2 flush 失败或未安装"
# 6. npm 缓存
echo ""
echo "6⃣ 清空 npm 缓存..."
npm cache clean --force 2>/dev/null && echo " ✅ npm cache 已清理" || echo " ⚠️ npm cache 清理失败"
echo ""
echo "═══════════════════════════════════════════════════════════"
echo " 安全清理完成"
echo " 可选(本机不构建 iOS 时rm -rf $GIT_ROOT/ios 释放约 5Mgit pull 会拉回)"
echo "═══════════════════════════════════════════════════════════"