bill322 commited on
Commit
59ca896
·
verified ·
1 Parent(s): 927f48a

Create sync.py

Browse files
Files changed (1) hide show
  1. sync.py +43 -0
sync.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, sys, tarfile
2
+ from huggingface_hub import HfApi, hf_hub_download
3
+ from datetime import datetime, timedelta
4
+
5
+ api = HfApi()
6
+ repo_id = os.getenv("HF_DATASET")
7
+ token = os.getenv("HF_TOKEN")
8
+
9
+ def restore():
10
+ try:
11
+ files = api.list_repo_files(repo_id=repo_id, repo_type="dataset", token=token)
12
+ now = datetime.now()
13
+ for i in range(5):
14
+ day = (now - timedelta(days=i)).strftime("%Y-%m-%d")
15
+ name = f"backup_{day}.tar.gz"
16
+ if name in files:
17
+ path = hf_hub_download(repo_id=repo_id, filename=name, repo_type="dataset", token=token)
18
+ with tarfile.open(path, "r:gz") as tar:
19
+ tar.extractall(path="/root/.openclaw/")
20
+ print(f"Success: Restored from {name}")
21
+ return True
22
+ except Exception as e:
23
+ print(f"Restore Error: {e}")
24
+
25
+ def backup():
26
+ try:
27
+ day = datetime.now().strftime("%Y-%m-%d")
28
+ name = f"backup_{day}.tar.gz"
29
+ with tarfile.open(name, "w:gz") as tar:
30
+ if os.path.exists("/root/.openclaw/sessions"):
31
+ tar.add("/root/.openclaw/sessions", arcname="sessions")
32
+ if os.path.exists("/root/.openclaw/openclaw.json"):
33
+ tar.add("/root/.openclaw/openclaw.json", arcname="openclaw.json")
34
+ api.upload_file(path_or_fileobj=name, path_in_repo=name, repo_id=repo_id, repo_type="dataset", token=token)
35
+ print(f"Backup {name} Success.")
36
+ except Exception as e:
37
+ print(f"Backup Error: {e}")
38
+
39
+ if __name__ == "__main__":
40
+ if len(sys.argv) > 1 and sys.argv[1] == "backup":
41
+ backup()
42
+ else:
43
+ restore()