memoryweave/scripts/backup.sh

56 lines
1.7 KiB
Bash
Executable File
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
# 织忆每日备份脚本(由 cronjob 每天 04:00 调用)
# 直接执行备份,不走 Go HTTP API
set -e
API_KEY="zhiyi-dev-key-2026"
BACKUP_ROOT="/home/muc/backups/memoryweave"
DATA_DIR="/var/lib/memoryweave"
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
BACKUP_DIR="$BACKUP_ROOT/$TIMESTAMP"
echo "[$TIMESTAMP] 织忆备份开始..."
# 1. 创建备份目录
mkdir -p "$BACKUP_DIR"
# 2. SQLite graph.db 备份
echo "[$TIMESTAMP] 备份 graph.db..."
if ! sqlite3 "$DATA_DIR/graph.db" ".backup $BACKUP_DIR/graph.db"; then
echo "[$TIMESTAMP] ERROR: graph.db 备份失败"
exit 1
fi
# 3. SQLite memoryweave.db 备份
echo "[$TIMESTAMP] 备份 memoryweave.db..."
if ! sqlite3 "$DATA_DIR/memoryweave.db" ".backup $BACKUP_DIR/memoryweave.db"; then
echo "[$TIMESTAMP] ERROR: memoryweave.db 备份失败"
exit 1
fi
# 4. Redis 异步保存
echo "[$TIMESTAMP] 触发 Redis BGSAVE..."
redis-cli BGSAVE 2>/dev/null || true
# 5. LanceDB 目录 tar 打包(最多等 300s
echo "[$TIMESTAMP] 打包 LanceDB 数据(可能需要 1-2 分钟)..."
if ! timeout 300 tar czf "$BACKUP_DIR/lance-data.tar.gz" \
-C "$DATA_DIR" episodes.lance memories.lance tombstones.lance; then
echo "[$TIMESTAMP] ERROR: LanceDB 打包超时或失败"
exit 1
fi
# 6. 验证
echo "[$TIMESTAMP] 验证备份..."
for f in graph.db memoryweave.db lance-data.tar.gz; do
if [ ! -s "$BACKUP_DIR/$f" ]; then
echo "[$TIMESTAMP] ERROR: $f 为空或不存在"
exit 1
fi
done
# 7. 打印结果
SIZE=$(du -sh "$BACKUP_DIR" | cut -f1)
echo "[$TIMESTAMP] 备份成功 | 路径: $BACKUP_DIR | 大小: $SIZE"
echo "[$TIMESTAMP] 织忆备份结果: OK | 路径: $BACKUP_DIR | 时间戳: $TIMESTAMP" > "$HOME/projects/memoryweave/backups/backup-$TIMESTAMP.log"