Update sync.py
Browse files
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 |
-
|
| 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 |
-
|
| 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]
|
| 55 |
continue
|
| 56 |
|
| 57 |
-
# 提取(会覆盖已存在文件)
|
| 58 |
tar.extract(member, path=OPENCLAW_DIR)
|
| 59 |
extracted_count += 1
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
sessions_found = True
|
| 64 |
|
| 65 |
-
print(f"[Restore] ✅
|
| 66 |
-
if sessions_found:
|
| 67 |
-
print("[Restore] ✅ Sessions directory/files detected in backup.")
|
| 68 |
|
| 69 |
-
# 恢复后诊断
|
| 70 |
-
print("\n[Restore] Post-restore
|
| 71 |
-
for
|
| 72 |
OPENCLAW_DIR,
|
| 73 |
-
os.path.join(OPENCLAW_DIR, "sessions"),
|
| 74 |
-
os.path.join(OPENCLAW_DIR, "
|
| 75 |
]:
|
| 76 |
-
if os.path.exists(
|
| 77 |
-
|
| 78 |
-
print(f" ✓ {
|
|
|
|
|
|
|
| 79 |
else:
|
| 80 |
-
print(f" ✗ {
|
| 81 |
|
| 82 |
-
print("[Restore]
|
| 83 |
|
| 84 |
return True
|
| 85 |
|
| 86 |
except (RepositoryNotFoundError, EntryNotFoundError):
|
| 87 |
-
print("[Restore]
|
| 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]
|
| 94 |
finally:
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
if 'download_path' in locals() and os.path.exists(download_path):
|
| 98 |
os.remove(download_path)
|
| 99 |
-
|
| 100 |
-
|
| 101 |
|
| 102 |
def backup():
|
| 103 |
try:
|
| 104 |
if not repo_id or not token:
|
| 105 |
-
print("Skip Backup
|
| 106 |
return
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
print(f"No data in {OPENCLAW_DIR} to backup.")
|
| 110 |
return
|
| 111 |
|
| 112 |
-
print(f"[Backup]
|
| 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(
|
| 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:
|