lydgs commited on
Commit
45bd58b
·
verified ·
1 Parent(s): 07bbcac

Update scripts/restore_from_dataset.py

Browse files
Files changed (1) hide show
  1. scripts/restore_from_dataset.py +2 -12
scripts/restore_from_dataset.py CHANGED
@@ -4,10 +4,8 @@ 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")
@@ -19,19 +17,16 @@ def restore_latest_backup():
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,
@@ -39,14 +34,9 @@ def restore_latest_backup():
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}")
 
4
  import shutil
5
  from huggingface_hub import HfApi, login, hf_hub_download
6
 
7
+ DB_TARGET = "/data/freeapi.db"
8
+ DATASET_REPO = "lydgs/freellm-backup" # 已改为你的用户名
 
 
9
 
10
  def restore_latest_backup():
11
  token = os.getenv("HF_TOKEN")
 
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("freeapi_backup_")]
22
  if not db_files:
23
  print("ℹ️ 数据集中没有找到备份文件,跳过恢复")
24
  return
25
 
 
26
  db_files.sort()
27
  latest = db_files[-1]
28
  print(f"🔄 发现最新备份: {latest},开始下载...")
29
 
 
30
  downloaded = hf_hub_download(
31
  repo_id=DATASET_REPO,
32
  filename=latest,
 
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)
41
  except Exception as e:
42
  print(f"❌ 数据库恢复失败: {e}")