#!/bin/bash set -e OPENCLAW_DIR="/root/.openclaw" BUCKET_DIR="/data/openclaw" EXCLUDES=( --exclude="cache/" --exclude="logs/" --exclude="tmp/" --exclude="*.sock" --exclude="*.lock*" --exclude="*-wal" --exclude="*-shm" --exclude="*-journal" ) # 1. Restore on boot if [ -d "$BUCKET_DIR" ] && [ "$(ls -A "$BUCKET_DIR" 2>/dev/null)" ]; then echo "[Storage] Restoring from $BUCKET_DIR..." rsync -a "${EXCLUDES[@]}" "$BUCKET_DIR/" "$OPENCLAW_DIR/" echo "[Storage] Restore completed." fi # 2. Database repair openclaw doctor --fix --yes 2>/dev/null || true # 3. Background sync to bucket every 30s if [ -d "/data" ]; then mkdir -p "$BUCKET_DIR" ( set +e while true; do sleep 30 # Flush SQLite WAL to main db files for db in "$OPENCLAW_DIR"/state/*.sqlite "$OPENCLAW_DIR"/agents/*/agent/*.sqlite; do if [ -f "$db" ]; then python3 -c "import sqlite3; c=sqlite3.connect('$db'); c.execute('PRAGMA wal_checkpoint(PASSIVE);'); c.close()" 2>/dev/null || true fi done # Sync state excluding runtime journals/locks rsync -a --delete "${EXCLUDES[@]}" "$OPENCLAW_DIR/" "$BUCKET_DIR/" 2>/dev/null || true done ) & fi # 4. Graceful shutdown sync sync_exit() { echo "[Storage] Container stopping, running final sync..." rsync -a --delete "${EXCLUDES[@]}" "$OPENCLAW_DIR/" "$BUCKET_DIR/" 2>/dev/null || true exit 0 } trap sync_exit SIGTERM SIGINT chmod -R 700 "$OPENCLAW_DIR" # 5. Start gateway exec openclaw gateway run --port 7860