he1237596 commited on
Commit
9f0c3bb
·
verified ·
1 Parent(s): 30802fb

Update sync.py

Browse files
Files changed (1) hide show
  1. sync.py +22 -15
sync.py CHANGED
@@ -8,23 +8,29 @@ 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
- # 直接下载最新文件
18
  print(f"Downloading {FILENAME} from {repo_id}...")
19
- path = hf_hub_download(repo_id=repo_id, filename=FILENAME, repo_type="dataset", token=token)
 
 
 
 
 
20
 
21
  with tarfile.open(path, "r:gz") as tar:
22
- tar.extractall(path="/root/.openclaw/")
23
- print(f"Success: Restored from {FILENAME}")
24
- return True
25
  except Exception as e:
26
- # 如果是第一次运行,仓库里没文件,报错是正常的
27
- print(f"Restore Note: No existing backup found or error: {e}")
28
 
29
  def backup():
30
  try:
@@ -33,18 +39,17 @@ def backup():
33
  return
34
 
35
  with tarfile.open(FILENAME, "w:gz") as tar:
36
- # 备份关键数据
37
- paths_to_backup = [
38
  "/root/.openclaw/sessions",
39
  "/root/.openclaw/agents/main/sessions",
40
  "/root/.openclaw/openclaw.json"
41
  ]
42
- for p in paths_to_backup:
 
43
  if os.path.exists(p):
44
- arcname = p.replace("/root/.openclaw/", "")
45
  tar.add(p, arcname=arcname)
46
-
47
- # 上传并覆盖
48
  api.upload_file(
49
  path_or_fileobj=FILENAME,
50
  path_in_repo=FILENAME,
@@ -52,9 +57,11 @@ def backup():
52
  repo_type="dataset",
53
  token=token
54
  )
55
- print(f"Backup {FILENAME} Success (Overwritten).")
 
56
  except Exception as e:
57
- print(f"Backup Error: {e}")
 
58
 
59
  if __name__ == "__main__":
60
  if len(sys.argv) > 1 and sys.argv[1] == "backup":
 
8
  token = os.getenv("HF_TOKEN")
9
  FILENAME = "latest_backup.tar.gz"
10
 
11
+ BASE_PATH = "/root/.openclaw/"
12
+
13
  def restore():
14
  try:
15
  if not repo_id or not token:
16
  print("Skip Restore: HF_DATASET or HF_TOKEN not set")
17
  return
18
 
 
19
  print(f"Downloading {FILENAME} from {repo_id}...")
20
+ path = hf_hub_download(
21
+ repo_id=repo_id,
22
+ filename=FILENAME,
23
+ repo_type="dataset",
24
+ token=token
25
+ )
26
 
27
  with tarfile.open(path, "r:gz") as tar:
28
+ tar.extractall(path=BASE_PATH)
29
+
30
+ print("Restore success")
31
  except Exception as e:
32
+ print(f"No backup found or restore failed: {e}")
33
+
34
 
35
  def backup():
36
  try:
 
39
  return
40
 
41
  with tarfile.open(FILENAME, "w:gz") as tar:
42
+ paths = [
 
43
  "/root/.openclaw/sessions",
44
  "/root/.openclaw/agents/main/sessions",
45
  "/root/.openclaw/openclaw.json"
46
  ]
47
+
48
+ for p in paths:
49
  if os.path.exists(p):
50
+ arcname = p.replace(BASE_PATH, "")
51
  tar.add(p, arcname=arcname)
52
+
 
53
  api.upload_file(
54
  path_or_fileobj=FILENAME,
55
  path_in_repo=FILENAME,
 
57
  repo_type="dataset",
58
  token=token
59
  )
60
+
61
+ print("Backup success")
62
  except Exception as e:
63
+ print(f"Backup error: {e}")
64
+
65
 
66
  if __name__ == "__main__":
67
  if len(sys.argv) > 1 and sys.argv[1] == "backup":