📸 OpenClaw Agent 状态快照与回滚

像 Git 一样管理你的 Agent 状态

快照 回滚 版本管理 灾难恢复

📖 为什么需要快照?

晚上11点47分,我在改 Agent 的系统 Prompt,手一抖,500行配置没了。那一刻,我明白了备份的重要性——不是"以后再说",是"5分钟前就该做"。

Agent 快照就像给 AI 系个安全带:

⚡ 快速开始

1. 创建快照

# CLI 创建快照
openclaw snapshot create \
  --name "before-prompt-tweak" \
  --description "修改系统Prompt之前的备份" \
  --include config,memory,skills,conversations

# 查看所有快照
openclaw snapshot list

# 输出:
# ID        NAME                      CREATED             SIZE
# snap-001  before-prompt-tweak       2026-04-23 12:00    2.3MB
# snap-002  after-cron-update         2026-04-22 08:00    2.1MB
# snap-003  initial-setup             2026-04-01 10:00    1.8MB

2. 回滚到快照

# 回滚到指定快照
openclaw snapshot rollback snap-001

# 预览回滚变更(不实际执行)
openclaw snapshot diff snap-002 snap-001

# 输出:
# CHANGED:  SOUL.md        (+15 lines, -3 lines)
# CHANGED:  config.yaml    (+2 lines, -8 lines)
# ADDED:    skills/new/     (3 files)
# REMOVED:  skills/old/    (1 file)

3. 自动快照策略

# ~/.openclaw/config.yaml
snapshots:
  # 自动创建快照的触发条件
  autoCreate:
    beforeConfigChange: true    # 配置变更前
    beforeDeploy: true          # 部署前
    onSchedule: "0 2 * * *"     # 每天凌晨2点
    maxAge: "7d"               # 快照保留7天
    
  # 快照内容
  include:
    - config.yaml              # Agent 配置
    - SOUL.md                  # 人设文件
    - USER.md                  # 用户偏好
    - TOOLS.md                 # 工具配置
    - MEMORY.md                # 长期记忆
    - skills/                  # 所有 Skills
    - memory/                  # 每日记忆
    - secrets.env              # 加密存储的密钥
    
  # 排除项
  exclude:
    - "*.log"
    - "node_modules/"
    - ".git/"
    
  # 存储配置
  storage:
    type: "local"  # local | s3 | gcs
    path: "./snapshots/"
    compression: true
    encryption: true  # AES-256 加密

🔧 高级功能

差异对比

# 比较两个快照的差异
openclaw snapshot diff snap-001 snap-003 --format json

# 比较当前状态与快照
openclaw snapshot diff HEAD snap-001

# 输出结构化的变更报告:
{
  "changed_files": ["SOUL.md", "config.yaml"],
  "added_files": ["skills/new-feature/"],
  "removed_files": [],
  "summary": "3 files changed, 12 insertions(+), 5 deletions(-)"
}

部分恢复

# 只恢复某个文件(不回滚全部)
openclaw snapshot restore snap-001 --files SOUL.md

# 只恢复某个目录
openclaw snapshot restore snap-001 --dirs skills/

# 交互式选择要恢复的文件
openclaw snapshot restore snap-001 --interactive

远程同步

# 推送快照到远程存储
openclaw snapshot push snap-001 --target s3://my-bucket/openclaw/

# 从远程拉取快照
openclaw snapshot pull snap-001 --source s3://my-bucket/openclaw/

# 自动备份到 GitHub
snapshots:
  gitSync:
    enabled: true
    repo: git@github.com:user/openclaw-configs.git
    branch: snapshots
    autoPush: true

💡 最佳实践

✅ 快照管理策略

场景 策略 说明
日常开发 配置变更前自动快照 防手滑
重大更新 手动命名快照 v2.0-prompt-rewrite
定时备份 每天凌晨自动快照 保留7天
多环境 dev/staging/prod 各自快照 环境隔离
团队共享 推送到远程仓库 团队协作

⚠️ 注意事项

  1. 密钥安全 - 快照中可能包含 API Key,务必加密
  2. 存储空间 - 定期清理过期快照
  3. 回滚验证 - 回滚后先在测试环境验证
  4. 对话上下文 - 对话历史可能很大,考虑是否包含

🔗 相关资源