Update Dockerfile
Browse files- Dockerfile +29 -7
Dockerfile
CHANGED
|
@@ -6,7 +6,7 @@ RUN apk add --no-cache python3 py3-pip curl socat tar && \
|
|
| 6 |
pip3 install --no-cache-dir --break-system-packages requests huggingface_hub && \
|
| 7 |
rm -rf /var/cache/apk/*
|
| 8 |
|
| 9 |
-
# ========= 备份恢复脚本 sync.py(
|
| 10 |
RUN cat > /usr/local/bin/sync.py << 'SYNC_EOF'
|
| 11 |
#!/usr/bin/env python3
|
| 12 |
import os, sys, tarfile, tempfile, time
|
|
@@ -36,12 +36,22 @@ def validate_hf_access(repo_id, token):
|
|
| 36 |
log("仓库创建成功")
|
| 37 |
else:
|
| 38 |
raise PermissionError(f"访问仓库失败: {e}")
|
|
|
|
| 39 |
test_file = tempfile.NamedTemporaryFile(mode="w", delete=False)
|
| 40 |
test_file.write("test")
|
| 41 |
test_file.close()
|
| 42 |
try:
|
| 43 |
-
api.upload_file(
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
finally:
|
| 46 |
os.unlink(test_file.name)
|
| 47 |
|
|
@@ -75,7 +85,13 @@ def restore():
|
|
| 75 |
log(f"找到备份: {latest},开始下载")
|
| 76 |
for attempt in range(RETRY_COUNT):
|
| 77 |
try:
|
| 78 |
-
local = api.hf_hub_download(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
break
|
| 80 |
except Exception as e:
|
| 81 |
log(f"下载失败 ({attempt+1}/{RETRY_COUNT}): {e}")
|
|
@@ -116,7 +132,13 @@ def backup():
|
|
| 116 |
if os.path.exists(src):
|
| 117 |
tar.add(src, arcname=cfg)
|
| 118 |
api = HfApi()
|
| 119 |
-
api.upload_file(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
log(f"备份 {backup_name} 上传成功")
|
| 121 |
# 清理旧备份
|
| 122 |
files = api.list_repo_files(repo_id=repo_id, repo_type="dataset", token=token)
|
|
@@ -126,7 +148,7 @@ def backup():
|
|
| 126 |
date_str = f[len(BACKUP_PREFIX):-7]
|
| 127 |
try:
|
| 128 |
if datetime.strptime(date_str, "%Y-%m-%d") < cutoff:
|
| 129 |
-
api.delete_file(f, repo_id=repo_id, repo_type="dataset", token=token)
|
| 130 |
log(f"删除旧备份: {f}")
|
| 131 |
except: pass
|
| 132 |
os.unlink(tmp.name)
|
|
@@ -140,7 +162,7 @@ SYNC_EOF
|
|
| 140 |
|
| 141 |
RUN chmod +x /usr/local/bin/sync.py
|
| 142 |
|
| 143 |
-
# ========= 启动脚本
|
| 144 |
RUN printf '#!/bin/sh\n\
|
| 145 |
set -e\n\
|
| 146 |
\n\
|
|
|
|
| 6 |
pip3 install --no-cache-dir --break-system-packages requests huggingface_hub && \
|
| 7 |
rm -rf /var/cache/apk/*
|
| 8 |
|
| 9 |
+
# ========= 备份恢复脚本 sync.py(修复 API 调用错误) =========
|
| 10 |
RUN cat > /usr/local/bin/sync.py << 'SYNC_EOF'
|
| 11 |
#!/usr/bin/env python3
|
| 12 |
import os, sys, tarfile, tempfile, time
|
|
|
|
| 36 |
log("仓库创建成功")
|
| 37 |
else:
|
| 38 |
raise PermissionError(f"访问仓库失败: {e}")
|
| 39 |
+
# 测试写入权限(使用关键字参数)
|
| 40 |
test_file = tempfile.NamedTemporaryFile(mode="w", delete=False)
|
| 41 |
test_file.write("test")
|
| 42 |
test_file.close()
|
| 43 |
try:
|
| 44 |
+
api.upload_file(
|
| 45 |
+
path_or_fileobj=test_file.name,
|
| 46 |
+
path_in_repo=".write_test",
|
| 47 |
+
repo_id=repo_id,
|
| 48 |
+
repo_type="dataset",
|
| 49 |
+
token=token
|
| 50 |
+
)
|
| 51 |
+
api.delete_file(path_in_repo=".write_test", repo_id=repo_id, repo_type="dataset", token=token)
|
| 52 |
+
log("写入权限验证通过")
|
| 53 |
+
except Exception as e:
|
| 54 |
+
raise PermissionError(f"写入权限不足: {e}")
|
| 55 |
finally:
|
| 56 |
os.unlink(test_file.name)
|
| 57 |
|
|
|
|
| 85 |
log(f"找到备份: {latest},开始下载")
|
| 86 |
for attempt in range(RETRY_COUNT):
|
| 87 |
try:
|
| 88 |
+
local = api.hf_hub_download(
|
| 89 |
+
repo_id=repo_id,
|
| 90 |
+
filename=latest,
|
| 91 |
+
repo_type="dataset",
|
| 92 |
+
token=token,
|
| 93 |
+
resume=True
|
| 94 |
+
)
|
| 95 |
break
|
| 96 |
except Exception as e:
|
| 97 |
log(f"下载失败 ({attempt+1}/{RETRY_COUNT}): {e}")
|
|
|
|
| 132 |
if os.path.exists(src):
|
| 133 |
tar.add(src, arcname=cfg)
|
| 134 |
api = HfApi()
|
| 135 |
+
api.upload_file(
|
| 136 |
+
path_or_fileobj=tmp.name,
|
| 137 |
+
path_in_repo=backup_name,
|
| 138 |
+
repo_id=repo_id,
|
| 139 |
+
repo_type="dataset",
|
| 140 |
+
token=token
|
| 141 |
+
)
|
| 142 |
log(f"备份 {backup_name} 上传成功")
|
| 143 |
# 清理旧备份
|
| 144 |
files = api.list_repo_files(repo_id=repo_id, repo_type="dataset", token=token)
|
|
|
|
| 148 |
date_str = f[len(BACKUP_PREFIX):-7]
|
| 149 |
try:
|
| 150 |
if datetime.strptime(date_str, "%Y-%m-%d") < cutoff:
|
| 151 |
+
api.delete_file(path_in_repo=f, repo_id=repo_id, repo_type="dataset", token=token)
|
| 152 |
log(f"删除旧备份: {f}")
|
| 153 |
except: pass
|
| 154 |
os.unlink(tmp.name)
|
|
|
|
| 162 |
|
| 163 |
RUN chmod +x /usr/local/bin/sync.py
|
| 164 |
|
| 165 |
+
# ========= 启动脚本 =========
|
| 166 |
RUN printf '#!/bin/sh\n\
|
| 167 |
set -e\n\
|
| 168 |
\n\
|