Spaces:
Runtime error
Runtime error
Update scripts/restore_from_dataset.py
Browse files
scripts/restore_from_dataset.py
CHANGED
|
@@ -2,10 +2,44 @@
|
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
import shutil
|
|
|
|
| 5 |
from huggingface_hub import HfApi, login, hf_hub_download
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def restore_latest_backup():
|
| 11 |
token = os.getenv("HF_TOKEN")
|
|
@@ -17,12 +51,21 @@ def restore_latest_backup():
|
|
| 17 |
api = HfApi()
|
| 18 |
|
| 19 |
try:
|
|
|
|
| 20 |
files = api.list_repo_files(repo_id=DATASET_REPO, repo_type="dataset")
|
| 21 |
-
db_files = [f for f in files if f.endswith(".db") and f.startswith(
|
|
|
|
| 22 |
if not db_files:
|
| 23 |
-
print("ℹ️ 数据集中没有找到备份文件
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
return
|
| 25 |
|
|
|
|
| 26 |
db_files.sort()
|
| 27 |
latest = db_files[-1]
|
| 28 |
print(f"🔄 发现最新备份: {latest},开始下载...")
|
|
@@ -34,7 +77,10 @@ def restore_latest_backup():
|
|
| 34 |
local_dir="/tmp"
|
| 35 |
)
|
| 36 |
|
|
|
|
| 37 |
os.makedirs(os.path.dirname(DB_TARGET), exist_ok=True)
|
|
|
|
|
|
|
| 38 |
shutil.copy(downloaded, DB_TARGET)
|
| 39 |
print(f"✅ 数据库恢复成功: {DB_TARGET} (来自备份 {latest})")
|
| 40 |
os.remove(downloaded)
|
|
|
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
import shutil
|
| 5 |
+
import datetime
|
| 6 |
from huggingface_hub import HfApi, login, hf_hub_download
|
| 7 |
|
| 8 |
+
# ===== 配置 =====
|
| 9 |
+
DB_TARGET = "/app/server/data/freeapi.db" # 本地数据库路径
|
| 10 |
+
DATASET_REPO = "lydgs/freellm-backup" # 你的数据集名称
|
| 11 |
+
BACKUP_PREFIX = "freeapi_backup" # 备份文件前缀
|
| 12 |
+
# ================
|
| 13 |
+
|
| 14 |
+
def upload_backup(db_path, dataset_repo, backup_prefix):
|
| 15 |
+
"""上传数据库作为备份,返回是否成功"""
|
| 16 |
+
if not os.path.exists(db_path):
|
| 17 |
+
print(f"⚠️ 本地数据库不存在: {db_path},无法创建备份")
|
| 18 |
+
return False
|
| 19 |
+
|
| 20 |
+
token = os.getenv("HF_TOKEN")
|
| 21 |
+
if not token:
|
| 22 |
+
print("❌ 环境变量 HF_TOKEN 未设置,无法创建备份")
|
| 23 |
+
return False
|
| 24 |
+
|
| 25 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 26 |
+
backup_name = f"{backup_prefix}_{timestamp}.db"
|
| 27 |
+
|
| 28 |
+
login(token=token)
|
| 29 |
+
api = HfApi()
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
api.upload_file(
|
| 33 |
+
path_or_fileobj=db_path,
|
| 34 |
+
path_in_repo=backup_name,
|
| 35 |
+
repo_id=dataset_repo,
|
| 36 |
+
repo_type="dataset"
|
| 37 |
+
)
|
| 38 |
+
print(f"✅ 初始备份创建成功: {backup_name}")
|
| 39 |
+
return True
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"❌ 创建初始备份失败: {e}")
|
| 42 |
+
return False
|
| 43 |
|
| 44 |
def restore_latest_backup():
|
| 45 |
token = os.getenv("HF_TOKEN")
|
|
|
|
| 51 |
api = HfApi()
|
| 52 |
|
| 53 |
try:
|
| 54 |
+
# 列出数据集中的所有文件
|
| 55 |
files = api.list_repo_files(repo_id=DATASET_REPO, repo_type="dataset")
|
| 56 |
+
db_files = [f for f in files if f.endswith(".db") and f.startswith(BACKUP_PREFIX)]
|
| 57 |
+
|
| 58 |
if not db_files:
|
| 59 |
+
print("ℹ️ 数据集中没有找到备份文件")
|
| 60 |
+
# 如果没有备份,但本地数据库已存在,则主动创建一份备份
|
| 61 |
+
if os.path.exists(DB_TARGET):
|
| 62 |
+
print("🔄 检测到本地数据库存在,将创建初始备份...")
|
| 63 |
+
upload_backup(DB_TARGET, DATASET_REPO, BACKUP_PREFIX)
|
| 64 |
+
else:
|
| 65 |
+
print("ℹ️ 本地数据库也不存在,跳过恢复")
|
| 66 |
return
|
| 67 |
|
| 68 |
+
# 有备份文件:取最新的进行恢复
|
| 69 |
db_files.sort()
|
| 70 |
latest = db_files[-1]
|
| 71 |
print(f"🔄 发现最新备份: {latest},开始下载...")
|
|
|
|
| 77 |
local_dir="/tmp"
|
| 78 |
)
|
| 79 |
|
| 80 |
+
# 确保目标目录存在
|
| 81 |
os.makedirs(os.path.dirname(DB_TARGET), exist_ok=True)
|
| 82 |
+
|
| 83 |
+
# 复制到目标位置
|
| 84 |
shutil.copy(downloaded, DB_TARGET)
|
| 85 |
print(f"✅ 数据库恢复成功: {DB_TARGET} (来自备份 {latest})")
|
| 86 |
os.remove(downloaded)
|