wd21 commited on
Commit
e88676b
·
verified ·
1 Parent(s): 3221b2e

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +65 -6
Dockerfile CHANGED
@@ -14,13 +14,73 @@ ENV PORT=7860 \
14
  OPENCLAW_GATEWAY_MODE=local \
15
  HOME=/root
16
 
17
- # 备份恢复脚本 sync.py(与之前相同,此处省略以节省篇幅,请从之前版本复制)
18
- # 注意:需要完整复制 sync.py 内容,否则备份功能失效。
19
  RUN cat > /usr/local/bin/sync.py << 'SYNC_EOF'
20
- # 这里放你完整的 sync.py 内容(从你之前的稳定版本中复制)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  SYNC_EOF
22
 
23
- # 启动脚本
 
 
24
  RUN cat > /usr/local/bin/start-openclaw << 'EOF'
25
  #!/bin/bash
26
  set -e
@@ -134,11 +194,10 @@ GATEWAY_PID=$!
134
  # 等待网关完全启动
135
  sleep 8
136
 
137
- # 自动批准 Telegram 配对(提取配对码)
138
  echo "Starting Telegram auto-pairing..."
139
  for i in {1..10}; do
140
  PAIR_OUTPUT=$(openclaw pairing list 2>/dev/null || true)
141
- # 提取配对码(支持旧格式和表格格式)
142
  CODE=$(echo "$PAIR_OUTPUT" | grep -oP 'Pairing code: \K[A-Z0-9]{7,}' | head -1)
143
  if [ -z "$CODE" ]; then
144
  CODE=$(echo "$PAIR_OUTPUT" | awk '/^│ Code/ {getline; split($0, a, "│"); gsub(/^ +| +$/, "", a[2]); if (a[2] ~ /^[A-Z0-9]{7,}$/) print a[2]}' | head -1)
 
14
  OPENCLAW_GATEWAY_MODE=local \
15
  HOME=/root
16
 
17
+ # ==================== 完整备份恢复脚本 sync.py ====================
 
18
  RUN cat > /usr/local/bin/sync.py << 'SYNC_EOF'
19
+ import os, sys, tarfile
20
+ from huggingface_hub import HfApi, hf_hub_download
21
+ from datetime import datetime, timedelta
22
+ import subprocess
23
+ import time
24
+
25
+ api = HfApi()
26
+ repo_id = os.getenv("HF_DATASET")
27
+ token = os.getenv("HF_TOKEN")
28
+
29
+ def restore():
30
+ try:
31
+ print(f"--- [SYNC] 启动恢复流程, 目标仓库: {repo_id} ---")
32
+ if not repo_id or not token:
33
+ print("--- [SYNC] 跳过恢复: 未配置 HF_DATASET 或 HF_TOKEN ---")
34
+ return False
35
+ files = api.list_repo_files(repo_id=repo_id, repo_type="dataset", token=token)
36
+ now = datetime.now()
37
+ for i in range(5):
38
+ day = (now - timedelta(days=i)).strftime("%Y-%m-%d")
39
+ name = f"backup_{day}.tar.gz"
40
+ if name in files:
41
+ print(f"--- [SYNC] 发现备份文件: {name}, 正在下载... ---")
42
+ path = hf_hub_download(repo_id=repo_id, filename=name, repo_type="dataset", token=token)
43
+ with tarfile.open(path, "r:gz") as tar:
44
+ tar.extractall(path="/root/.openclaw/")
45
+ print(f"--- [SYNC] 恢复成功! 数据已覆盖至 /root/.openclaw/ ---")
46
+ return True
47
+ print("--- [SYNC] 未找到最近 5 天的备份包 ---")
48
+ except Exception as e:
49
+ print(f"--- [SYNC] 恢复异常: {e} ---")
50
+
51
+ def backup():
52
+ try:
53
+ day = datetime.now().strftime("%Y-%m-%d")
54
+ name = f"backup_{day}.tar.gz"
55
+ print(f"--- [SYNC] 正在执行全量备份: {name} ---")
56
+ with tarfile.open(name, "w:gz") as tar:
57
+ for target in ["sessions", "workspace", "agents", "memory", "openclaw.json", "wechat-data"]:
58
+ full_path = f"/root/.openclaw/{target}"
59
+ if os.path.exists(full_path):
60
+ tar.add(full_path, arcname=target)
61
+ api.upload_file(path_or_fileobj=name, path_in_repo=name, repo_id=repo_id, repo_type="dataset", token=token)
62
+ print(f"--- [SYNC] 备份上传成功! ---")
63
+ for _ in range(3):
64
+ try:
65
+ result = subprocess.run(["openclaw", "wechat", "send", "--to=me", f"备份成功: {name}"], capture_output=True, timeout=10)
66
+ if result.returncode == 0:
67
+ break
68
+ except Exception:
69
+ pass
70
+ time.sleep(5)
71
+ except Exception as e:
72
+ print(f"--- [SYNC] 备份失败: {e} ---")
73
+
74
+ if __name__ == "__main__":
75
+ if len(sys.argv) > 1 and sys.argv[1] == "backup":
76
+ backup()
77
+ else:
78
+ restore()
79
  SYNC_EOF
80
 
81
+ RUN chmod +x /usr/local/bin/sync.py
82
+
83
+ # 启动脚本 start-openclaw
84
  RUN cat > /usr/local/bin/start-openclaw << 'EOF'
85
  #!/bin/bash
86
  set -e
 
194
  # 等待网关完全启动
195
  sleep 8
196
 
197
+ # 自动批准 Telegram 配对
198
  echo "Starting Telegram auto-pairing..."
199
  for i in {1..10}; do
200
  PAIR_OUTPUT=$(openclaw pairing list 2>/dev/null || true)
 
201
  CODE=$(echo "$PAIR_OUTPUT" | grep -oP 'Pairing code: \K[A-Z0-9]{7,}' | head -1)
202
  if [ -z "$CODE" ]; then
203
  CODE=$(echo "$PAIR_OUTPUT" | awk '/^│ Code/ {getline; split($0, a, "│"); gsub(/^ +| +$/, "", a[2]); if (a[2] ~ /^[A-Z0-9]{7,}$/) print a[2]}' | head -1)