#!/bin/bash
# OpenClaw 每周自动备份脚本
# 运行时间: 每周日凌晨 2:00

BACKUP_DIR="/root/openclaw-backups"
DATE=$(date +%Y%m%d-%H%M%S)
BACKUP_FILE="$BACKUP_DIR/openclaw-weekly-$DATE.tar.gz"

# 创建备份目录
mkdir -p "$BACKUP_DIR"

# 执行备份
tar -czf "$BACKUP_FILE" \
  -C /root/.openclaw \
  --exclude='workspace/cat-fly/__pycache__' \
  --exclude='workspace/market-regime-identifier/__pycache__' \
  --exclude='*.pyc' \
  --exclude='*.log' \
  workspace/ \
  openclaw.json \
  openclaw.json.bak \
  openclaw.json.bak.1 \
  openclaw.json.bak.2 \
  openclaw.json.bak.3 \
  openclaw.json.bak.4 2>/dev/null

# 检查备份是否成功
if [ $? -eq 0 ]; then
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] ✓ OpenClaw 备份成功: $BACKUP_FILE"
    echo "  文件大小: $(du -h $BACKUP_FILE | cut -f1)"
    
    # 只保留最近4个备份（约1个月）
    ls -t $BACKUP_DIR/openclaw-weekly-*.tar.gz | tail -n +5 | xargs -r rm -f
    echo "  已清理旧备份，保留最近4个"
else
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] ✗ OpenClaw 备份失败"
    exit 1
fi
