| |
| |
| |
| |
| |
|
|
| 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") |
|
|
| |
| FILENAME = "latest_backup.tar.gz" |
| |
| 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 |
|
|
| |
| 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 |
|
|
| |
| 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 |
|
|
| |
| 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", |
| |
| "/root/.openclaw/workspace", |
| "/root/.openclaw/workspace-jarvis", |
| "/root/.openclaw/workspace-writer", |
| "/root/.openclaw/workspace-mp-agent", |
| "/root/.openclaw/workspace-stock", |
| |
| "/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.") |
|
|
| |
| browsers_dir = "/root/.openclaw/browsers" |
| if os.path.exists(browsers_dir): |
| |
| 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: |
| |
| 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() |