chatyou commited on
Commit
9fb0402
·
verified ·
1 Parent(s): e429936

Create sync.py

Browse files
Files changed (1) hide show
  1. sync.py +65 -0
sync.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """从 Hugging Face Dataset 下载并解压备份,恢复数据"""
13
+ try:
14
+ if not repo_id or not token:
15
+ print("跳过恢复: HF_DATASET 或 HF_TOKEN 未设置")
16
+ return
17
+ print(f"正在从 {repo_id} 下载 {FILENAME}...")
18
+ # 下载文件
19
+ path = hf_hub_download(repo_id=repo_id, filename=FILENAME, repo_type="dataset", token=token)
20
+ # 解压到 OpenClaw 的数据目录
21
+ with tarfile.open(path, "r:gz") as tar:
22
+ tar.extractall(path="/root/.openclaw/")
23
+ print(f"成功从 {FILENAME} 恢复数据。")
24
+ return True
25
+ except Exception as e:
26
+ # 如果是第一次运行,仓库里可能没有备份文件,报错是正常的
27
+ print(f"恢复说明: 未找到现有备份或发生错误: {e}")
28
+
29
+ def backup():
30
+ """将 OpenClaw 的关键数据打包并上传到 Hugging Face Dataset"""
31
+ try:
32
+ if not repo_id or not token:
33
+ print("跳过备份: HF_DATASET 或 HF_TOKEN 未设置")
34
+ return
35
+ print("正在创建数据备份包...")
36
+ with tarfile.open(FILENAME, "w:gz") as tar:
37
+ # 需要备份的关键路径列表
38
+ paths_to_backup = [
39
+ "/root/.openclaw/sessions",
40
+ "/root/.openclaw/agents/main/sessions",
41
+ "/root/.openclaw/openclaw.json"
42
+ ]
43
+ for p in paths_to_backup:
44
+ if os.path.exists(p):
45
+ # 计算在压缩包内的相对路径
46
+ arcname = p.replace("/root/.openclaw/", "")
47
+ tar.add(p, arcname=arcname)
48
+ print(f" 已添加: {p}")
49
+ # 上传文件,覆盖已有文件
50
+ api.upload_file(
51
+ path_or_fileobj=FILENAME,
52
+ path_in_repo=FILENAME,
53
+ repo_id=repo_id,
54
+ repo_type="dataset",
55
+ token=token
56
+ )
57
+ print(f"备份 {FILENAME} 成功上传(已覆盖)。")
58
+ except Exception as e:
59
+ print(f"备份错误: {e}")
60
+
61
+ if __name__ == "__main__":
62
+ if len(sys.argv) > 1 and sys.argv[1] == "backup":
63
+ backup()
64
+ else:
65
+ restore()