001project_wildgrowth/backend/deploy/deploy-rsync-from-local.sh

70 lines
3.2 KiB
Bash
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.

#!/bin/bash
# ============================================
# 本机 → 服务器 rsync 回退部署(当服务器 git pull 超时/失败时使用)
# ============================================
# 使用方法(在项目根目录执行):
# export RSYNC_DEPLOY_PASSWORD='你的SSH密码' # 同 DEPLOY_QUICK.md 部署凭据
# bash backend/deploy/deploy-rsync-from-local.sh
# 或一行(密码同部署凭据):
# RSYNC_DEPLOY_PASSWORD='yangyichenYANGYICHENkaifa859' bash backend/deploy/deploy-rsync-from-local.sh
# ============================================
# 会做rsync backend/ 到服务器(--delete再在服务器执行 install/prisma/build/pm2 重启。
# ============================================
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
BACKEND_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
REPO_ROOT="$(cd "$BACKEND_ROOT/.." && pwd)"
SERVER_HOST="${RSYNC_DEPLOY_HOST:-120.55.112.195}"
SERVER_USER="${RSYNC_DEPLOY_USER:-root}"
SERVER_BACKEND="/var/www/wildgrowth-backend/backend"
if [ -z "$RSYNC_DEPLOY_PASSWORD" ]; then
echo "❌ 请设置 RSYNC_DEPLOY_PASSWORD同 DEPLOY_QUICK.md 部署凭据密码)"
echo " export RSYNC_DEPLOY_PASSWORD='你的密码'"
echo " 或: RSYNC_DEPLOY_PASSWORD='...' bash backend/deploy/deploy-rsync-from-local.sh"
exit 1
fi
echo "═══════════════════════════════════════════════════════════"
echo " 📤 Rsync 回退部署:本机 backend/ → 服务器"
echo " 主机: $SERVER_USER@$SERVER_HOST"
echo "═══════════════════════════════════════════════════════════"
echo ""
# 1. rsync--delete 保持服务器与本地一致,排除无需同步的目录)
echo "📂 步骤 1: rsync 同步(--delete..."
export RSYNC_RSH="sshpass -p \"$RSYNC_DEPLOY_PASSWORD\" ssh -o StrictHostKeyChecking=no"
rsync -avz --delete \
--exclude=node_modules \
--exclude='.env*' \
--exclude=dist \
--exclude=.deploy-last \
--exclude=.git \
--exclude=public/uploads \
--exclude=logs \
"$BACKEND_ROOT/" "$SERVER_USER@$SERVER_HOST:$SERVER_BACKEND/"
unset RSYNC_RSH
echo "✅ 同步完成"
echo ""
# 2. 在服务器执行部署步骤(跳过 git pull
echo "🔄 步骤 2: 在服务器执行 install / prisma / build / pm2 restart..."
if command -v sshpass >/dev/null 2>&1; then
sshpass -p "$RSYNC_DEPLOY_PASSWORD" ssh -o StrictHostKeyChecking=no "$SERVER_USER@$SERVER_HOST" \
"cd $SERVER_BACKEND && SKIP_GIT_PULL=1 bash deploy/deploy-from-github.sh"
else
echo "⚠️ 未安装 sshpass请手动 SSH 后执行:"
echo " cd $SERVER_BACKEND && SKIP_GIT_PULL=1 bash deploy/deploy-from-github.sh"
exit 1
fi
echo ""
echo "═══════════════════════════════════════════════════════════"
echo " ✅ Rsync 回退部署完成"
echo "═══════════════════════════════════════════════════════════"