Spaces:
Sleeping
Sleeping
Create scripts/restore_from_dataset.py
Browse files
scripts/restore_from_dataset.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
import shutil
|
| 5 |
+
from huggingface_hub import HfApi, login, hf_hub_download
|
| 6 |
+
|
| 7 |
+
# ===== 配置 =====
|
| 8 |
+
DB_TARGET = "/data/freeapi.db" # 数据库最终存放位置
|
| 9 |
+
DATASET_REPO = "你的用户名/freellm-backup" # 替换为你的数据集名称
|
| 10 |
+
# ================
|
| 11 |
+
|
| 12 |
+
def restore_latest_backup():
|
| 13 |
+
token = os.getenv("HF_TOKEN")
|
| 14 |
+
if not token:
|
| 15 |
+
print("⚠️ 未设置 HF_TOKEN 环境变量,跳过数据库恢复")
|
| 16 |
+
return
|
| 17 |
+
|
| 18 |
+
login(token=token)
|
| 19 |
+
api = HfApi()
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
# 列出数据集中的所有文件
|
| 23 |
+
files = api.list_repo_files(repo_id=DATASET_REPO, repo_type="dataset")
|
| 24 |
+
db_files = [f for f in files if f.endswith(".db") and f.startswith("freeapi_backup_")]
|
| 25 |
+
if not db_files:
|
| 26 |
+
print("ℹ️ 数据集中没有找到备份文件,跳过恢复")
|
| 27 |
+
return
|
| 28 |
+
|
| 29 |
+
# 按文件名排序(时间戳升序),取最新的
|
| 30 |
+
db_files.sort()
|
| 31 |
+
latest = db_files[-1]
|
| 32 |
+
print(f"🔄 发现最新备份: {latest},开始下载...")
|
| 33 |
+
|
| 34 |
+
# 下载到临时目录
|
| 35 |
+
downloaded = hf_hub_download(
|
| 36 |
+
repo_id=DATASET_REPO,
|
| 37 |
+
filename=latest,
|
| 38 |
+
repo_type="dataset",
|
| 39 |
+
local_dir="/tmp"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# 确保目标目录存在
|
| 43 |
+
os.makedirs(os.path.dirname(DB_TARGET), exist_ok=True)
|
| 44 |
+
|
| 45 |
+
# 复制到目标位置
|
| 46 |
+
shutil.copy(downloaded, DB_TARGET)
|
| 47 |
+
print(f"✅ 数据库恢复成功: {DB_TARGET} (来自备份 {latest})")
|
| 48 |
+
|
| 49 |
+
# 可选:删除临时下载文件
|
| 50 |
+
os.remove(downloaded)
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"❌ 数据库恢复失败: {e}")
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
restore_latest_backup()
|