Spaces:
Running
Running
Update Dockerfile
Browse files- Dockerfile +62 -8
Dockerfile
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
RUN cat > /app/download_server.js << 'EOF'
|
| 2 |
const http = require('http');
|
| 3 |
const fs = require('fs');
|
|
@@ -18,23 +32,18 @@ function safeResolvePath(filename) {
|
|
| 18 |
return null;
|
| 19 |
}
|
| 20 |
|
| 21 |
-
// 执行一次:sqlite 一致性快照 -> 上传 Google Drive
|
| 22 |
function runBackup(cb) {
|
| 23 |
const src = '/root/.omniroute/storage.sqlite';
|
| 24 |
const tmp = '/data/omni_storage.sqlite';
|
| 25 |
if (!fs.existsSync(src)) return cb(new Error('storage.sqlite 不存在'));
|
| 26 |
-
|
| 27 |
execFile('sqlite3', [src, `.backup '${tmp}'`], (e1) => {
|
| 28 |
if (e1) return cb(new Error('sqlite 快照失败: ' + e1.message));
|
| 29 |
execFile('rclone', ['copyto', tmp, `${GD_REMOTE}/omni_storage.sqlite`], (e2) => {
|
| 30 |
if (e2) return cb(new Error('rclone 上传失败: ' + e2.message));
|
| 31 |
-
// settings.json 一并备份(存在才传)
|
| 32 |
const setSrc = '/root/.omniroute/settings.json';
|
| 33 |
if (fs.existsSync(setSrc)) {
|
| 34 |
execFile('rclone', ['copyto', setSrc, `${GD_REMOTE}/omni_settings.json`], () => cb(null));
|
| 35 |
-
} else {
|
| 36 |
-
cb(null);
|
| 37 |
-
}
|
| 38 |
});
|
| 39 |
});
|
| 40 |
}
|
|
@@ -43,7 +52,6 @@ const server = http.createServer((req, res) => {
|
|
| 43 |
const url = new URL(req.url, `http://localhost:${PORT}`);
|
| 44 |
const route = url.pathname;
|
| 45 |
|
| 46 |
-
// ── 手动触发备份 ──
|
| 47 |
if (route === '/backup') {
|
| 48 |
runBackup((err) => {
|
| 49 |
if (err) {
|
|
@@ -90,7 +98,6 @@ const server = http.createServer((req, res) => {
|
|
| 90 |
return;
|
| 91 |
}
|
| 92 |
|
| 93 |
-
// 其余反向代理给 omniroute
|
| 94 |
const proxyReq = http.request(
|
| 95 |
{ hostname: '127.0.0.1', port: UPSTREAM_PORT, path: req.url, method: req.method, headers: req.headers },
|
| 96 |
proxyRes => { res.writeHead(proxyRes.statusCode, proxyRes.headers); proxyRes.pipe(res); }
|
|
@@ -106,3 +113,50 @@ server.listen(PORT, '0.0.0.0', () => {
|
|
| 106 |
console.log(`[server] listening ${PORT}; routes /list /download /backup, proxy -> ${UPSTREAM_PORT}`);
|
| 107 |
});
|
| 108 |
EOF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM node:22-alpine
|
| 2 |
+
|
| 3 |
+
RUN apk add --no-cache sqlite rclone
|
| 4 |
+
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
RUN npm install -g omniroute
|
| 7 |
+
|
| 8 |
+
ENV PORT=7860
|
| 9 |
+
ENV HOST=0.0.0.0
|
| 10 |
+
ENV NODE_ENV=production
|
| 11 |
+
|
| 12 |
+
EXPOSE 7860
|
| 13 |
+
|
| 14 |
+
# ───────── 下载 + 备份 + 反向代理服务(监听 7860)─────────
|
| 15 |
RUN cat > /app/download_server.js << 'EOF'
|
| 16 |
const http = require('http');
|
| 17 |
const fs = require('fs');
|
|
|
|
| 32 |
return null;
|
| 33 |
}
|
| 34 |
|
|
|
|
| 35 |
function runBackup(cb) {
|
| 36 |
const src = '/root/.omniroute/storage.sqlite';
|
| 37 |
const tmp = '/data/omni_storage.sqlite';
|
| 38 |
if (!fs.existsSync(src)) return cb(new Error('storage.sqlite 不存在'));
|
|
|
|
| 39 |
execFile('sqlite3', [src, `.backup '${tmp}'`], (e1) => {
|
| 40 |
if (e1) return cb(new Error('sqlite 快照失败: ' + e1.message));
|
| 41 |
execFile('rclone', ['copyto', tmp, `${GD_REMOTE}/omni_storage.sqlite`], (e2) => {
|
| 42 |
if (e2) return cb(new Error('rclone 上传失败: ' + e2.message));
|
|
|
|
| 43 |
const setSrc = '/root/.omniroute/settings.json';
|
| 44 |
if (fs.existsSync(setSrc)) {
|
| 45 |
execFile('rclone', ['copyto', setSrc, `${GD_REMOTE}/omni_settings.json`], () => cb(null));
|
| 46 |
+
} else { cb(null); }
|
|
|
|
|
|
|
| 47 |
});
|
| 48 |
});
|
| 49 |
}
|
|
|
|
| 52 |
const url = new URL(req.url, `http://localhost:${PORT}`);
|
| 53 |
const route = url.pathname;
|
| 54 |
|
|
|
|
| 55 |
if (route === '/backup') {
|
| 56 |
runBackup((err) => {
|
| 57 |
if (err) {
|
|
|
|
| 98 |
return;
|
| 99 |
}
|
| 100 |
|
|
|
|
| 101 |
const proxyReq = http.request(
|
| 102 |
{ hostname: '127.0.0.1', port: UPSTREAM_PORT, path: req.url, method: req.method, headers: req.headers },
|
| 103 |
proxyRes => { res.writeHead(proxyRes.statusCode, proxyRes.headers); proxyRes.pipe(res); }
|
|
|
|
| 113 |
console.log(`[server] listening ${PORT}; routes /list /download /backup, proxy -> ${UPSTREAM_PORT}`);
|
| 114 |
});
|
| 115 |
EOF
|
| 116 |
+
|
| 117 |
+
# ───────── 启动脚本 ─────────
|
| 118 |
+
RUN cat > /app/entrypoint.sh << 'EOF'
|
| 119 |
+
#!/bin/sh
|
| 120 |
+
set -u
|
| 121 |
+
|
| 122 |
+
mkdir -p /root/.omniroute /data /root/.config/rclone
|
| 123 |
+
|
| 124 |
+
# rclone 配置
|
| 125 |
+
if [ -n "${RCLONE_CONF:-}" ]; then
|
| 126 |
+
printf '%s\n' "$RCLONE_CONF" > /root/.config/rclone/rclone.conf
|
| 127 |
+
echo "✅ 已写入 rclone 配置"
|
| 128 |
+
else
|
| 129 |
+
echo "⚠️ 未设置 RCLONE_CONF,Google Drive 不可用"
|
| 130 |
+
fi
|
| 131 |
+
|
| 132 |
+
# 固定加密 key
|
| 133 |
+
if [ -n "${STORAGE_ENCRYPTION_KEY:-}" ]; then
|
| 134 |
+
echo "STORAGE_ENCRYPTION_KEY=$STORAGE_ENCRYPTION_KEY" > /root/.omniroute/.env
|
| 135 |
+
echo "✅ 已写入固定 STORAGE_ENCRYPTION_KEY"
|
| 136 |
+
else
|
| 137 |
+
echo "⚠️ 未设置 STORAGE_ENCRYPTION_KEY,恢复的库可能解不开!"
|
| 138 |
+
fi
|
| 139 |
+
|
| 140 |
+
GD_REMOTE="om:om-backup"
|
| 141 |
+
|
| 142 |
+
# 开机从 Google Drive 恢复
|
| 143 |
+
if [ -n "${RCLONE_CONF:-}" ]; then
|
| 144 |
+
if rclone copyto "$GD_REMOTE/omni_storage.sqlite" /root/.omniroute/storage.sqlite --no-traverse 2>/dev/null; then
|
| 145 |
+
echo "✅ 从 GDrive 恢复 storage.sqlite"
|
| 146 |
+
rm -f /root/.omniroute/storage.sqlite-wal /root/.omniroute/storage.sqlite-shm
|
| 147 |
+
else
|
| 148 |
+
echo "⚠️ GDrive 无 storage 备份,跳过恢复"
|
| 149 |
+
fi
|
| 150 |
+
if rclone copyto "$GD_REMOTE/omni_settings.json" /root/.omniroute/settings.json --no-traverse 2>/dev/null; then
|
| 151 |
+
echo "✅ 从 GDrive 恢复 settings.json"
|
| 152 |
+
else
|
| 153 |
+
echo "⚠️ GDrive 无 settings 备份,跳过恢复"
|
| 154 |
+
fi
|
| 155 |
+
fi
|
| 156 |
+
|
| 157 |
+
node /app/download_server.js &
|
| 158 |
+
exec env PORT=8860 omniroute
|
| 159 |
+
EOF
|
| 160 |
+
RUN chmod +x /app/entrypoint.sh
|
| 161 |
+
|
| 162 |
+
CMD ["/app/entrypoint.sh"]
|