Spaces:
Running
Running
Create backup.py
Browse files
backup.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
from huggingface_hub import HfApi
|
3 |
+
import sys
|
4 |
+
import os
|
5 |
+
import shutil
|
6 |
+
import time
|
7 |
+
import glob
|
8 |
+
from datetime import datetime
|
9 |
+
import tempfile
|
10 |
+
|
11 |
+
def manage_backups(api, repo_id, max_files=10):
|
12 |
+
"""管理备份文件数量,删除旧的备份"""
|
13 |
+
try:
|
14 |
+
files = api.list_repo_files(repo_id=repo_id, repo_type="dataset")
|
15 |
+
backup_files = [f for f in files if f.startswith('navidrome_backup_') and f.endswith('.tar.gz')]
|
16 |
+
backup_files.sort()
|
17 |
+
|
18 |
+
if len(backup_files) >= max_files:
|
19 |
+
files_to_delete = backup_files[:(len(backup_files) - max_files + 1)]
|
20 |
+
for file_to_delete in files_to_delete:
|
21 |
+
try:
|
22 |
+
api.delete_file(path_in_repo=file_to_delete, repo_id=repo_id, repo_type="dataset")
|
23 |
+
print(f'已删除旧备份: {file_to_delete}')
|
24 |
+
except Exception as e:
|
25 |
+
print(f'删除 {file_to_delete} 时出错: {str(e)}')
|
26 |
+
except Exception as e:
|
27 |
+
print(f'管理备份时出错: {str(e)}')
|
28 |
+
|
29 |
+
def upload_backup(file_path, file_name, token, repo_id):
|
30 |
+
"""上传备份文件到HuggingFace"""
|
31 |
+
api = HfApi(token=token)
|
32 |
+
try:
|
33 |
+
api.upload_file(
|
34 |
+
path_or_fileobj=file_path,
|
35 |
+
path_in_repo=file_name,
|
36 |
+
repo_id=repo_id,
|
37 |
+
repo_type="dataset"
|
38 |
+
)
|
39 |
+
print(f"成功上传 {file_name}")
|
40 |
+
|
41 |
+
manage_backups(api, repo_id)
|
42 |
+
except Exception as e:
|
43 |
+
print(f"文件上传出错: {str(e)}")
|
44 |
+
|
45 |
+
def download_latest_backup(token, repo_id, data_dir):
|
46 |
+
"""下载最新的备份文件并恢复"""
|
47 |
+
try:
|
48 |
+
# 确保目标目录存在并有正确权限
|
49 |
+
os.makedirs(data_dir, exist_ok=True)
|
50 |
+
os.chmod(data_dir, 0o755)
|
51 |
+
|
52 |
+
api = HfApi(token=token)
|
53 |
+
files = api.list_repo_files(repo_id=repo_id, repo_type="dataset")
|
54 |
+
backup_files = [f for f in files if f.startswith('navidrome_backup_') and f.endswith('.tar.gz')]
|
55 |
+
|
56 |
+
if not backup_files:
|
57 |
+
print("未找到备份文件")
|
58 |
+
return False
|
59 |
+
|
60 |
+
latest_backup = sorted(backup_files)[-1]
|
61 |
+
|
62 |
+
# 使用临时目录下载文件
|
63 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
64 |
+
filepath = api.hf_hub_download(
|
65 |
+
repo_id=repo_id,
|
66 |
+
filename=latest_backup,
|
67 |
+
repo_type="dataset",
|
68 |
+
local_dir=temp_dir
|
69 |
+
)
|
70 |
+
|
71 |
+
if filepath and os.path.exists(filepath):
|
72 |
+
# 解压备份文件
|
73 |
+
import tarfile
|
74 |
+
with tarfile.open(filepath, "r:gz") as tar:
|
75 |
+
tar.extractall(path=data_dir)
|
76 |
+
|
77 |
+
print(f"成功从 {latest_backup} 恢复备份到 {data_dir}")
|
78 |
+
return True
|
79 |
+
return False
|
80 |
+
|
81 |
+
except Exception as e:
|
82 |
+
print(f"下载备份时出错: {str(e)}")
|
83 |
+
return False
|
84 |
+
|
85 |
+
def create_backup(data_dir, exclude_dirs=None):
|
86 |
+
"""创建备份文件,排除指定目录"""
|
87 |
+
if exclude_dirs is None:
|
88 |
+
exclude_dirs = []
|
89 |
+
|
90 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
91 |
+
backup_filename = f"navidrome_backup_{timestamp}.tar.gz"
|
92 |
+
backup_path = f"/tmp/{backup_filename}"
|
93 |
+
|
94 |
+
try:
|
95 |
+
import tarfile
|
96 |
+
with tarfile.open(backup_path, "w:gz") as tar:
|
97 |
+
for item in os.listdir(data_dir):
|
98 |
+
item_path = os.path.join(data_dir, item)
|
99 |
+
# 排除指定目录
|
100 |
+
if os.path.isdir(item_path) and item in exclude_dirs:
|
101 |
+
continue
|
102 |
+
tar.add(item_path, arcname=item)
|
103 |
+
|
104 |
+
print(f"备份文件创建成功: {backup_path}")
|
105 |
+
return backup_path, backup_filename
|
106 |
+
except Exception as e:
|
107 |
+
print(f"创建备份时出错: {str(e)}")
|
108 |
+
return None, None
|
109 |
+
|
110 |
+
if __name__ == "__main__":
|
111 |
+
action = sys.argv[1]
|
112 |
+
token = sys.argv[2]
|
113 |
+
repo_id = sys.argv[3]
|
114 |
+
data_dir = sys.argv[4]
|
115 |
+
|
116 |
+
if action == "upload":
|
117 |
+
# 排除音乐目录和缓存,只备份数据和配置
|
118 |
+
exclude_dirs = ["cache", "music"]
|
119 |
+
backup_path, backup_filename = create_backup(data_dir, exclude_dirs)
|
120 |
+
if backup_path:
|
121 |
+
upload_backup(backup_path, backup_filename, token, repo_id)
|
122 |
+
# 清理临时文件
|
123 |
+
os.remove(backup_path)
|
124 |
+
elif action == "download":
|
125 |
+
download_latest_backup(token, repo_id, data_dir)
|