feat: 新增备份与恢复页(面板数据 + compose 配置,可下载)
This commit is contained in:
1 parent
4a68439f2c
commit
1791e94028
5 files changed
+178
-1
No files matched your search
+49
-1
@@ -92,7 +92,7 @@ function toast(msg, err) {
|
||||
}
|
||||
|
||||
/* ---------------- 路由 ---------------- */
|
||||
const TITLES = { apps: '概览', 'apps-all': '全部应用', containers: '运行的容器', stopped: '停止的容器', images: '本地镜像', registry: '镜像仓库', networks: '网络', settings: '告警设置' };
|
||||
const TITLES = { apps: '概览', 'apps-all': '全部应用', containers: '运行的容器', stopped: '停止的容器', images: '本地镜像', registry: '镜像仓库', networks: '网络', backup: '备份与恢复', settings: '告警设置' };
|
||||
const SORTS = {
|
||||
apps: [['severity', '异常优先'], ['cpu', 'CPU 占用'], ['mem', '内存占用'], ['containers', '容器数'], ['name', '名称']],
|
||||
containers: [['cpu', 'CPU 占用'], ['mem', '内存占用'], ['net', '网络速率'], ['io', '磁盘 IO'], ['pids', '进程数'], ['name', '名称']],
|
||||
@@ -133,6 +133,7 @@ async function render(silent) {
|
||||
if (state.view === 'images') return renderImages();
|
||||
if (state.view === 'registry') return renderRegistry();
|
||||
if (state.view === 'networks') return renderNetworks();
|
||||
if (state.view === 'backup') return renderBackup();
|
||||
if (state.view === 'settings') return renderSettings();
|
||||
} catch (e) { if (!silent) $('#view').innerHTML = `<div class="empty">加载失败:${esc(e.message)}</div>`; }
|
||||
}
|
||||
@@ -1087,6 +1088,53 @@ async function renderNetworks() {
|
||||
});
|
||||
}
|
||||
|
||||
async function renderBackup() {
|
||||
const data = await api('/api/backup');
|
||||
const items = data.items || [];
|
||||
$('#view').innerHTML = `
|
||||
<div class="card net-new">
|
||||
<div class="sec-head" style="border:none;margin:0 0 8px"><b>创建备份</b><span class="muted">打包成 tar.gz,保存在 ${esc(data.dir || '/data/backups')}</span></div>
|
||||
<label class="chkline" style="display:flex;gap:8px;margin-bottom:8px"><input type="checkbox" id="bkPanel" checked /> 面板数据(告警设置、事件、流量/CPU/内存历史账本)</label>
|
||||
<label class="chkline" style="display:flex;gap:8px;margin-bottom:10px"><input type="checkbox" id="bkApps" /> Compose 应用配置文件(各项目目录下的 compose/.env 等,<b>不含数据卷</b>,单文件 2MB 以内)</label>
|
||||
<div style="display:flex;gap:8px;align-items:center">
|
||||
<button class="btn primary" id="bkCreate">立即备份</button>
|
||||
<span class="muted" id="bkHint">备份期间请不要关闭页面</span>
|
||||
</div>
|
||||
</div>
|
||||
<table class="tb"><thead><tr><th>备份文件</th><th class="num">大小</th><th>创建时间</th><th></th></tr></thead><tbody>
|
||||
${items.map(b => `<tr>
|
||||
<td><b>${esc(b.name)}</b></td>
|
||||
<td class="num">${fmtBytes(b.size)}</td>
|
||||
<td class="muted">${new Date(b.time).toLocaleString('zh-CN')}</td>
|
||||
<td class="img-acts">
|
||||
<button class="btn ghost sm" data-bkdl="${esc(b.name)}">下载</button>
|
||||
<button class="btn danger sm" data-bkdel="${esc(b.name)}">删除</button>
|
||||
</td>
|
||||
</tr>`).join('') || '<tr><td colspan="4" class="muted">还没有备份</td></tr>'}
|
||||
</tbody></table>
|
||||
<div class="foot">共 ${items.length} 个备份 · 恢复方法:解压后把 panel/ 内容放回 /data,apps/<项目名>/ 放回 compose 目录,再 <code>docker compose up -d</code></div>`;
|
||||
|
||||
$('#bkCreate').onclick = async () => {
|
||||
const btn = $('#bkCreate');
|
||||
btn.disabled = true; btn.textContent = '打包中…'; $('#bkHint').textContent = '正在打包,数据多时需要几十秒';
|
||||
try {
|
||||
const r = await api('/api/backup', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ panel: $('#bkPanel').checked, apps: $('#bkApps').checked }) });
|
||||
toast(`已生成备份 ${r.name}(${fmtBytes(r.size)}${r.apps ? ` · ${r.apps} 个应用配置` : ''})`);
|
||||
setTimeout(render, 500);
|
||||
} catch (e) { toast(e.message, true); btn.disabled = false; btn.textContent = '立即备份'; $('#bkHint').textContent = '备份失败'; }
|
||||
};
|
||||
$$('#view [data-bkdl]').forEach(b => b.onclick = () => {
|
||||
const a = document.createElement('a');
|
||||
a.href = `/api/backup/${encodeURIComponent(b.dataset.bkdl)}/download`;
|
||||
document.body.appendChild(a); a.click(); a.remove();
|
||||
});
|
||||
$$('#view [data-bkdel]').forEach(b => b.onclick = async () => {
|
||||
if (!confirm('确定删除该备份文件?')) return;
|
||||
try { await api(`/api/backup/${encodeURIComponent(b.dataset.bkdel)}`, { method: 'DELETE' }); toast('已删除'); render(); }
|
||||
catch (e) { toast(e.message, true); }
|
||||
});
|
||||
}
|
||||
|
||||
async function renderSettings() {
|
||||
const s = await api('/api/settings');
|
||||
$('#view').innerHTML = `
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
<a data-view="images"><i data-ico="disc"></i>本地镜像</a>
|
||||
<a data-view="registry"><i data-ico="repo"></i>镜像仓库</a>
|
||||
<a data-view="networks"><i data-ico="globe"></i>网络</a>
|
||||
<a data-view="backup"><i data-ico="disc"></i>备份与恢复</a>
|
||||
<a data-view="settings"><i data-ico="bell"></i>告警设置</a>
|
||||
</nav>
|
||||
<div class="nav-foot">
|
||||
|
||||
Reference in new issue
Block a user