Delete sync.py
Browse files
sync.py
DELETED
|
@@ -1,125 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
import os
|
| 3 |
-
import sys
|
| 4 |
-
import tarfile
|
| 5 |
-
from datetime import datetime, timedelta
|
| 6 |
-
from huggingface_hub import HfApi, hf_hub_download
|
| 7 |
-
|
| 8 |
-
api = HfApi()
|
| 9 |
-
repo_id = os.getenv("HF_DATASET")
|
| 10 |
-
token = os.getenv("HF_TOKEN")
|
| 11 |
-
|
| 12 |
-
# 配置(从环境变量读取,带默认值)
|
| 13 |
-
LOOKBACK_DAYS = int(os.getenv("BACKUP_LOOKBACK_DAYS", "10"))
|
| 14 |
-
KEEP_BACKUPS = int(os.getenv("KEEP_BACKUPS_COUNT", "10"))
|
| 15 |
-
BACKUP_TARGETS = os.getenv(
|
| 16 |
-
"BACKUP_TARGETS", "sessions,workspace,agents,memory,openclaw.json"
|
| 17 |
-
).split(",")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def log(msg):
|
| 21 |
-
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {msg}")
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def restore():
|
| 25 |
-
"""从 Hugging Face Hub 恢复最近10天的备份"""
|
| 26 |
-
try:
|
| 27 |
-
log(f"[SYNC] 启动恢复流程, 目标仓库: {repo_id}")
|
| 28 |
-
if not repo_id or not token:
|
| 29 |
-
log("[SYNC] 跳过恢复: 未配置 HF_DATASET 或 HF_TOKEN")
|
| 30 |
-
return False
|
| 31 |
-
|
| 32 |
-
files = api.list_repo_files(repo_id=repo_id, repo_type="dataset", token=token)
|
| 33 |
-
now = datetime.now()
|
| 34 |
-
|
| 35 |
-
for i in range(LOOKBACK_DAYS):
|
| 36 |
-
day = (now - timedelta(days=i)).strftime("%Y-%m-%d")
|
| 37 |
-
name = f"backup_{day}.tar.gz"
|
| 38 |
-
if name in files:
|
| 39 |
-
log(f"[SYNC] 发现备份文件: {name}, 正在下载...")
|
| 40 |
-
path = hf_hub_download(
|
| 41 |
-
repo_id=repo_id,
|
| 42 |
-
filename=name,
|
| 43 |
-
repo_type="dataset",
|
| 44 |
-
token=token,
|
| 45 |
-
)
|
| 46 |
-
with tarfile.open(path, "r:gz") as tar:
|
| 47 |
-
tar.extractall(path="/root/.openclaw/")
|
| 48 |
-
log("[SYNC] 恢复成功! 数据已覆盖至 /root/.openclaw/")
|
| 49 |
-
return True
|
| 50 |
-
|
| 51 |
-
log("[SYNC] 未找到最近10天的备份包")
|
| 52 |
-
return False
|
| 53 |
-
except Exception as e:
|
| 54 |
-
log(f"[SYNC] 恢复异常: {e}")
|
| 55 |
-
return False
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
def backup():
|
| 59 |
-
"""执行备份并清理旧备份"""
|
| 60 |
-
try:
|
| 61 |
-
day = datetime.now().strftime("%Y-%m-%d")
|
| 62 |
-
name = f"backup_{day}.tar.gz"
|
| 63 |
-
log(f"[SYNC] 正在执行全量备份: {name}")
|
| 64 |
-
|
| 65 |
-
# 在工作目录创建备份
|
| 66 |
-
work_dir = "/tmp/openclaw-backup"
|
| 67 |
-
os.makedirs(work_dir, exist_ok=True)
|
| 68 |
-
backup_path = os.path.join(work_dir, name)
|
| 69 |
-
|
| 70 |
-
with tarfile.open(backup_path, "w:gz") as tar:
|
| 71 |
-
targets = [t.strip() for t in BACKUP_TARGETS if t.strip()]
|
| 72 |
-
for target in targets:
|
| 73 |
-
full_path = f"/root/.openclaw/{target}"
|
| 74 |
-
if os.path.exists(full_path):
|
| 75 |
-
tar.add(full_path, arcname=target)
|
| 76 |
-
|
| 77 |
-
# 上传备份
|
| 78 |
-
api.upload_file(
|
| 79 |
-
path_or_fileobj=backup_path,
|
| 80 |
-
path_in_repo=name,
|
| 81 |
-
repo_id=repo_id,
|
| 82 |
-
repo_type="dataset",
|
| 83 |
-
token=token,
|
| 84 |
-
)
|
| 85 |
-
log("[SYNC] 备份上传成功!")
|
| 86 |
-
|
| 87 |
-
# 清理旧备份
|
| 88 |
-
cleanup_old_backups()
|
| 89 |
-
|
| 90 |
-
# 清理临时文件
|
| 91 |
-
os.remove(backup_path)
|
| 92 |
-
return True
|
| 93 |
-
|
| 94 |
-
except Exception as e:
|
| 95 |
-
log(f"[SYNC] 备份失败: {e}")
|
| 96 |
-
return False
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
def cleanup_old_backups():
|
| 100 |
-
"""保留最近10个备份,删除旧的"""
|
| 101 |
-
try:
|
| 102 |
-
files = api.list_repo_files(repo_id=repo_id, repo_type="dataset", token=token)
|
| 103 |
-
backups = [
|
| 104 |
-
f for f in files if f.startswith("backup_") and f.endswith(".tar.gz")
|
| 105 |
-
]
|
| 106 |
-
backups.sort(reverse=True)
|
| 107 |
-
|
| 108 |
-
if len(backups) > KEEP_BACKUPS:
|
| 109 |
-
for old_backup in backups[KEEP_BACKUPS:]:
|
| 110 |
-
log(f"[SYNC] 清理旧备份: {old_backup}")
|
| 111 |
-
api.delete_file(
|
| 112 |
-
path_in_repo=old_backup,
|
| 113 |
-
repo_id=repo_id,
|
| 114 |
-
repo_type="dataset",
|
| 115 |
-
token=token,
|
| 116 |
-
)
|
| 117 |
-
except Exception as e:
|
| 118 |
-
log(f"[SYNC] 清理旧备份失败: {e}")
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
if __name__ == "__main__":
|
| 122 |
-
if len(sys.argv) > 1 and sys.argv[1] == "backup":
|
| 123 |
-
backup()
|
| 124 |
-
else:
|
| 125 |
-
restore()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|