From 34b3e47d5b8a385fa7637a34851218946d82bff8 Mon Sep 17 00:00:00 2001 From: linbuxiao Date: Fri, 30 Jan 2026 12:23:20 +0800 Subject: [PATCH] feat: support using R2 to sync *.md files in the clawd workspace --- src/gateway/sync.test.ts | 7 +++++++ src/gateway/sync.ts | 4 ++-- start-moltbot.sh | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/gateway/sync.test.ts b/src/gateway/sync.test.ts index 994ef3f..80b3944 100644 --- a/src/gateway/sync.test.ts +++ b/src/gateway/sync.test.ts @@ -115,6 +115,13 @@ describe('syncToR2', () => { expect(rsyncCall).toContain('--delete'); expect(rsyncCall).toContain('/root/.clawdbot/'); expect(rsyncCall).toContain('/data/moltbot/'); + expect(rsyncCall).toContain('/root/clawd/'); + expect(rsyncCall).toContain('/data/moltbot/clawd/'); + expect(rsyncCall).toContain("--include='MEMORY.md'"); + expect(rsyncCall).toContain("--include='IDENTITY.md'"); + expect(rsyncCall).toContain("--include='USER.md'"); + expect(rsyncCall).toContain("--include='SOUL.md'"); + expect(rsyncCall).toContain("--include='memory/***'"); }); }); }); diff --git a/src/gateway/sync.ts b/src/gateway/sync.ts index a10c711..e91cdaf 100644 --- a/src/gateway/sync.ts +++ b/src/gateway/sync.ts @@ -59,7 +59,7 @@ export async function syncToR2(sandbox: Sandbox, env: MoltbotEnv): Promise ${R2_MOUNT_PATH}/.last-sync`; + const syncCmd = `rsync -r --no-times --delete --exclude='*.lock' --exclude='*.log' --exclude='*.tmp' /root/.clawdbot/ ${R2_MOUNT_PATH}/clawdbot/ && rsync -r --no-times --delete /root/clawd/skills/ ${R2_MOUNT_PATH}/skills/ && rsync -r --no-times --delete --include='MEMORY.md' --include='IDENTITY.md' --include='USER.md' --include='SOUL.md' --include='memory/***' --exclude='*' /root/clawd/ ${R2_MOUNT_PATH}/clawd/ && date -Iseconds > ${R2_MOUNT_PATH}/.last-sync`; try { const proc = await sandbox.startProcess(syncCmd); @@ -67,7 +67,7 @@ export async function syncToR2(sandbox: Sandbox, env: MoltbotEnv): Promise/dev/null)" ]; fi fi +# Restore clawd memory artifacts from R2 backup if available (only if R2 is newer) +CLAWD_DIR="/root/clawd" +if [ -d "$BACKUP_DIR/clawd" ]; then + if should_restore_from_r2; then + echo "Restoring clawd memory artifacts from $BACKUP_DIR/clawd..." + mkdir -p "$CLAWD_DIR" + cp -a "$BACKUP_DIR/clawd/MEMORY.md" "$CLAWD_DIR/" 2>/dev/null || true + cp -a "$BACKUP_DIR/clawd/IDENTITY.md" "$CLAWD_DIR/" 2>/dev/null || true + cp -a "$BACKUP_DIR/clawd/USER.md" "$CLAWD_DIR/" 2>/dev/null || true + cp -a "$BACKUP_DIR/clawd/SOUL.md" "$CLAWD_DIR/" 2>/dev/null || true + if [ -d "$BACKUP_DIR/clawd/memory" ]; then + mkdir -p "$CLAWD_DIR/memory" + cp -a "$BACKUP_DIR/clawd/memory/." "$CLAWD_DIR/memory/" 2>/dev/null || true + fi + echo "Restored clawd memory artifacts from R2 backup" + fi +fi + # If config file still doesn't exist, create from template if [ ! -f "$CONFIG_FILE" ]; then echo "No existing config found, initializing from template..."