memoryweave/scripts/restore.sh

96 lines
2.7 KiB
Bash
Executable File
Raw Permalink 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
# 织忆恢复脚本 — 从备份恢复数据
# 用法: ./restore.sh [backup_dir]
# backup_dir: 可选,如不填则列出可用备份供选择
set -e
BACKUP_ROOT="/home/muc/backups/memoryweave"
DATA_DIR="/var/lib/memoryweave"
STAMP=$(date +%Y%m%d-%H%M%S)
if [ -n "$1" ]; then
BACKUP_DIR="$1"
else
echo "=== 可用备份 ==="
backups=($(ls -t "$BACKUP_ROOT"/))
if [ ${#backups[@]} -eq 0 ]; then
echo "错误: 没有找到任何备份"
exit 1
fi
for i in "${!backups[@]}"; do
echo " [$i] ${backups[$i]}"
done
echo ""
read -p "选择备份 [0-${#backups[@]}-1]: " idx
BACKUP_DIR="$BACKUP_ROOT/${backups[$idx]}"
fi
if [ ! -d "$BACKUP_DIR" ]; then
echo "错误: 备份目录不存在: $BACKUP_DIR"
exit 1
fi
echo "[$STAMP] 织忆恢复开始..."
echo "[$STAMP] 使用备份: $BACKUP_DIR"
read -p "即将停止 zhiyid 并恢复数据,确认? [y/N]: " confirm
[ "$confirm" = "y" ] || { echo "取消"; exit 0; }
# 停止服务
echo "[$STAMP] 停止 zhiyid..."
systemctl --user stop zhiyid 2>/dev/null || sudo systemctl stop zhiyid 2>/dev/null || true
# 备份当前数据(出事前的快照)
CURRENT_BACKUP="$DATA_DIR/pre-restore-$STAMP"
echo "[$STAMP] 备份当前数据到: $CURRENT_BACKUP"
mkdir -p "$CURRENT_BACKUP"
cp -r "$DATA_DIR"/*.db "$DATA_DIR"/*.lance "$CURRENT_BACKUP/" 2>/dev/null || true
# 恢复 SQLite
for db in graph.db memoryweave.db; do
echo "[$STAMP] 恢复 $db..."
if [ -f "$BACKUP_DIR/$db" ]; then
cp "$BACKUP_DIR/$db" "$DATA_DIR/$db"
else
echo "[$STAMP] 警告: 备份中无 $db,跳过"
fi
done
# 恢复 LanceDB
echo "[$STAMP] 恢复 LanceDB 数据..."
if [ -f "$BACKUP_DIR/lance-data.tar.gz" ]; then
tar xzf "$BACKUP_DIR/lance-data.tar.gz" -C "$DATA_DIR"
else
echo "[$STAMP] 警告: 备份中无 lance-data.tar.gz跳过"
fi
# 验证
echo "[$STAMP] 验证恢复..."
ok=true
for db in graph.db memoryweave.db; do
if [ -s "$DATA_DIR/$db" ]; then
echo "[$STAMP] $db: OK ($(du -sh "$DATA_DIR/$db" | cut -f1))"
else
echo "[$STAMP] $db: 失败"
ok=false
fi
done
for lance in episodes.lance memories.lance tombstones.lance; do
if [ -s "$DATA_DIR/$lance" ]; then
echo "[$STAMP] $lance: OK"
fi
done
# 重启
echo "[$STAMP] 重启 zhiyid..."
systemctl --user start zhiyid 2>/dev/null || sudo systemctl start zhiyid 2>/dev/null || true
sleep 2
# 健康检查
if curl -sf http://127.0.0.1:7821/health > /dev/null 2>&1; then
echo "[$STAMP] ✅ 恢复成功zhiyid 健康检查通过"
else
echo "[$STAMP] ⚠️ zhiyid 未响应,请手动检查"
fi
echo "[$STAMP] 完成。当前数据快照已保存到: $CURRENT_BACKUP"