Testclaw / sync.py
Elysiadev11's picture
Update sync.py
fe6d3fe verified
# ─────────────────────────────────────────────────────────────
# πŸ“ 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()