backup-ui.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  6. <title>MES 数据库手动备份</title>
  7. <style>
  8. :root {
  9. --bg: #0f172a; --card: #1e293b; --line: #334155; --text: #e2e8f0;
  10. --muted: #94a3b8; --accent: #38bdf8; --ok: #34d399; --warn: #fbbf24; --bad: #f87171;
  11. }
  12. * { box-sizing: border-box; }
  13. body {
  14. margin: 0;
  15. font-family: "Microsoft YaHei", "Segoe UI", sans-serif;
  16. background: radial-gradient(1200px 600px at 10% -10%, #1e3a5f 0%, var(--bg) 45%);
  17. color: var(--text); min-height: 100vh;
  18. }
  19. .wrap { max-width: 980px; margin: 0 auto; padding: 28px 20px 48px; }
  20. h1 { font-size: 22px; margin: 0 0 6px; }
  21. .sub { color: var(--muted); margin: 0 0 22px; font-size: 13px; line-height: 1.6; }
  22. .card {
  23. background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  24. padding: 18px; margin-bottom: 16px;
  25. }
  26. .row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
  27. .row h2 { margin: 0; font-size: 15px; }
  28. .btn {
  29. border: 0; border-radius: 8px; padding: 8px 14px; cursor: pointer;
  30. font-size: 13px; color: #0f172a; background: var(--accent); font-weight: 600;
  31. }
  32. .btn:disabled { opacity: .45; cursor: not-allowed; }
  33. .btn.ghost { background: #334155; color: var(--text); }
  34. .btn.go { background: #22c55e; color: #052e16; padding: 12px 22px; font-size: 15px; }
  35. .drives { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; }
  36. .drive {
  37. border: 1px solid var(--line); border-radius: 10px; padding: 12px;
  38. cursor: pointer; background: #0f172a; text-align: left;
  39. }
  40. .drive:hover { border-color: var(--accent); }
  41. .drive.on { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
  42. .drive.usb { border-color: #6366f1; }
  43. .drive .t { font-size: 12px; color: var(--muted); }
  44. .drive .n { font-size: 16px; font-weight: 700; margin: 4px 0; }
  45. .drive .s { font-size: 12px; color: var(--muted); }
  46. .tag { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 999px; background: #334155; }
  47. .tag.usb { background: #3730a3; color: #c7d2fe; }
  48. .picked {
  49. background: #0f172a; border: 1px dashed var(--line); border-radius: 10px;
  50. padding: 14px 14px; font-size: 15px; word-break: break-all; min-height: 48px;
  51. }
  52. .picked.empty { color: var(--muted); }
  53. .folders { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
  54. .folder {
  55. background: #0f172a; border: 1px solid var(--line); color: var(--text);
  56. border-radius: 8px; padding: 6px 10px; cursor: pointer; font-size: 13px;
  57. }
  58. .check { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; margin: 12px 0; }
  59. .hint { color: var(--muted); font-size: 12px; margin: 8px 0 0; }
  60. .bar { height: 10px; background: #0f172a; border-radius: 999px; overflow: hidden; margin: 10px 0 6px; }
  61. .bar > span { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #22c55e, #38bdf8); transition: width .4s; }
  62. .bar.indeterminate > span { width: 40% !important; animation: slide 1.2s infinite; }
  63. @keyframes slide { 0% { transform: translateX(-100%); } 100% { transform: translateX(280%); } }
  64. .log {
  65. background: #020617; border: 1px solid var(--line); border-radius: 10px;
  66. padding: 12px; height: 200px; overflow: auto; font-family: Consolas, monospace;
  67. font-size: 12px; white-space: pre-wrap; color: #cbd5e1;
  68. }
  69. .status { font-size: 13px; margin-left: 10px; }
  70. .ok { color: var(--ok); } .bad { color: var(--bad); } .run { color: var(--warn); }
  71. </style>
  72. </head>
  73. <body>
  74. <div class="wrap">
  75. <h1>MES 数据库手动备份</h1>
  76. <p class="sub">独立工具。点磁盘或点「浏览文件夹」选择保存位置(U 盘 / 移动硬盘 / 本机磁盘),不用手输路径。数据会边导出边压缩;库很大时请预留足够空间,完成前不要拔盘。</p>
  77. <div class="card">
  78. <div class="row">
  79. <h2>1. 点选磁盘</h2>
  80. <button class="btn ghost" id="btnRefresh" type="button">刷新磁盘</button>
  81. </div>
  82. <div class="drives" id="drives">正在读取磁盘...</div>
  83. <p class="hint" id="dbHint"></p>
  84. </div>
  85. <div class="card">
  86. <div class="row">
  87. <h2>2. 保存位置(点选,不用输入)</h2>
  88. <div>
  89. <button class="btn ghost" id="btnUp" type="button">上一级</button>
  90. <button class="btn" id="btnBrowse" type="button">浏览文件夹...</button>
  91. </div>
  92. </div>
  93. <div class="picked empty" id="picked">尚未选择。请点上方磁盘,或点「浏览文件夹」弹出系统窗口选择。</div>
  94. <div class="folders" id="folders"></div>
  95. <p class="hint" id="spaceHint"></p>
  96. <label class="check">
  97. <input id="keepOld" type="checkbox"/>
  98. 同时删除该目录中超过 7 天的旧备份(U 盘建议不勾选)
  99. </label>
  100. <div class="bar" id="bar"><span id="barInner"></span></div>
  101. <div class="hint" id="progHint"></div>
  102. <button class="btn go" id="btnBackup" type="button">开始备份</button>
  103. <span class="status" id="status"></span>
  104. </div>
  105. <div class="card">
  106. <h2>运行日志</h2>
  107. <div class="log" id="log">等待操作...</div>
  108. </div>
  109. </div>
  110. <script>
  111. var selectedPath = "";
  112. var parentPath = "";
  113. var polling = null;
  114. var dbBytes = 0;
  115. function $(id) { return document.getElementById(id); }
  116. function asArray(x) {
  117. if (!x) return [];
  118. return Object.prototype.toString.call(x) === "[object Array]" ? x : [x];
  119. }
  120. function gb(n) { return (n / 1073741824).toFixed(2); }
  121. function mb(n) { return (n / 1048576).toFixed(1); }
  122. function setStatus(text, cls) {
  123. $("status").className = "status " + (cls || "");
  124. $("status").textContent = text ? (" " + text) : "";
  125. }
  126. function setLog(lines) {
  127. $("log").textContent = (lines && lines.length) ? lines.join("\n") : "等待操作...";
  128. $("log").scrollTop = $("log").scrollHeight;
  129. }
  130. function setPicked(path) {
  131. selectedPath = path || "";
  132. var el = $("picked");
  133. if (!selectedPath) {
  134. el.className = "picked empty";
  135. el.textContent = "尚未选择。请点上方磁盘,或点「浏览文件夹」弹出系统窗口选择。";
  136. } else {
  137. el.className = "picked";
  138. el.textContent = selectedPath;
  139. }
  140. }
  141. function api(url, opt) {
  142. return fetch(url, opt).then(function (r) {
  143. return r.text().then(function (t) {
  144. var j = {};
  145. try { j = JSON.parse(t); } catch (e) { throw new Error(t || "接口无响应"); }
  146. if (!r.ok || j.ok === false) throw new Error(j.message || "请求失败");
  147. return j;
  148. });
  149. });
  150. }
  151. function loadDrives() {
  152. $("drives").textContent = "正在读取磁盘...";
  153. api("/api/drives").then(function (j) {
  154. dbBytes = j.dbBytes || 0;
  155. if (dbBytes > 0) {
  156. $("dbHint").textContent = "当前数据库约 " + gb(dbBytes) + " GB。建议目标盘剩余空间大于该值。备份采用 gzip 压缩,边导边压。";
  157. }
  158. var html = "";
  159. asArray(j.drives).forEach(function (d) {
  160. var cls = "drive" + (d.removable ? " usb" : "");
  161. var space = d.ready ? ("剩余 " + d.freeGB + " GB / 共 " + d.sizeGB + " GB") : "未就绪";
  162. html += '<button type="button" class="' + cls + '" data-path="' + d.path + '" data-free="' + (d.freeBytes || 0) + '">'
  163. + '<div class="t"><span class="tag' + (d.removable ? " usb" : "") + '">' + d.type + "</span></div>"
  164. + '<div class="n">' + d.letter + " " + (d.name || "") + "</div>"
  165. + '<div class="s">' + space + "</div></button>";
  166. });
  167. $("drives").innerHTML = html || "没有发现可用磁盘";
  168. Array.prototype.forEach.call($("drives").querySelectorAll(".drive"), function (el) {
  169. el.onclick = function () {
  170. Array.prototype.forEach.call($("drives").querySelectorAll(".drive"), function (x) { x.classList.remove("on"); });
  171. el.classList.add("on");
  172. var p = el.getAttribute("data-path");
  173. setPicked(p + "MES_DB_Backup");
  174. showSpace(parseInt(el.getAttribute("data-free") || "0", 10));
  175. loadFolders(p);
  176. };
  177. });
  178. }).catch(function (e) {
  179. $("drives").textContent = "读取磁盘失败:" + e.message;
  180. });
  181. }
  182. function showSpace(freeBytes) {
  183. if (!freeBytes) { $("spaceHint").textContent = ""; return; }
  184. var msg = "该磁盘剩余 " + gb(freeBytes) + " GB。";
  185. if (dbBytes > 0 && freeBytes < dbBytes * 1.2) {
  186. msg += " 空间可能不够,建议换更大的盘。";
  187. }
  188. $("spaceHint").textContent = msg;
  189. }
  190. function loadFolders(path) {
  191. if (!path) return;
  192. api("/api/folders?path=" + encodeURIComponent(path)).then(function (j) {
  193. var d = j.data || {};
  194. parentPath = d.parent || "";
  195. var html = "";
  196. asArray(d.folders).forEach(function (f) {
  197. html += '<button class="folder" type="button" data-path="' + f.path.replace(/"/g, "") + '">📁 ' + f.name + "</button>";
  198. });
  199. $("folders").innerHTML = html || '<span style="color:#94a3b8;font-size:13px;">该目录下没有子文件夹,将备份到上面显示的位置</span>';
  200. Array.prototype.forEach.call($("folders").querySelectorAll(".folder"), function (el) {
  201. el.onclick = function () {
  202. var p = el.getAttribute("data-path");
  203. setPicked(p);
  204. loadFolders(p);
  205. };
  206. });
  207. }).catch(function (e) {
  208. $("folders").innerHTML = '<span style="color:#f87171;font-size:13px;">' + e.message + "</span>";
  209. });
  210. }
  211. function pollStatus() {
  212. api("/api/status").then(function (j) {
  213. setLog(asArray(j.log));
  214. var written = j.writtenBytes || 0;
  215. var est = j.dbBytes || dbBytes || 0;
  216. $("bar").className = "bar" + (j.running && (!est || written < 1024) ? " indeterminate" : "");
  217. var pct = 0;
  218. if (est > 0 && written > 0) {
  219. pct = Math.max(2, Math.min(99, Math.round(written / est * 100)));
  220. } else if (!j.running && j.result === "ok") {
  221. pct = 100;
  222. }
  223. $("barInner").style.width = pct + "%";
  224. if (j.running) {
  225. $("btnBackup").disabled = true;
  226. $("progHint").textContent = "已写入 " + mb(written) + " MB" + (j.fileName ? ("(" + j.fileName + ")") : "") + ",请勿拔盘";
  227. setStatus("正在备份,数据量大时可能需要较长时间...", "run");
  228. } else {
  229. $("btnBackup").disabled = false;
  230. $("bar").className = "bar";
  231. if (j.result === "ok") {
  232. $("barInner").style.width = "100%";
  233. $("progHint").textContent = "完成,文件约 " + mb(written) + " MB:" + (j.fileName || "");
  234. setStatus("备份完成", "ok");
  235. } else if (j.result === "error") {
  236. setStatus(j.error || "备份失败", "bad");
  237. }
  238. if (polling) { clearInterval(polling); polling = null; }
  239. }
  240. }).catch(function () {});
  241. }
  242. $("btnRefresh").onclick = loadDrives;
  243. $("btnUp").onclick = function () {
  244. if (parentPath) {
  245. setPicked(parentPath);
  246. loadFolders(parentPath);
  247. }
  248. };
  249. $("btnBrowse").onclick = function () {
  250. setStatus("请在弹出的系统窗口中选择文件夹(窗口可能在浏览器后面)", "run");
  251. api("/api/browse", { method: "POST" }).then(function (j) {
  252. if (j.cancelled) { setStatus("", ""); return; }
  253. setPicked(j.path);
  254. showSpace(j.freeBytes || 0);
  255. loadFolders(j.path);
  256. setStatus("已选择保存位置", "ok");
  257. }).catch(function (e) {
  258. setStatus(e.message, "bad");
  259. });
  260. };
  261. $("btnBackup").onclick = function () {
  262. if (!selectedPath) { setStatus("请先点选磁盘或浏览文件夹", "bad"); return; }
  263. $("btnBackup").disabled = true;
  264. setStatus("正在启动备份...", "run");
  265. $("bar").className = "bar indeterminate";
  266. api("/api/backup", {
  267. method: "POST",
  268. headers: { "Content-Type": "application/json" },
  269. body: JSON.stringify({ path: selectedPath, keepDays: $("keepOld").checked ? 7 : 0 })
  270. }).then(function () {
  271. if (polling) clearInterval(polling);
  272. polling = setInterval(pollStatus, 1000);
  273. pollStatus();
  274. }).catch(function (e) {
  275. $("btnBackup").disabled = false;
  276. $("bar").className = "bar";
  277. setStatus(e.message, "bad");
  278. });
  279. };
  280. loadDrives();
  281. </script>
  282. </body>
  283. </html>