Elysiadev11 commited on
Commit
9ed6b0b
Β·
verified Β·
1 Parent(s): 5d74f2a

Update sync.py

Browse files
Files changed (1) hide show
  1. sync.py +68 -60
sync.py CHANGED
@@ -1,30 +1,33 @@
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,
@@ -34,16 +37,14 @@ def restore():
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,
@@ -56,27 +57,28 @@ def restore():
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,
@@ -85,36 +87,42 @@ def backup():
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__":
 
1
  # ─────────────────────────────────────────────────────────────
2
+ # πŸ“ PATH: sync.py (root HF Space repo)
 
 
3
  # ─────────────────────────────────────────────────────────────
4
 
5
  import os
6
  import sys
7
  import tarfile
8
+ import time
9
  from huggingface_hub import HfApi, hf_hub_download
10
 
11
+ api = HfApi()
12
  repo_id = os.getenv("HF_DATASET")
13
+ token = os.getenv("HF_TOKEN")
14
 
15
+ FILENAME = "latest_backup.tar.gz"
 
 
16
  BROWSER_FILENAME = "browser_backup.tar.gz"
17
 
18
+ # Folder/file yang di-SKIP dari backup utama
19
+ # - openclaw.json β†’ auto-create setiap startup, tidak perlu backup
20
+ # - browsers β†’ punya backup terpisah (BROWSER_FILENAME)
21
+ SKIP_NAMES = {"openclaw.json", "browsers"}
22
+
23
+
24
  def restore():
25
+ if not repo_id or not token:
26
+ print("Skip Restore: HF_DATASET atau HF_TOKEN belum di-set.")
27
+ return
 
28
 
29
+ # ── Restore 1: sessions & memory ──────────────────────────
30
+ try:
31
  print(f"Downloading {FILENAME} from {repo_id}...")
32
  path = hf_hub_download(
33
  repo_id=repo_id,
 
37
  )
38
  with tarfile.open(path, "r:gz") as tar:
39
  tar.extractall(path="/root/.openclaw/")
40
+ print(f"Success: Restored from {FILENAME}")
41
 
42
  except Exception as e:
43
+ print(f"Restore Note: {e}")
44
+ print(" β†’ Fresh install, normal untuk deploy pertama.")
45
 
46
+ # ── Restore 2: browser binary ──────────────────────────────
47
  try:
 
 
 
 
48
  print(f"Downloading {BROWSER_FILENAME} from {repo_id}...")
49
  browser_path = hf_hub_download(
50
  repo_id=repo_id,
 
57
  print(f"Success: Restored browser from {BROWSER_FILENAME}")
58
 
59
  except Exception as e:
60
+ print(f"Restore Note (browser): {e} β€” akan install fresh jika dibutuhkan.")
61
 
62
 
63
  def backup():
64
+ if not repo_id or not token:
65
+ print("Skip Backup: HF_DATASET atau HF_TOKEN belum di-set.")
66
+ return
67
+
68
+ # ── Backup 1: semua file/folder kecuali yang di SKIP_NAMES ─
69
  try:
70
+ base_dir = "/root/.openclaw"
 
 
71
 
72
+ print(f"Creating {FILENAME} (semua kecuali: {SKIP_NAMES})...")
73
  with tarfile.open(FILENAME, "w:gz") as tar:
74
+ for item in os.listdir(base_dir):
75
+ if item in SKIP_NAMES:
76
+ print(f" - Skip: {item}")
77
+ continue
78
+ full_path = os.path.join(base_dir, item)
79
+ arcname = item # path relatif di dalam tar = relatif ke /root/.openclaw/
80
+ tar.add(full_path, arcname=arcname)
81
+ print(f" + Added: {full_path}")
 
 
82
 
83
  api.upload_file(
84
  path_or_fileobj=FILENAME,
 
87
  repo_type="dataset",
88
  token=token
89
  )
90
+ print(f"Backup Success β€” {time.strftime('%Y-%m-%d %H:%M:%S')}")
91
+
92
+ except Exception as e:
93
+ print(f"Backup Error: {e}")
94
 
95
+ # ── Backup 2: browser binary (upload sekali saja, ~150MB) ──
96
+ try:
97
  browsers_dir = "/root/.openclaw/browsers"
98
+ if not os.path.exists(browsers_dir):
99
+ return
100
+
101
+ # Cek apakah sudah ada di dataset
102
+ try:
103
+ hf_hub_download(
104
+ repo_id=repo_id,
105
+ filename=BROWSER_FILENAME,
106
+ repo_type="dataset",
107
+ token=token
108
+ )
109
+ print("Browser backup sudah ada di dataset β€” skip upload.")
110
+ except Exception:
111
+ # Belum ada β†’ upload sekarang
112
+ print("Uploading browser backup (first time, ~150MB)...")
113
+ with tarfile.open(BROWSER_FILENAME, "w:gz") as tar:
114
+ tar.add(browsers_dir, arcname="browsers")
115
+ api.upload_file(
116
+ path_or_fileobj=BROWSER_FILENAME,
117
+ path_in_repo=BROWSER_FILENAME,
118
+ repo_id=repo_id,
119
+ repo_type="dataset",
120
+ token=token
121
+ )
122
+ print("Browser backup uploaded.")
123
 
124
  except Exception as e:
125
+ print(f"Backup Error (browser): {e}")
126
 
127
 
128
  if __name__ == "__main__":