Spaces:
Sleeping
Sleeping
File size: 4,482 Bytes
b69a3ea fe6d3fe b69a3ea fe6d3fe b69a3ea fe6d3fe b69a3ea fe6d3fe b69a3ea fe6d3fe b69a3ea fe6d3fe b69a3ea fe6d3fe b69a3ea | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# π PATH: sync.py (root HF Space repo)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
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 (semua isi workspace kecuali browser)
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 semua config, memori, & 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 full workspace from {FILENAME}")
except Exception as e:
print(f"Restore Note (workspace): {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():
try:
if not repo_id or not token:
print("Skip Backup: HF_DATASET or HF_TOKEN not set")
return
# ββ Backup 1: SAPU BERSIH seluruh isi workspace KECUALI browser ββ
workspace_dir = "/root/.openclaw/"
with tarfile.open(FILENAME, "w:gz") as tar:
if os.path.exists(workspace_dir):
for item in os.listdir(workspace_dir):
# Lewati folder 'browsers' agar backup utama tidak bengkak
if item == "browsers":
continue
p = os.path.join(workspace_dir, item)
arcname = item # Menjaga struktur asli tanpa path root
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 full workspace {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() |