xiaowei-system/scripts/restore-state-db.sh

66 lines
1.9 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.

#!/usr/bin/env bash
# state.db 回滚脚本2026-09-03 主人 C 方案带的回滚保险)
# **必须从独立终端跑**(不能从 gateway 内部跑,会被杀)
# 用法bash ~/.hermes/scripts/restore-state-db.sh
set -u
SNAP="/home/muc/.hermes/backups/state-db-snap-20260903_153655"
DB="/home/muc/.hermes/state.db"
WAL="/home/muc/.hermes/state.db-wal"
SHM="/home/muc/.hermes/state.db-shm"
JOBS="/home/muc/.hermes/cron/jobs.json"
LOG="/home/muc/.hermes/backups/state-db/restore-$(date +%Y%m%d_%H%M%S).log"
mkdir -p "$(dirname "$LOG")"
log() { echo "[$(date '+%F %T')] $*" | tee -a "$LOG"; }
if [ ! -f "$SNAP/state.db" ]; then
log "❌ 快照不存在: $SNAP/state.db"
exit 2
fi
log "✅ 快照 OK: $SNAP"
# 0. 停 gateway
log "[0/4] 停 gateway..."
systemctl --user stop hermes-gateway 2>&1 | tee -a "$LOG"
sleep 2
# 1. 原子替换
log "[1/4] 还原 state.db..."
cp -a "$SNAP/state.db" "$DB"
[ -f "$SNAP/state.db-wal" ] && cp -a "$SNAP/state.db-wal" "$WAL" || rm -f "$WAL"
[ -f "$SNAP/state.db-shm" ] && cp -a "$SNAP/state.db-shm" "$SHM" || rm -f "$SHM"
log "✅ state.db 还原完成"
if [ -f "$SNAP/jobs.json" ]; then
cp -a "$SNAP/jobs.json" "$JOBS"
log "✅ jobs.json 还原完成"
fi
# 2. 启 gateway3 次重试)
log "[2/4] 启 gateway3 次重试)..."
for i in 1 2 3; do
systemctl --user start hermes-gateway 2>&1 | tee -a "$LOG"
sleep 3
if systemctl --user is-active hermes-gateway >/dev/null 2>&1; then
log "✅ gateway 第 $i 次启动成功"
break
fi
log "⚠️ 第 $i 次启动未就绪,等待后重试"
sleep 3
done
# 3. 健康检查
log "[3/4] 健康检查..."
sleep 5
if systemctl --user is-active hermes-gateway >/dev/null 2>&1; then
log "✅ gateway ACTIVE回滚成功"
log "📋 主人请重新发起对话(小唯已失忆)"
exit 0
else
log "❌ gateway 还是没起来"
journalctl --user -u hermes-gateway -n 30 --no-pager 2>&1 | tee -a "$LOG"
exit 1
fi