WEB/rollback.sh
2026-08-26 18:27:37 +08:00

32 lines
1.1 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
set -eu
USER="webdev"
IP="112.124.21.102" # 服务器 IP
BASE_PATH="/www/wwwroot/web" # 项目在服务器上的存放路径
# SSH 密钥提示:请确保本地已将 id_ed25519.pub 添加到服务器的 ~/.ssh/authorized_keys
# 密钥内容:ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJtwa+MFLQEgkEbcGQ6dkQOiFydRvjVeidJ/7uBsF2e 3313488199@qq.com
SSH_KEY="$HOME/.ssh/id_ed25519_admin"
SSH_OPTS="-i $SSH_KEY -o StrictHostKeyChecking=no"
echo "⏪ 正在回滚到上一个版本..."
ssh $SSH_OPTS $USER@$IP "BASE='$BASE_PATH' bash -s" << 'REMOTE_EOF'
set -euo pipefail
# 获取 releases 目录下按时间排序的倒数第二个版本(当前版本的上一版)
PREVIOUS_VERSION="$(ls -dt "$BASE"/releases/*/ 2>/dev/null | sed -n '2p' || true)"
if [ -z "$PREVIOUS_VERSION" ]; then
echo "❌ 找不到可回滚的历史版本"
exit 1
fi
# 原子切换软链接(与发版脚本一致,切换瞬间服务不中断)
ln -sfn "$PREVIOUS_VERSION" "$BASE/current_new"
mv -Tf "$BASE/current_new" "$BASE/current"
echo "✅ 已回滚至: $PREVIOUS_VERSION"
REMOTE_EOF