Files
Artveris/scripts/upgrade-remote-steps.sh

95 lines
3.9 KiB
Bash

#!/usr/bin/env bash
# artveris 远端升级序列(由 upgrade-remote.ps1 上传并调用):
# 备份(DB+代码) -> 解压新包 -> 停服 -> upgrade.sh -> 重启 -> 健康检查
# 用法: bash upgrade-remote-steps.sh <包名不含扩展名> [部署目录] [pm2项目名]
set -eu
PKG="${1:?用法: bash upgrade-remote-steps.sh <包名不含扩展名> [部署目录] [pm2项目名]}"
DEPLOY="${2:-/www/wwwroot/artveris}"
PM2_NAME="${3:-server}"
UP="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)"
STAMP=$(date +%Y%m%d%H%M%S)
BK="$UP/backup"
echo "==== [0] 前置检查 ===="
[ -f "$UP/$PKG.tar.gz" ] || { echo "缺少 $UP/$PKG.tar.gz"; exit 1; }
[ -f "$DEPLOY/.env" ] || { echo "缺少 $DEPLOY/.env,目标不是有效部署"; exit 1; }
pm2 describe "$PM2_NAME" >/dev/null 2>&1 || { echo "pm2 项目 $PM2_NAME 不存在"; exit 1; }
mkdir -p "$BK"
echo "==== [1] 数据库备份 ===="
CNF=$(mktemp /tmp/artveris-dump.XXXXXX.cnf)
chmod 600 "$CNF"
DB_NAME=$(node -e "
const fs=require('fs');
const m=fs.readFileSync('$DEPLOY/.env','utf8').match(/^DATABASE_URL\s*=\s*\"?([^\"\r\n]+)\"?/m);
if(!m){console.error('未找到 DATABASE_URL');process.exit(1)}
const u=new URL(m[1]);
const esc=(s)=>decodeURIComponent(s).replace(/\"/g,'\\\\\"');
const cnf='[client]\nhost='+u.hostname+'\nport='+(u.port||3306)+'\nuser=\"'+esc(u.username)+'\"\npassword=\"'+esc(u.password)+'\"\n';
fs.writeFileSync('$CNF',cnf);
console.log(u.pathname.slice(1).split('?')[0]);
")
DB_FILE="$BK/db-$DB_NAME-$STAMP.sql.gz"
mysqldump --defaults-extra-file="$CNF" --single-transaction --no-tablespaces --default-character-set=utf8mb4 "$DB_NAME" | gzip > "$DB_FILE"
rm -f "$CNF"
[ -s "$DB_FILE" ] || { echo "数据库备份为空,中止"; exit 1; }
echo "DB 备份: $(ls -lh "$DB_FILE" | awk '{print $5, $9}')"
echo "==== [2] 代码快照备份(不含 node_modules/storage/backups) ===="
CODE_FILE="$BK/code-$STAMP.tar.gz"
tar -czf "$CODE_FILE" -C "$DEPLOY" --exclude=./node_modules --exclude=./storage --exclude=./backups .
echo "代码备份: $(ls -lh "$CODE_FILE" | awk '{print $5, $9}')"
echo "==== [3] 解压新包 ===="
cd "$UP"
rm -rf "$PKG"
tar -xzf "$PKG.tar.gz"
[ -f "$UP/$PKG/upgrade.sh" ] || { echo "包内缺少 upgrade.sh"; exit 1; }
echo "==== [4] 停止服务 ===="
pm2 stop "$PM2_NAME"
echo "==== [5] 执行升级(替换代码 + 装依赖 + prisma generate + migrate deploy) ===="
if ! sh "$UP/$PKG/upgrade.sh" "$DEPLOY"; then
echo "升级失败!服务保持停止状态。可用备份回滚:$CODE_FILE / $DB_FILE"
echo "回滚代码: tar -xzf $CODE_FILE -C $DEPLOY 后 pm2 restart $PM2_NAME"
exit 1
fi
echo "==== [6] 重启服务 ===="
pm2 restart "$PM2_NAME"
pm2 save >/dev/null 2>&1 || true
echo "==== [7] 健康检查 ===="
PORT=$(grep -E '^API_PORT=' "$DEPLOY/.env" | head -1 | sed 's/^API_PORT=//; s/\"//g')
PORT=${PORT:-3000}
OK=0
for i in 1 2 3 4 5 6 7 8 9 10; do
sleep 3
CODE=$(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:$PORT/api/install/status" || true)
if [ "$CODE" = "200" ]; then OK=1; break; fi
echo "等待服务启动($i)... HTTP $CODE"
done
if [ "$OK" != "1" ]; then
echo "健康检查失败!请立刻查看 pm2 logs $PM2_NAME;备份在 $BK"
pm2 logs "$PM2_NAME" --lines 30 --nostream || true
exit 1
fi
curl -s -o /dev/null -w 'install/status HTTP %{http_code}\n' "http://127.0.0.1:$PORT/api/install/status"
curl -s -o /dev/null -w 'admin HTTP %{http_code}\n' "http://127.0.0.1:$PORT/admin/"
curl -s -o /dev/null -w 'trace HTTP %{http_code}\n' "http://127.0.0.1:$PORT/trace/"
echo "==== [8] 清理:保留最近 3 份升级包与备份 ===="
cd "$UP"
ls -1t artveris-upgrade-*.tar.gz 2>/dev/null | tail -n +4 | while read -r old; do
rm -rf "$old" "${old%.tar.gz}"
echo "已清理旧包 $old"
done
ls -1dt artveris-upgrade-*/ 2>/dev/null | tail -n +4 | xargs -r rm -rf
cd "$BK"
ls -1t db-*.sql.gz 2>/dev/null | tail -n +4 | xargs -r rm -f
ls -1t code-*.tar.gz 2>/dev/null | tail -n +4 | xargs -r rm -f
echo "==== 升级完成 ===="