Elysiadev11 commited on
Commit
f617184
Β·
verified Β·
1 Parent(s): 943fc6f

Delete sync.py with huggingface_hub

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