Spaces:
Sleeping
Sleeping
Update sync.py
Browse files
sync.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 2 |
# π PATH: sync.py (root HF Space repo)
|
| 3 |
-
# Upload/edit file ini di:
|
| 4 |
-
# https://huggingface.co/spaces/mark421/OpenClaw-ai/blob/main/sync.py
|
| 5 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 6 |
|
| 7 |
import os
|
|
@@ -13,7 +11,7 @@ api = HfApi()
|
|
| 13 |
repo_id = os.getenv("HF_DATASET")
|
| 14 |
token = os.getenv("HF_TOKEN")
|
| 15 |
|
| 16 |
-
# File backup utama (
|
| 17 |
FILENAME = "latest_backup.tar.gz"
|
| 18 |
# File backup browser (chromium binary ~150MB, download sekali saja)
|
| 19 |
BROWSER_FILENAME = "browser_backup.tar.gz"
|
|
@@ -24,7 +22,7 @@ def restore():
|
|
| 24 |
print("Skip Restore: HF_DATASET or HF_TOKEN not set")
|
| 25 |
return
|
| 26 |
|
| 27 |
-
# Restore config & sessions
|
| 28 |
print(f"Downloading {FILENAME} from {repo_id}...")
|
| 29 |
path = hf_hub_download(
|
| 30 |
repo_id=repo_id,
|
|
@@ -34,10 +32,10 @@ def restore():
|
|
| 34 |
)
|
| 35 |
with tarfile.open(path, "r:gz") as tar:
|
| 36 |
tar.extractall(path="/root/.openclaw/")
|
| 37 |
-
print(f"Success: Restored
|
| 38 |
|
| 39 |
except Exception as e:
|
| 40 |
-
print(f"Restore Note (
|
| 41 |
|
| 42 |
try:
|
| 43 |
if not repo_id or not token:
|
|
@@ -65,17 +63,17 @@ def backup():
|
|
| 65 |
print("Skip Backup: HF_DATASET or HF_TOKEN not set")
|
| 66 |
return
|
| 67 |
|
| 68 |
-
# ββ Backup 1:
|
|
|
|
| 69 |
with tarfile.open(FILENAME, "w:gz") as tar:
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
arcname = p.replace("/root/.openclaw/", "")
|
| 79 |
tar.add(p, arcname=arcname)
|
| 80 |
|
| 81 |
api.upload_file(
|
|
@@ -85,7 +83,7 @@ def backup():
|
|
| 85 |
repo_type="dataset",
|
| 86 |
token=token
|
| 87 |
)
|
| 88 |
-
print(f"Backup
|
| 89 |
|
| 90 |
# ββ Backup 2: browser binary (besar ~150MB, hanya upload kalau belum ada) ββ
|
| 91 |
browsers_dir = "/root/.openclaw/browsers"
|
|
|
|
| 1 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 2 |
# π PATH: sync.py (root HF Space repo)
|
|
|
|
|
|
|
| 3 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 4 |
|
| 5 |
import os
|
|
|
|
| 11 |
repo_id = os.getenv("HF_DATASET")
|
| 12 |
token = os.getenv("HF_TOKEN")
|
| 13 |
|
| 14 |
+
# File backup utama (semua isi workspace kecuali browser)
|
| 15 |
FILENAME = "latest_backup.tar.gz"
|
| 16 |
# File backup browser (chromium binary ~150MB, download sekali saja)
|
| 17 |
BROWSER_FILENAME = "browser_backup.tar.gz"
|
|
|
|
| 22 |
print("Skip Restore: HF_DATASET or HF_TOKEN not set")
|
| 23 |
return
|
| 24 |
|
| 25 |
+
# Restore semua config, memori, & sessions
|
| 26 |
print(f"Downloading {FILENAME} from {repo_id}...")
|
| 27 |
path = hf_hub_download(
|
| 28 |
repo_id=repo_id,
|
|
|
|
| 32 |
)
|
| 33 |
with tarfile.open(path, "r:gz") as tar:
|
| 34 |
tar.extractall(path="/root/.openclaw/")
|
| 35 |
+
print(f"Success: Restored full workspace from {FILENAME}")
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
+
print(f"Restore Note (workspace): {e}")
|
| 39 |
|
| 40 |
try:
|
| 41 |
if not repo_id or not token:
|
|
|
|
| 63 |
print("Skip Backup: HF_DATASET or HF_TOKEN not set")
|
| 64 |
return
|
| 65 |
|
| 66 |
+
# ββ Backup 1: SAPU BERSIH seluruh isi workspace KECUALI browser ββ
|
| 67 |
+
workspace_dir = "/root/.openclaw/"
|
| 68 |
with tarfile.open(FILENAME, "w:gz") as tar:
|
| 69 |
+
if os.path.exists(workspace_dir):
|
| 70 |
+
for item in os.listdir(workspace_dir):
|
| 71 |
+
# Lewati folder 'browsers' agar backup utama tidak bengkak
|
| 72 |
+
if item == "browsers":
|
| 73 |
+
continue
|
| 74 |
+
|
| 75 |
+
p = os.path.join(workspace_dir, item)
|
| 76 |
+
arcname = item # Menjaga struktur asli tanpa path root
|
|
|
|
| 77 |
tar.add(p, arcname=arcname)
|
| 78 |
|
| 79 |
api.upload_file(
|
|
|
|
| 83 |
repo_type="dataset",
|
| 84 |
token=token
|
| 85 |
)
|
| 86 |
+
print(f"Backup full workspace {FILENAME} Success.")
|
| 87 |
|
| 88 |
# ββ Backup 2: browser binary (besar ~150MB, hanya upload kalau belum ada) ββ
|
| 89 |
browsers_dir = "/root/.openclaw/browsers"
|