介绍
# Obsidian Conversation Backup
自动将 Clawdbot 对话备份到 Obsidian,并采用美观的聊天格式。通过每小时增量快照防止因 `/new` 重置而导致的数据丢失。
## 功能
- **增量备份**:仅每小时对新消息进行快照(无重复) - **聊天格式化**:使用带有表情符号、时间戳且支持多段落的 Obsidian 标注 - **每小时汇总**:按时钟小时组织对话,方便查阅 - **零 Token 成本**:纯 Shell 脚本,无 LLM 调用 - **智能过滤**:跳过空消息和系统通知
## 快速设置
### 安装
```bash # Extract the skill (if downloaded as .skill file) unzip obsidian-conversation-backup.skill cd obsidian-conversation-backup
# Run installer (interactive) chmod +x install.sh ./install.sh ```
安装程序将询问: - Obsidian vault 路径 - 会话目录位置 - 跟踪文件位置
**或者手动设置:**
1. 复制 `config.example` 为 `config` 2. 使用你的路径编辑 `config` 3. 为脚本添加执行权限:`chmod +x scripts/*.sh`
### 启用自动备份
添加到 crontab 以实现每小时备份:
```bash crontab -e
# Add this line (runs every hour at :00) 0 * * * * /path/to/obsidian-conversation-backup/scripts/monitor_and_save.sh ```
### 自定义聊天外观(可选)
编辑 `scripts/format_message_v2.jq` 以更改: - 用户表情符号(默认:🐉) - 助手表情符号(默认:🦞) - 标注类型(默认:用户为 `[!quote]`,助手为 `[!check]`)
## 用法
### 自动增量备份
在 cron 中配置后,系统将自动运行:
**每小时:** - 检查新消息(≥10 行) - 如果发现新消息则创建增量快照 - 保存至:`YYYY-MM-DD-HHmm-incremental.md` - 如果没有新对话则跳过
**输出示例:** ``` 2026-01-20-1500-incremental.md (messages from last save to now) 2026-01-20-1600-incremental.md (new messages since 15:00) 2026-01-20-1700-incremental.md (new messages since 16:00) ```
**保护措施**:最大对话丢失时长 = 1 小时
### 按需完整快照
随时保存完整对话:
```bash scripts/save_full_snapshot.sh [topic-name] ```
**示例:** ```bash scripts/save_full_snapshot.sh important-decisions scripts/save_full_snapshot.sh bug-fix-discussion scripts/save_full_snapshot.sh # uses "full-conversation" as default ```
### 每小时汇总(组织整理)
按时钟小时创建有组织的汇总:
```bash scripts/create_hourly_snapshots.sh YYYY-MM-DD ```
**示例:** ```bash scripts/create_hourly_snapshots.sh 2026-01-20 ```
**输出:** ``` 2026-01-20-1500-hourly.md (15:00-15:59 messages) 2026-01-20-1600-hourly.md (16:00-16:59 messages) 2026-01-20-1700-hourly.md (17:00-17:59 messages) ```
**使用场景**:用于方便查阅的每日整理
## 聊天格式
消息显示为彩色的 Obsidian 标注:
**用户消息**(蓝色 `[!quote]` 标注): ``` > [!quote] 🐉 User · 15:30 > This is my message ```
**助手消息**(绿色 `[!check]` 标注): ``` > [!check] 🦞 Zoidbot · 15:31 > This is the response ```
**功能:** - 时间戳(HH:MM 格式) - 多段落支持(使用 `<br><br>` 分隔段落) - 正确的换行(所有行前缀为 `> `) - 过滤掉空消息 - 排除系统通知
## Token 监控
`monitor_and_save.sh` 脚本还会跟踪 Token 使用情况:
**通过 Telegram 发送警告:** - **800k tokens (80%)**:“考虑尽快执行 /new” - **900k tokens (90%)**:“立即执行 /new”
**实现方式:** ```bash # Sends warning only when crossing threshold (one-time) # No repeated warnings # Resets when back under 800k ```
## 文件结构
``` scripts/ ├── monitor_and_save.sh # Hourly incremental backup + token monitoring ├── save_full_snapshot.sh # On-demand full conversation save ├── create_hourly_snapshots.sh # Organize by clock hour └── format_message_v2.jq # Chat formatting logic ```
## 配置
### 跟踪文件
系统使用隐藏文件来跟踪状态:
```bash /root/clawd/.last_save_line_count # For token monitoring /root/clawd/.last_snapshot_timestamp # For incremental saves /root/clawd/.token_warning_sent # For warning deduplication ```
**注意**:不要删除这些文件,否则增量备份可能会重复内容。
### 会话文件位置
默认:`/root/.clawdbot/agents/main/sessions/*.jsonl`
如果你的会话文件在其他位置,请更新每个脚本中的 `SESSION_FILE` 路径。
## 故障排除
### 未创建快照
1. 检查 cron 是否正在运行:`crontab -l` 2. 验证脚本是否有执行权限:`chmod +x scripts/*.sh` 3. 检查日志:手动运行以查看错误
### 消息跳出标注框
- 确保 `format_message_v2.jq` 中包含 `gsub("\n\n"; "<br><br>")` 这一行 - 检查所有行是否都有 `> ` 前缀 - 验证 jq 是否已安装:`jq --version`
### 快照中出现重复内容
- 删除跟踪文件并让系统重置: ```bash rm /root/clawd/.last_snapshot_timestamp ```
### 出现空的标注框
- 更新 `format_message_v2.jq` 以过滤空消息 - 检查是否存在 `if ($text_content | length) > 0` 条件
## 系统要求
- **jq**:JSON 解析 (`apt-get install jq`) - **cron**:用于自动备份 - **Obsidian vault**:Markdown 文件的目标目录
## 高级自定义
### 更改备份频率
编辑 crontab: ```bash # Every 2 hours 0 */2 * * * /path/to/monitor_and_save.sh
# Every 30 minutes */30 * * * * /path/to/monitor_and_save.sh
# Specific times only (9am, 12pm, 6pm, 9pm) 0 9,12,18,21 * * * /path/to/monitor_and_save.sh ```
### 更改最小消息阈值
编辑 `monitor_and_save.sh`: ```bash # Change from 10 to 5 messages minimum if [[ $new_lines -lt 5 ]]; then ```
### 添加更多标注样式
Obsidian 标注类型: - `[!quote]` - 蓝色 - `[!check]` - 绿色 - `[!note]` - 青色 - `[!tip]` - 紫色 - `[!warning]` - 橙色 - `[!danger]` - 红色
### 自定义 Telegram 通知
编辑 `monitor_and_save.sh` 以更改警告文本或添加自定义通知。
## 最佳实践
1. **在一天结束时运行每小时汇总** - 将其作为整理工具,而非备份手段 2. **保持增量备份运行** - 这是你的安全保障 3. **设置后测试脚本** - 先手动运行以验证输出 4. **备份跟踪文件** - 将 `.last_snapshot_timestamp` 包含在 vault 备份中 5. **使用描述性的主题名称** - 对于完整快照,请使用有意义的名称
## 示例工作流
**日常惯例:** 1. 自动增量备份每小时运行(无需操作) 2. 一天结束时:`scripts/create_hourly_snapshots.sh 2026-01-20` 3. 在 Obsidian 中查看整理好的每小时文件 4. 如有需要可删除旧的增量文件(每小时汇总已覆盖它们)
**在 /new 重置之前:** 1. 可选:`scripts/save_full_snapshot.sh before-reset` 2. 安全运行 `/new` - 对话已备份 3. 继续聊天 - 增量备份将自动恢复
## 与 Clawdbot 集成
此技能兼容: - **HEARTBEAT.md**:自动 Token 监控 - **MEMORY.md**:对话归档系统 - **Telegram 集成**:警告通知 - **任何 Obsidian vault**:适用于现有的 vault
## 致谢
由 Clawdbot 社区创建,旨在提供可靠的对话备份和美观的 Obsidian 格式。