sharween commited on
Commit
9e4b886
·
verified ·
1 Parent(s): 1244853

Update sync.py

Browse files
Files changed (1) hide show
  1. sync.py +29 -47
sync.py CHANGED
@@ -13,7 +13,6 @@ FILENAME = "latest_backup.tar.gz"
13
  OPENCLAW_DIR = "/root/.openclaw"
14
 
15
  def is_safe_path(base_dir: str, target_path: str) -> bool:
16
- """严格防止路径穿越"""
17
  base_abs = os.path.abspath(base_dir)
18
  target_abs = os.path.abspath(target_path)
19
  return os.path.commonpath([base_abs]) == os.path.commonpath([base_abs, target_abs])
@@ -33,84 +32,68 @@ def restore():
33
  force_download=True
34
  )
35
 
36
- if not os.path.exists(download_path):
37
- print("[Restore] Download succeeded but file not found locally.")
38
- return
39
-
40
- print(f"[Restore] Download successful ({os.path.getsize(download_path)} bytes). Starting safe restore...")
41
 
42
- # 创建目标目录(如果不存在)
43
  os.makedirs(OPENCLAW_DIR, exist_ok=True)
44
 
45
- # 安全提取(逐成员处理 + 覆盖)
46
  extracted_count = 0
47
- sessions_found = False
48
 
49
  with tarfile.open(download_path, "r:gz") as tar:
50
  for member in tar.getmembers():
51
- # 跳过不安全路径
52
  member_path = os.path.join(OPENCLAW_DIR, member.name)
53
  if not is_safe_path(OPENCLAW_DIR, member_path):
54
- print(f"[Restore] Warning: Skipping unsafe path: {member.name}")
55
  continue
56
 
57
- # 提取(会覆盖已存在文件)
58
  tar.extract(member, path=OPENCLAW_DIR)
59
  extracted_count += 1
60
 
61
- # 诊断:检查是否包含 sessions
62
- if "sessions" in member.name.lower():
63
- sessions_found = True
64
 
65
- print(f"[Restore] ✅ Successfully extracted {extracted_count} items.")
66
- if sessions_found:
67
- print("[Restore] ✅ Sessions directory/files detected in backup.")
68
 
69
- # 恢复后诊断检查
70
- print("\n[Restore] Post-restore diagnosis:")
71
- for check_path in [
72
  OPENCLAW_DIR,
73
- os.path.join(OPENCLAW_DIR, "sessions"),
74
- os.path.join(OPENCLAW_DIR, "agents", "main", "sessions")
75
  ]:
76
- if os.path.exists(check_path):
77
- items = len(os.listdir(check_path)) if os.listdir(check_path) else 0
78
- print(f" ✓ {check_path} exists → {items} items")
 
 
79
  else:
80
- print(f" ✗ {check_path} does NOT exist")
81
 
82
- print("[Restore] Full restore completed. Chat history should now be available.")
83
 
84
  return True
85
 
86
  except (RepositoryNotFoundError, EntryNotFoundError):
87
- print("[Restore] Note: No backup file found in HF repo yet (normal for first run).")
88
- except HfHubHTTPError as e:
89
- print(f"[Restore] HTTP Error (network or repo issue): {e}")
90
- except tarfile.TarError as e:
91
- print(f"[Restore] Tar file error (possibly corrupted backup): {e}")
92
  except Exception as e:
93
- print(f"[Restore] Unexpected error: {e}")
94
  finally:
95
- # 清理下载的临时文件
96
- try:
97
- if 'download_path' in locals() and os.path.exists(download_path):
98
  os.remove(download_path)
99
- except:
100
- pass
101
 
102
  def backup():
103
  try:
104
  if not repo_id or not token:
105
- print("Skip Backup: HF_DATASET or HF_TOKEN not set")
106
  return
107
-
108
- if not os.path.exists(OPENCLAW_DIR) or not os.listdir(OPENCLAW_DIR):
109
- print(f"No data in {OPENCLAW_DIR} to backup.")
110
  return
111
 
112
- print(f"[Backup] Creating full backup of {OPENCLAW_DIR}...")
113
-
114
  with tarfile.open(FILENAME, "w:gz") as tar:
115
  tar.add(OPENCLAW_DIR, arcname=os.path.basename(OPENCLAW_DIR))
116
 
@@ -121,8 +104,7 @@ def backup():
121
  repo_type="dataset",
122
  token=token
123
  )
124
- print(f"[Backup] ✅ Success: latest_backup.tar.gz uploaded.")
125
-
126
  except Exception as e:
127
  print(f"[Backup] Error: {e}")
128
  finally:
 
13
  OPENCLAW_DIR = "/root/.openclaw"
14
 
15
  def is_safe_path(base_dir: str, target_path: str) -> bool:
 
16
  base_abs = os.path.abspath(base_dir)
17
  target_abs = os.path.abspath(target_path)
18
  return os.path.commonpath([base_abs]) == os.path.commonpath([base_abs, target_abs])
 
32
  force_download=True
33
  )
34
 
35
+ print(f"[Restore] Download OK ({os.path.getsize(download_path)} bytes). Starting restore...")
 
 
 
 
36
 
 
37
  os.makedirs(OPENCLAW_DIR, exist_ok=True)
38
 
 
39
  extracted_count = 0
40
+ sessions_files = 0
41
 
42
  with tarfile.open(download_path, "r:gz") as tar:
43
  for member in tar.getmembers():
 
44
  member_path = os.path.join(OPENCLAW_DIR, member.name)
45
  if not is_safe_path(OPENCLAW_DIR, member_path):
46
+ print(f"[Restore] Skipping unsafe path: {member.name}")
47
  continue
48
 
 
49
  tar.extract(member, path=OPENCLAW_DIR)
50
  extracted_count += 1
51
 
52
+ if "sessions" in member.name and member.name.endswith((".json", ".jsonl")):
53
+ sessions_files += 1
 
54
 
55
+ print(f"[Restore] ✅ Extracted {extracted_count} files, including {sessions_files} session files.")
 
 
56
 
57
+ # 恢复后详细诊断(重点看 agents/main/sessions)
58
+ print("\n[Restore] Post-restore check:")
59
+ for p in [
60
  OPENCLAW_DIR,
61
+ os.path.join(OPENCLAW_DIR, "agents", "main", "sessions"),
62
+ os.path.join(OPENCLAW_DIR, "sessions")
63
  ]:
64
+ if os.path.exists(p):
65
+ files = list(os.listdir(p))
66
+ print(f" ✓ {p} → {len(files)} items")
67
+ if "sessions" in p and any(f.endswith(".jsonl") for f in files[:10]):
68
+ print(f" (contains .jsonl transcripts)")
69
  else:
70
+ print(f" ✗ {p} missing")
71
 
72
+ print("[Restore] Restore finished. Now running openclaw doctor --fix...")
73
 
74
  return True
75
 
76
  except (RepositoryNotFoundError, EntryNotFoundError):
77
+ print("[Restore] No backup found yet (normal on first run).")
 
 
 
 
78
  except Exception as e:
79
+ print(f"[Restore] Error: {e}")
80
  finally:
81
+ if 'download_path' in locals() and os.path.exists(download_path):
82
+ try:
 
83
  os.remove(download_path)
84
+ except:
85
+ pass
86
 
87
  def backup():
88
  try:
89
  if not repo_id or not token:
90
+ print("Skip Backup")
91
  return
92
+ if not os.path.exists(OPENCLAW_DIR):
93
+ print("No data to backup")
 
94
  return
95
 
96
+ print(f"[Backup] Backing up entire {OPENCLAW_DIR}...")
 
97
  with tarfile.open(FILENAME, "w:gz") as tar:
98
  tar.add(OPENCLAW_DIR, arcname=os.path.basename(OPENCLAW_DIR))
99
 
 
104
  repo_type="dataset",
105
  token=token
106
  )
107
+ print("[Backup] ✅ Success")
 
108
  except Exception as e:
109
  print(f"[Backup] Error: {e}")
110
  finally: