#!/usr/bin/env bash # ARTEX 升级与回滚脚本: # - 升级 Docker 部署(保留 pgdata / ./data / ./skills,兼容旧版自动校准目录权限) # - 回滚到任意历史部署快照(本地固定镜像 ID,离线/覆盖 latest 也可 100% 精准回滚;健康检查失败自动回滚) # - 本地 Go 编译更新(自动备份 ./artex -> ./artex.old 支持回滚) set -euo pipefail cd "$(cd "$(dirname "$0")" && pwd)" # shellcheck source=scripts/deploy-lib.sh . ./scripts/deploy-lib.sh sync_repo(){ [ "${ARTEX_SKIP_GIT_PULL:-0}" = "1" ] && return 0 [ -d .git ] && command -v git >/dev/null 2>&1 || { warn_l "非 git 工作副本,跳过 git pull" "Not a git working tree; skipping git pull"; return 0; } is_yes "$(ask_l '拉取最新代码 (git pull --ff-only)? (y/n)' 'Pull latest code (git pull --ff-only)? (y/n)' y)" || return 0 if ! git pull --ff-only; then warn_l "git pull 未能快进(本地有改动或分支分叉)——请手动处理后重试,本次沿用当前代码" \ "git pull could not fast-forward (local changes or diverged branch) -- continuing with current files" fi } require_docker_env(){ command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1 \ || die_l "未检测到 docker / docker compose,请先用 ./install.sh 安装部署" \ "docker / docker compose not found; run ./install.sh first" [ -f .env ] || die_l "未找到 .env,请先运行 ./install.sh 完成首次部署" \ ".env not found; run ./install.sh for initial deployment" } # ── ① Docker 升级(自动快照 + 失败自动回滚)───────────────────────────── update_docker(){ local tag="${1:-}" skip_db="${2:-0}" require_docker_env preflight_docker if [ -z "$tag" ] && [ "${ARTEX_NONINTERACTIVE:-0}" != "1" ]; then tag="$(ask_l '目标镜像 tag(回车沿用 .env / latest;如 v0.3.35)' 'Target image tag (Enter to keep .env / latest; e.g. v0.3.35)' '')" fi validate_sandbox_compose "$COMPOSE_FILE" "$tag" info_l "正在创建升级前部署快照(用于失败自动回滚或手动回滚)…" \ "Creating pre-upgrade deployment snapshot (for auto-rollback or manual rollback)..." local snap_id snap_id="$(create_deploy_snapshot "upgrade" "$skip_db")" ok_l "已创建回滚快照:${snap_id}" "Created rollback snapshot: ${snap_id}" prepare_env_for_docker "$tag" .env prepare_migrated_workspaces ./data deploy_with_rollback "$snap_id" ok_l "升级完成 → http://localhost:8787" "Upgrade complete -> http://localhost:8787" info_l "如需回滚到升级前状态:./update.sh --rollback ${snap_id}" \ "To roll back to the previous state: ./update.sh --rollback ${snap_id}" info_l "查看日志:docker compose -f ${COMPOSE_FILE} logs -f artex" \ "View logs: docker compose -f ${COMPOSE_FILE} logs -f artex" } # ── ④ 回滚到历史部署快照 ──────────────────────────────────────────────── interactive_rollback(){ local snap_id="${1:-}" restore_db="${2:-0}" require_docker_env if [ -z "$snap_id" ]; then echo "$(msg '可用的部署回滚快照:' 'Available rollback snapshots:')" list_deploy_snapshots local latest="" [ -f "${DEPLOY_STATE_DIR}/latest" ] && latest="$(cat "${DEPLOY_STATE_DIR}/latest")" [ -n "$latest" ] || die_l "当前没有任何可回滚的部署快照" "No rollback snapshots available" snap_id="$(ask_l '要回滚的快照 ID(回车选最新)' 'Snapshot ID to roll back to (Enter for latest)' "$latest")" fi local snap_dir="${DEPLOY_SNAP_DIR}/${snap_id}" [ -f "${snap_dir}/meta.env" ] || die_l "快照不存在:${snap_id}" "Snapshot not found: ${snap_id}" if [ "$restore_db" != "1" ] && [ "$(env_get HAS_PG_DUMP "${snap_dir}/meta.env")" = "1" ] && [ "${ARTEX_NONINTERACTIVE:-0}" != "1" ]; then warn_l "当前版本与上一版本数据库结构前向/后向兼容,通常无需覆盖数据库。" \ "The database schema is forward/backward compatible; restoring the DB dump is usually unnecessary." if is_yes "$(ask_l '是否同时用快照中的 postgres.sql.gz 覆盖恢复数据库? (y/n)' 'Also overwrite and restore the database from snapshot postgres.sql.gz? (y/n)' n)"; then restore_db=1 fi fi rollback_to_snapshot "$snap_id" "$restore_db" } # ── ⑤ 本地编译更新 ────────────────────────────────────────────────────── update_local(){ command -v go >/dev/null 2>&1 || die_l "未检测到 Go(>=1.26):https://go.dev/dl/" \ "Go (>=1.26) not found: https://go.dev/dl/" [ -f config.json ] || warn_l "未找到 config.json——若首次部署请改用 ./install.sh" \ "config.json not found -- run ./install.sh for initial setup" ok "Go: $(go version)" if [ -x ./artex ]; then cp ./artex ./artex.old ok_l "已备份当前二进制 → ./artex.old(如需回滚可执行 mv ./artex.old ./artex 后重启)" \ "Backed up current binary -> ./artex.old (to roll back: mv ./artex.old ./artex and restart)" fi if command -v npm >/dev/null 2>&1; then info_l "重建前端静态产物…" "Rebuilding static frontend assets..." ( cd web && npm ci && npm run build:static ) rm -rf server/webui/dist && cp -r web/out server/webui/dist info_l "重新编译内嵌单二进制…" "Rebuilding embedded single binary..." CGO_ENABLED=0 go build -tags embedui -trimpath -o artex ./cmd/artex else warn_l "未检测到 npm:编译**不内嵌前端**的后端(前端需另跑 npm run dev)" \ "npm not found: building backend WITHOUT embedded frontend (run npm run dev separately)" CGO_ENABLED=0 go build -o artex ./cmd/artex fi ok_l "编译完成 → ./artex" "Build complete -> ./artex" warn_l "请重启正在运行的 artex 进程以生效(重启时会自动迁移 schema)" \ "Please restart the running artex process to take effect (schema migrates automatically on startup)" } usage(){ if [ "$ARTEX_SCRIPT_LANG" = "zh" ]; then cat <<'USAGE' 用法: ./update.sh [动作] [选项] 动作(不传则进入交互菜单): --upgrade 升级 Docker 部署(自动创建回滚快照) --rollback [SNAPSHOT_ID] 回滚到最新或指定的历史部署快照 --list-snapshots 列出所有可用的部署回滚快照 --local 本地 Go 重新编译更新 选项: --tag 指定目标镜像版本 tag(如 v0.3.34 / latest) --restore-db 配合 --rollback 同时恢复快照中的 PostgreSQL 备份 --no-db-backup 创建快照时跳过 pg_dump --skip-pull 跳过镜像拉取 --skip-git-pull 跳过 git pull -y, --yes 非交互模式 -h, --help 显示本帮助 USAGE else cat <<'USAGE' Usage: ./update.sh [action] [options] Actions (opens interactive menu if omitted): --upgrade Upgrade Docker deployment (creates rollback snapshot) --rollback [SNAPSHOT_ID] Roll back to the latest or specified snapshot --list-snapshots List all available rollback snapshots --local Rebuild and update locally with Go Options: --tag Target image tag (e.g. v0.3.34 / latest) --restore-db With --rollback, also restore the snapshot PostgreSQL dump --no-db-backup Skip pg_dump when creating a snapshot --skip-pull Skip pulling Docker images --skip-git-pull Skip git pull -y, --yes Non-interactive mode -h, --help Show this help USAGE fi } ACTION="" OPT_TAG="" OPT_SNAP="" OPT_RESTORE_DB=0 OPT_SKIP_DB=0 while [ "$#" -gt 0 ]; do case "$1" in --upgrade) ACTION="upgrade" ;; --rollback) ACTION="rollback" if [ "${2:-}" != "" ] && [ "${2#-}" = "${2}" ]; then shift OPT_SNAP="$1" fi ;; --list-snapshots) ACTION="list-snapshots" ;; --local) ACTION="local" ;; --tag) shift; [ "$#" -gt 0 ] || die_l "--tag 需要参数" "--tag requires a value"; OPT_TAG="$1" ;; --restore-db) OPT_RESTORE_DB=1 ;; --no-db-backup) OPT_SKIP_DB=1 ;; --skip-pull) export ARTEX_SKIP_PULL=1 ;; --skip-git-pull) export ARTEX_SKIP_GIT_PULL=1 ;; -y|--yes) export ARTEX_NONINTERACTIVE=1 ;; -h|--help) usage; exit 0 ;; *) die_l "未知参数:$1(使用 --help 查看帮助)" "Unknown option: $1 (use --help for usage)" ;; esac shift done case "$ACTION" in upgrade) sync_repo update_docker "$OPT_TAG" "$OPT_SKIP_DB" exit 0 ;; rollback) interactive_rollback "$OPT_SNAP" "$OPT_RESTORE_DB" exit 0 ;; list-snapshots) list_deploy_snapshots exit 0 ;; local) sync_repo update_local exit 0 ;; esac echo "==========================================" echo "$(msg ' ARTEX 升级与回滚' ' ARTEX Upgrade & Rollback')" echo "$(msg ' 1) Docker 升级(自动创建回滚快照)' ' 1) Docker upgrade (auto-creates rollback snapshot)')" echo "$(msg ' 2) 回滚到历史部署快照' ' 2) Roll back to a previous snapshot')" echo "$(msg ' 3) 本地更新(Go 重新编译)' ' 3) Local update (rebuild with Go)')" echo "==========================================" case "$(ask_l '选择' 'Select' 1)" in 1) sync_repo; update_docker "$OPT_TAG" "$OPT_SKIP_DB" ;; 2) interactive_rollback "$OPT_SNAP" "$OPT_RESTORE_DB" ;; 3) sync_repo; update_local ;; *) die_l "无效选择" "Invalid selection" ;; esac