Elysiadev11 commited on
Commit
b69a3ea
Β·
verified Β·
1 Parent(s): 78e7c64

Create sync.py

Browse files
Files changed (1) hide show
  1. sync.py +124 -0
sync.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
8
+ import sys
9
+ import tarfile
10
+ from huggingface_hub import HfApi, hf_hub_download
11
+
12
+ api = HfApi()
13
+ repo_id = os.getenv("HF_DATASET")
14
+ token = os.getenv("HF_TOKEN")
15
+
16
+ # File backup utama (config, sessions)
17
+ FILENAME = "latest_backup.tar.gz"
18
+ # File backup browser (chromium binary ~150MB, download sekali saja)
19
+ BROWSER_FILENAME = "browser_backup.tar.gz"
20
+
21
+ def restore():
22
+ try:
23
+ if not repo_id or not token:
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,
31
+ filename=FILENAME,
32
+ repo_type="dataset",
33
+ token=token
34
+ )
35
+ with tarfile.open(path, "r:gz") as tar:
36
+ tar.extractall(path="/root/.openclaw/")
37
+ print(f"Success: Restored config from {FILENAME}")
38
+
39
+ except Exception as e:
40
+ print(f"Restore Note (config): {e}")
41
+
42
+ try:
43
+ if not repo_id or not token:
44
+ return
45
+
46
+ # Restore browser binary (kalau ada)
47
+ print(f"Downloading {BROWSER_FILENAME} from {repo_id}...")
48
+ browser_path = hf_hub_download(
49
+ repo_id=repo_id,
50
+ filename=BROWSER_FILENAME,
51
+ repo_type="dataset",
52
+ token=token
53
+ )
54
+ with tarfile.open(browser_path, "r:gz") as tar:
55
+ tar.extractall(path="/root/.openclaw/")
56
+ print(f"Success: Restored browser from {BROWSER_FILENAME}")
57
+
58
+ except Exception as e:
59
+ print(f"Restore Note (browser): {e} β€” will install fresh if needed")
60
+
61
+
62
+ def backup():
63
+ try:
64
+ if not repo_id or not token:
65
+ print("Skip Backup: HF_DATASET or HF_TOKEN not set")
66
+ return
67
+
68
+ # ── Backup 1: config & sessions (kecil, selalu diupdate) ──
69
+ with tarfile.open(FILENAME, "w:gz") as tar:
70
+ paths_to_backup = [
71
+ "/root/.openclaw/sessions",
72
+ "/root/.openclaw/agents/main/sessions",
73
+ "/root/.openclaw/openclaw.json",
74
+ "/root/.openclaw/credentials",
75
+ ]
76
+ for p in paths_to_backup:
77
+ if os.path.exists(p):
78
+ arcname = p.replace("/root/.openclaw/", "")
79
+ tar.add(p, arcname=arcname)
80
+
81
+ api.upload_file(
82
+ path_or_fileobj=FILENAME,
83
+ path_in_repo=FILENAME,
84
+ repo_id=repo_id,
85
+ repo_type="dataset",
86
+ token=token
87
+ )
88
+ print(f"Backup config {FILENAME} Success.")
89
+
90
+ # ── Backup 2: browser binary (besar ~150MB, hanya upload kalau belum ada) ──
91
+ browsers_dir = "/root/.openclaw/browsers"
92
+ if os.path.exists(browsers_dir):
93
+ # Cek apakah browser_backup sudah ada di dataset
94
+ try:
95
+ hf_hub_download(
96
+ repo_id=repo_id,
97
+ filename=BROWSER_FILENAME,
98
+ repo_type="dataset",
99
+ token=token
100
+ )
101
+ print(f"Browser backup already exists in dataset, skipping upload.")
102
+ except Exception:
103
+ # Belum ada, upload sekarang
104
+ print(f"Uploading browser backup (first time, ~150MB)...")
105
+ with tarfile.open(BROWSER_FILENAME, "w:gz") as tar:
106
+ tar.add(browsers_dir, arcname="browsers")
107
+ api.upload_file(
108
+ path_or_fileobj=BROWSER_FILENAME,
109
+ path_in_repo=BROWSER_FILENAME,
110
+ repo_id=repo_id,
111
+ repo_type="dataset",
112
+ token=token
113
+ )
114
+ print(f"Browser backup uploaded successfully.")
115
+
116
+ except Exception as e:
117
+ print(f"Backup Error: {e}")
118
+
119
+
120
+ if __name__ == "__main__":
121
+ if len(sys.argv) > 1 and sys.argv[1] == "backup":
122
+ backup()
123
+ else:
124
+ restore()