openclaw-plus / sync.py
Elysiadev11's picture
Update sync.py
9ed6b0b verified
# ─────────────────────────────────────────────────────────────
# πŸ“ PATH: sync.py (root HF Space repo)
# ─────────────────────────────────────────────────────────────
import os
import sys
import tarfile
import time
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"
# Folder/file yang di-SKIP dari backup utama
# - openclaw.json β†’ auto-create setiap startup, tidak perlu backup
# - browsers β†’ punya backup terpisah (BROWSER_FILENAME)
SKIP_NAMES = {"openclaw.json", "browsers"}
def restore():
if not repo_id or not token:
print("Skip Restore: HF_DATASET atau HF_TOKEN belum di-set.")
return
# ── Restore 1: sessions & memory ──────────────────────────
try:
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 from {FILENAME}")
except Exception as e:
print(f"Restore Note: {e}")
print(" β†’ Fresh install, normal untuk deploy pertama.")
# ── Restore 2: browser binary ──────────────────────────────
try:
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} β€” akan install fresh jika dibutuhkan.")
def backup():
if not repo_id or not token:
print("Skip Backup: HF_DATASET atau HF_TOKEN belum di-set.")
return
# ── Backup 1: semua file/folder kecuali yang di SKIP_NAMES ─
try:
base_dir = "/root/.openclaw"
print(f"Creating {FILENAME} (semua kecuali: {SKIP_NAMES})...")
with tarfile.open(FILENAME, "w:gz") as tar:
for item in os.listdir(base_dir):
if item in SKIP_NAMES:
print(f" - Skip: {item}")
continue
full_path = os.path.join(base_dir, item)
arcname = item # path relatif di dalam tar = relatif ke /root/.openclaw/
tar.add(full_path, arcname=arcname)
print(f" + Added: {full_path}")
api.upload_file(
path_or_fileobj=FILENAME,
path_in_repo=FILENAME,
repo_id=repo_id,
repo_type="dataset",
token=token
)
print(f"Backup Success β€” {time.strftime('%Y-%m-%d %H:%M:%S')}")
except Exception as e:
print(f"Backup Error: {e}")
# ── Backup 2: browser binary (upload sekali saja, ~150MB) ──
try:
browsers_dir = "/root/.openclaw/browsers"
if not os.path.exists(browsers_dir):
return
# Cek apakah sudah ada di dataset
try:
hf_hub_download(
repo_id=repo_id,
filename=BROWSER_FILENAME,
repo_type="dataset",
token=token
)
print("Browser backup sudah ada di dataset β€” skip upload.")
except Exception:
# Belum ada β†’ upload sekarang
print("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("Browser backup uploaded.")
except Exception as e:
print(f"Backup Error (browser): {e}")
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "backup":
backup()
else:
restore()