Update sync.py
Browse files
sync.py
CHANGED
|
@@ -1,102 +1,43 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
import tarfile
|
| 4 |
-
import shutil
|
| 5 |
-
from pathlib import Path
|
| 6 |
from huggingface_hub import HfApi, hf_hub_download
|
| 7 |
-
from huggingface_hub.utils import HfHubHTTPError, RepositoryNotFoundError, EntryNotFoundError
|
| 8 |
|
| 9 |
api = HfApi()
|
| 10 |
repo_id = os.getenv("HF_DATASET")
|
| 11 |
token = os.getenv("HF_TOKEN")
|
| 12 |
FILENAME = "latest_backup.tar.gz"
|
| 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])
|
| 19 |
|
| 20 |
def restore():
|
| 21 |
try:
|
| 22 |
if not repo_id or not token:
|
| 23 |
print("Skip Restore: HF_DATASET or HF_TOKEN not set")
|
| 24 |
return
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 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 |
-
|
| 80 |
-
|
| 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 |
-
|
| 97 |
with tarfile.open(FILENAME, "w:gz") as tar:
|
| 98 |
-
tar.add(
|
| 99 |
|
|
|
|
| 100 |
api.upload_file(
|
| 101 |
path_or_fileobj=FILENAME,
|
| 102 |
path_in_repo=FILENAME,
|
|
@@ -104,15 +45,9 @@ def backup():
|
|
| 104 |
repo_type="dataset",
|
| 105 |
token=token
|
| 106 |
)
|
| 107 |
-
print("
|
| 108 |
except Exception as e:
|
| 109 |
-
print(f"
|
| 110 |
-
finally:
|
| 111 |
-
if os.path.exists(FILENAME):
|
| 112 |
-
try:
|
| 113 |
-
os.remove(FILENAME)
|
| 114 |
-
except:
|
| 115 |
-
pass
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
if len(sys.argv) > 1 and sys.argv[1] == "backup":
|
|
|
|
| 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 |
+
# 直接下载最新文件
|
| 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 |
+
# 修改为完整恢复 /root/.openclaw 下所有内容
|
| 22 |
+
with tarfile.open(path, "r:gz") as tar:
|
| 23 |
+
tar.extractall(path="/root/.openclaw/")
|
| 24 |
+
print(f"Success: Restored from {FILENAME}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
return True
|
|
|
|
|
|
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
+
# 如果是第一次运行,仓库里没文件,报错是正常的
|
| 28 |
+
print(f"Restore Note: No existing backup found or error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def backup():
|
| 31 |
try:
|
| 32 |
if not repo_id or not token:
|
| 33 |
+
print("Skip Backup: HF_DATASET or HF_TOKEN not set")
|
|
|
|
|
|
|
|
|
|
| 34 |
return
|
| 35 |
|
| 36 |
+
# 修改为备份 /root/.openclaw 下全部文件和文件夹
|
| 37 |
with tarfile.open(FILENAME, "w:gz") as tar:
|
| 38 |
+
tar.add("/root/.openclaw", arcname=".")
|
| 39 |
|
| 40 |
+
# 上传并覆盖
|
| 41 |
api.upload_file(
|
| 42 |
path_or_fileobj=FILENAME,
|
| 43 |
path_in_repo=FILENAME,
|
|
|
|
| 45 |
repo_type="dataset",
|
| 46 |
token=token
|
| 47 |
)
|
| 48 |
+
print(f"Backup {FILENAME} Success (Overwritten).")
|
| 49 |
except Exception as e:
|
| 50 |
+
print(f"Backup Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
if len(sys.argv) > 1 and sys.argv[1] == "backup":
|