# ───────────────────────────────────────────────────────────── # 📁 PATH: sync.py (root HF Space repo) # Upload/edit file ini di: # https://huggingface.co/spaces/mark421/OpenClaw-ai/blob/main/sync.py # ───────────────────────────────────────────────────────────── import os import sys import tarfile from huggingface_hub import HfApi, hf_hub_download api = HfApi() repo_id = os.getenv("HF_DATASET") token = os.getenv("HF_TOKEN") # File backup utama (config, sessions) FILENAME = "latest_backup.tar.gz" # File backup browser (chromium binary ~150MB, download sekali saja) BROWSER_FILENAME = "browser_backup.tar.gz" def restore(): try: if not repo_id or not token: print("Skip Restore: HF_DATASET or HF_TOKEN not set") return # Restore config & sessions print(f"Downloading {FILENAME} from {repo_id}...") path = hf_hub_download( repo_id=repo_id, filename=FILENAME, repo_type="dataset", token=token ) with tarfile.open(path, "r:gz") as tar: tar.extractall(path="/root/.openclaw/") print(f"Success: Restored config from {FILENAME}") except Exception as e: print(f"Restore Note (config): {e}") try: if not repo_id or not token: return # Restore browser binary (kalau ada) print(f"Downloading {BROWSER_FILENAME} from {repo_id}...") browser_path = hf_hub_download( repo_id=repo_id, filename=BROWSER_FILENAME, repo_type="dataset", token=token ) with tarfile.open(browser_path, "r:gz") as tar: tar.extractall(path="/root/.openclaw/") print(f"Success: Restored browser from {BROWSER_FILENAME}") except Exception as e: print(f"Restore Note (browser): {e} — will install fresh if needed") def backup(): """ Backup strategy: Always backup the entire workspace directory. This ensures all agent workspaces (workspace, workspace-jarvis, workspace-writer, etc.) and their memory files are included in every backup. """ try: if not repo_id or not token: print("Skip Backup: HF_DATASET or HF_TOKEN not set") return # ── Backup: Always include ALL workspaces (required) ── with tarfile.open(FILENAME, "w:gz") as tar: paths_to_backup = [ "/root/.openclaw/sessions", "/root/.openclaw/agents/main/sessions", "/root/.openclaw/agents/jarvis/sessions", "/root/.openclaw/agents/writer/sessions", "/root/.openclaw/agents/mp-agent/sessions", "/root/.openclaw/agents/stocke/sessions", "/root/.openclaw/openclaw.json", "/root/.openclaw/credentials", # ALWAYS backup all workspaces (full workspace requirement) "/root/.openclaw/workspace", "/root/.openclaw/workspace-jarvis", "/root/.openclaw/workspace-writer", "/root/.openclaw/workspace-mp-agent", "/root/.openclaw/workspace-stock", # System skills and plugins "/usr/local/lib/node_modules/openclaw/skills", "/usr/local/lib/node_modules/openclaw/dist/extensions", ] for p in paths_to_backup: if os.path.exists(p): arcname = p.replace("/root/.openclaw/", "") tar.add(p, arcname=arcname) api.upload_file( path_or_fileobj=FILENAME, path_in_repo=FILENAME, repo_id=repo_id, repo_type="dataset", token=token ) print(f"Backup config {FILENAME} Success.") # ── Backup 2: browser binary (besar ~150MB, hanya upload kalau belum ada) ── browsers_dir = "/root/.openclaw/browsers" if os.path.exists(browsers_dir): # Cek apakah browser_backup sudah ada di dataset try: hf_hub_download( repo_id=repo_id, filename=BROWSER_FILENAME, repo_type="dataset", token=token ) print(f"Browser backup already exists in dataset, skipping upload.") except Exception: # Belum ada, upload sekarang print(f"Uploading browser backup (first time, ~150MB)...") with tarfile.open(BROWSER_FILENAME, "w:gz") as tar: tar.add(browsers_dir, arcname="browsers") api.upload_file( path_or_fileobj=BROWSER_FILENAME, path_in_repo=BROWSER_FILENAME, repo_id=repo_id, repo_type="dataset", token=token ) print(f"Browser backup uploaded successfully.") except Exception as e: print(f"Backup Error: {e}") if __name__ == "__main__": if len(sys.argv) > 1 and sys.argv[1] == "backup": backup() else: restore()