conanholmes commited on
Commit
7f610cc
·
verified ·
1 Parent(s): a5359f2

Create sync.py

Browse files
Files changed (1) hide show
  1. sync.py +67 -0
sync.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import tarfile
4
+ from huggingface_hub import HfApi, hf_hub_download
5
+
6
+ api = HfApi()
7
+ repo_id = os.getenv("HF_DATASET")
8
+ token = os.getenv("HF_TOKEN")
9
+ FILENAME = "latest_backup.tar.gz"
10
+
11
+ def restore():
12
+ try:
13
+ if not repo_id or not token:
14
+ print("Skip Restore: HF_DATASET or HF_TOKEN not set")
15
+ return
16
+
17
+ print(f"Downloading {FILENAME} from {repo_id}...")
18
+ path = hf_hub_download(
19
+ repo_id=repo_id,
20
+ filename=FILENAME,
21
+ repo_type="dataset",
22
+ token=token
23
+ )
24
+
25
+ with tarfile.open(path, "r:gz") as tar:
26
+ tar.extractall(path="/root/.openclaw/")
27
+
28
+ print(f"Success: Restored from {FILENAME}")
29
+ return True
30
+ except Exception as e:
31
+ print(f"Restore Note: No existing backup found or error: {e}")
32
+
33
+ def backup():
34
+ try:
35
+ if not repo_id or not token:
36
+ print("Skip Backup: HF_DATASET or HF_TOKEN not set")
37
+ return
38
+
39
+ with tarfile.open(FILENAME, "w:gz") as tar:
40
+ paths_to_backup = [
41
+ "/root/.openclaw/sessions",
42
+ "/root/.openclaw/agents/main/sessions",
43
+ "/root/.openclaw/openclaw.json"
44
+ ]
45
+
46
+ for p in paths_to_backup:
47
+ if os.path.exists(p):
48
+ arcname = p.replace("/root/.openclaw/", "")
49
+ tar.add(p, arcname=arcname)
50
+
51
+ api.upload_file(
52
+ path_or_fileobj=FILENAME,
53
+ path_in_repo=FILENAME,
54
+ repo_id=repo_id,
55
+ repo_type="dataset",
56
+ token=token
57
+ )
58
+
59
+ print(f"Backup {FILENAME} Success (Overwritten).")
60
+ except Exception as e:
61
+ print(f"Backup Error: {e}")
62
+
63
+ if __name__ == "__main__":
64
+ if len(sys.argv) > 1 and sys.argv[1] == "backup":
65
+ backup()
66
+ else:
67
+ restore()