在huggingface上部署komari(webdav同步)

项目地址:

https://github.com/komari-monitor/komari

成品展示:

https://komari.xjfkkk.cloudns.org

1.创建Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
FROM ghcr.io/komari-monitor/komari:latest

WORKDIR /app

RUN apk add --update --no-cache python3 py3-pip curl lsof

RUN python3 -m venv /app/venv
ENV PATH="/app/venv/bin:$PATH"
RUN pip install --no-cache-dir requests webdavclient3

RUN mkdir -p /app/data && chmod -R 775 /app/data
RUN chmod -R 775 /app
RUN mkdir -p /app/sync

COPY sync_data.sh /app/sync/
RUN chmod +x /app/sync/sync_data.sh

EXPOSE 25774

CMD ["/bin/sh", "-c", "/app/sync/sync_data.sh & sleep 10 && exec /app/komari server"]

README.md 末尾添加

1
app_port: 25774

2.创建sync_data.sh文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/sh

VENV_PYTHON="/app/venv/bin/python"

if [ -z "$WEBDAV_URL" ] || [ -z "$WEBDAV_USERNAME" ] || [ -z "$WEBDAV_PASSWORD" ]; then
echo "Starting without sync functionality - missing WEBDAV_URL, WEBDAV_USERNAME, or WEBDAV_PASSWORD"
exit 0
fi

WEBDAV_BACKUP_PATH=${WEBDAV_BACKUP_PATH:-""}
FULL_WEBDAV_URL="${WEBDAV_URL}"
if [ -n "$WEBDAV_BACKUP_PATH" ]; then
FULL_WEBDAV_URL="${WEBDAV_URL}/${WEBDAV_BACKUP_PATH}"
fi

restore_backup() {
echo "开始从 WebDAV 下载最新备份..."
$VENV_PYTHON -c "
import sys
import os
import tarfile
import requests
import shutil
import subprocess
from webdav3.client import Client
options = {
'webdav_hostname': '${FULL_WEBDAV_URL}',
'webdav_login': '${WEBDAV_USERNAME}',
'webdav_password': '${WEBDAV_PASSWORD}'
}
client = Client(options)
try:
backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('komari_backup_')]
except Exception as e:
print(f'连接 WebDAV 服务器出错: {e}')
sys.exit(1)
if not backups:
print('没有找到备份文件,跳过恢复步骤。')
sys.exit(0)
latest_backup = sorted(backups)[-1]
print(f'最新备份文件:{latest_backup}')
try:
with requests.get(f'${FULL_WEBDAV_URL}/{latest_backup}', auth=('${WEBDAV_USERNAME}', '${WEBDAV_PASSWORD}'), stream=True) as r:
if r.status_code == 200:
with open(f'/tmp/{latest_backup}', 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
print(f'成功下载备份文件到 /tmp/{latest_backup}')
# 检查数据库文件是否被占用
try:
# 检查整个目录下的文件是否被占用(仅检测主要数据库文件,有需要可扩展)
pids = subprocess.check_output(['lsof', '-t', '/app/data/komari.db']).decode().strip()
if pids:
print('数据库文件被占用,无法恢复备份。')
sys.exit(1)
except subprocess.CalledProcessError:
pass
# 解压整个目录
with tarfile.open(f'/tmp/{latest_backup}', 'r:gz') as tar:
tar.extractall('/app/')
print(f'成功从 {latest_backup} 恢复备份到 /app/data')
else:
print(f'下载备份失败:{r.status_code}')
except Exception as e:
print(f'恢复备份过程中出错: {e}')
"
}

echo "Downloading latest backup from WebDAV..."
restore_backup

sync_data() {
while true; do
echo "Starting sync process at $(date)"
timestamp=$(date +%Y%m%d_%H%M%S)
backup_file="komari_backup_${timestamp}.tar.gz"

# 备份整个/app/data目录
cd /app
tar -czf "/tmp/${backup_file}" data

curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$FULL_WEBDAV_URL/${backup_file}"
if [ $? -eq 0 ]; then
echo "Successfully uploaded ${backup_file} to WebDAV"
else
echo "Failed to upload ${backup_file} to WebDAV"
fi

$VENV_PYTHON -c "
from webdav3.client import Client
options = {
'webdav_hostname': '${FULL_WEBDAV_URL}',
'webdav_login': '${WEBDAV_USERNAME}',
'webdav_password': '${WEBDAV_PASSWORD}'
}
client = Client(options)
backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('komari_backup_')]
backups.sort()
if len(backups) > 5:
to_delete = len(backups) - 5
for file in backups[:to_delete]:
client.clean(file)
print(f'Successfully deleted {file}.')
else:
print('Only {} backups found, no need to clean.'.format(len(backups)))
" 2>&1

rm -f "/tmp/${backup_file}"

SYNC_INTERVAL=${SYNC_INTERVAL:-600}
echo "Next sync in ${SYNC_INTERVAL} seconds..."
sleep $SYNC_INTERVAL
done
}

sync_data &

备份上限5份,同步时间默认600s

3.注册webdav并开启传输权限

https://infini-cloud.net/en/

image|690x210

4.填入相关变量

1
2
3
4
5
6
7
8
9
#面板变量
ADMIN_USERNAME # 登录用户名
ADMIN_PASSWORD # 登录密码
#备份变量
WEBDAV_URL # https://jike.teracloud.jp/dav
WEBDAV_BACKUP_PATH # 备份文件夹名(备份文件夹,需要自行在网盘中创建,如komari)
WEBDAV_USERNAME # 用户名
WEBDAV_PASSWORD # 连接密码,第3步获取到的
SYNC_INTERVAL # 同步时间,默认600

一定要是私人变量

5.在cloudflare进行反代

在cloudflare创建一个Workers,填入代码并修改将xxxx.hf.space替换为你的space域名
image|690x216

image|690x277

image|690x323

image|690x141

image|690x199

1
2
3
4
5
6
7
export default {
async fetch(request, env) {
const url = new URL(request.url);
url.host = 'xxxx.hf.space';
return fetch(new Request(url, request))
}
}

完成后添加自定义域名
image|690x284

image|690x154

image|272x500

6.设置github登录并禁用密码

新建一个 OAuth app
https://github.com/settings/applications/new

image|690x386

格式

1
2
3
4
5
# URL
https://example.com

# callback URL
https://example.com/api/oauth_callback

将获取到的Client IDClient secrets进行保存
image|690x331

登录komari后台
image|690x144

开启跨域请求
image|690x195

填写获取到的Client IDClient secrets
image|690x258

绑定github
image|667x500

关闭密码登录
image|690x177

7.更换主题

下载主题
https://github.com/Montia37/komari-theme-purcarte/archive/refs/tags/v1.1.6.zip
上传主题
image|690x233