backup-ui.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. .form {
  70. display: grid;
  71. grid-template-columns: 72px 1fr 72px 1fr;
  72. gap: 10px 12px;
  73. align-items: center;
  74. }
  75. .form label { color: var(--muted); font-size: 13px; }
  76. .form input, .form select {
  77. background: #0f172a; border: 1px solid var(--line); color: var(--text);
  78. border-radius: 8px; padding: 8px 10px; font-size: 14px; width: 100%;
  79. }
  80. .form .readonly {
  81. background: #0b1220; color: var(--muted);
  82. }
  83. .cfg-msg { display: block; margin-top: 10px; min-height: 22px; font-size: 14px; font-weight: 600; }
  84. .ok { color: var(--ok); } .bad { color: var(--bad); } .run { color: var(--warn); }
  85. </style>
  86. </head>
  87. <body>
  88. <div class="wrap">
  89. <h1>MES 数据库手动备份 · 选文件夹</h1>
  90. <p class="sub">独立工具,仅支持 MySQL。填写连接后,点「浏览文件夹」选择 U 盘或硬盘目录(不用手输、打开时不扫描磁盘)。数据会边导出边压缩,完成前不要拔盘。</p>
  91. <div class="card">
  92. <div class="row">
  93. <h2>1. MySQL 连接(可配置)</h2>
  94. </div>
  95. <div class="form">
  96. <label>类型</label>
  97. <input class="readonly" value="MySQL" readonly />
  98. <label>端口</label>
  99. <input id="dbPort" type="number" min="1" max="65535" value="3306" />
  100. <label>IP/主机</label>
  101. <input id="dbHost" type="text" placeholder="127.0.0.1 或 192.168.x.x" />
  102. <label>库名</label>
  103. <input id="dbName" type="text" placeholder="mes_cloud" />
  104. <label>账号</label>
  105. <input id="dbUser" type="text" autocomplete="off" />
  106. <label>密码</label>
  107. <input id="dbPass" type="password" autocomplete="new-password" />
  108. </div>
  109. <div class="form-actions">
  110. <button class="btn ghost" id="btnTest" type="button">测试连接</button>
  111. <button class="btn" id="btnSaveCfg" type="button">保存配置</button>
  112. </div>
  113. <div class="cfg-msg" id="cfgStatus">填写 IP、账号、密码后点「测试连接」</div>
  114. </div>
  115. <div class="card">
  116. <div class="row">
  117. <h2>2. 选择保存位置</h2>
  118. <button class="btn" id="btnBrowse" type="button">浏览文件夹...</button>
  119. </div>
  120. <p class="hint">打开页面不会扫描磁盘。插入 U 盘后点「浏览文件夹」,在系统窗口里选目录即可。</p>
  121. <div class="picked empty" id="picked">尚未选择。请点「浏览文件夹」。</div>
  122. <p class="hint" id="spaceHint"></p>
  123. <label class="check">
  124. <input id="keepOld" type="checkbox"/>
  125. 同时删除该目录中超过 7 天的旧备份(U 盘建议不勾选)
  126. </label>
  127. <div class="bar" id="bar"><span id="barInner"></span></div>
  128. <div class="hint" id="progHint"></div>
  129. <button class="btn go" id="btnBackup" type="button">开始备份</button>
  130. <span class="status" id="status"></span>
  131. </div>
  132. <div class="card">
  133. <h2>运行日志</h2>
  134. <div class="log" id="log">等待操作...</div>
  135. </div>
  136. </div>
  137. <script>
  138. var selectedPath = "";
  139. var parentPath = "";
  140. var polling = null;
  141. var dbBytes = 0;
  142. function $(id) {
  143. return document.getElementById(id);
  144. }
  145. function dbBody() {
  146. return {
  147. engine: "mysql",
  148. host: ($("dbHost").value || "").trim(),
  149. port: parseInt($("dbPort").value || "3306", 10),
  150. database: ($("dbName").value || "").trim(),
  151. user: ($("dbUser").value || "").trim(),
  152. password: $("dbPass").value || ""
  153. };
  154. }
  155. function setCfgStatus(text, cls) {
  156. $("cfgStatus").className = "cfg-msg " + (cls || "");
  157. $("cfgStatus").textContent = text || "";
  158. }
  159. function loadConfig() {
  160. api("/api/config").then(function (j) {
  161. $("dbHost").value = j.host || "";
  162. $("dbPort").value = j.port || 3306;
  163. $("dbName").value = j.database || "";
  164. $("dbUser").value = j.user || "";
  165. $("dbPass").value = j.password || "";
  166. }).catch(function (e) {
  167. setCfgStatus(e.message, "bad");
  168. });
  169. }
  170. function asArray(x) {
  171. if (!x) return [];
  172. return Object.prototype.toString.call(x) === "[object Array]" ? x : [x];
  173. }
  174. function gb(n) { return (n / 1073741824).toFixed(2); }
  175. function mb(n) { return (n / 1048576).toFixed(1); }
  176. function setStatus(text, cls) {
  177. $("status").className = "status " + (cls || "");
  178. $("status").textContent = text ? (" " + text) : "";
  179. }
  180. function setLog(lines) {
  181. $("log").textContent = (lines && lines.length) ? lines.join("\n") : "等待操作...";
  182. $("log").scrollTop = $("log").scrollHeight;
  183. }
  184. function setPicked(path) {
  185. selectedPath = path || "";
  186. var el = $("picked");
  187. if (!selectedPath) {
  188. el.className = "picked empty";
  189. el.textContent = "尚未选择。请点「浏览文件夹」。";
  190. } else {
  191. el.className = "picked";
  192. el.textContent = selectedPath;
  193. }
  194. }
  195. function api(url, opt) {
  196. opt = opt || {};
  197. var ctrl = new AbortController();
  198. var ms = opt.timeoutMs || 12000;
  199. var timer = setTimeout(function () { ctrl.abort(); }, ms);
  200. var fo = Object.assign({}, opt, { signal: ctrl.signal });
  201. delete fo.timeoutMs;
  202. return fetch(url, fo).then(function (r) {
  203. return r.text().then(function (t) {
  204. var j = {};
  205. try { j = t ? JSON.parse(t) : {}; } catch (e) { throw new Error(t || "接口无响应"); }
  206. if (!r.ok || j.ok === false) throw new Error(j.message || "请求失败");
  207. return j;
  208. });
  209. }).catch(function (e) {
  210. if (e.name === "AbortError") throw new Error("等待超时,请确认已关掉旧的备份窗口后重新打开页面");
  211. throw e;
  212. }).then(function (v) {
  213. clearTimeout(timer);
  214. return v;
  215. }, function (e) {
  216. clearTimeout(timer);
  217. throw e;
  218. });
  219. }
  220. function showSpace(freeBytes) {
  221. if (!freeBytes) { $("spaceHint").textContent = ""; return; }
  222. var msg = "该磁盘剩余 " + gb(freeBytes) + " GB。";
  223. if (dbBytes > 0 && freeBytes < dbBytes * 1.2) {
  224. msg += " 空间可能不够,建议换更大的盘。";
  225. }
  226. $("spaceHint").textContent = msg;
  227. }
  228. function pollStatus() {
  229. api("/api/status").then(function (j) {
  230. setLog(asArray(j.log));
  231. var written = j.writtenBytes || 0;
  232. var est = j.dbBytes || dbBytes || 0;
  233. $("bar").className = "bar" + (j.running && (!est || written < 1024) ? " indeterminate" : "");
  234. var pct = 0;
  235. if (est > 0 && written > 0) {
  236. pct = Math.max(2, Math.min(99, Math.round(written / est * 100)));
  237. } else if (!j.running && j.result === "ok") {
  238. pct = 100;
  239. }
  240. $("barInner").style.width = pct + "%";
  241. if (j.running) {
  242. $("btnBackup").disabled = true;
  243. $("progHint").textContent = "已写入 " + mb(written) + " MB" + (j.fileName ? ("(" + j.fileName + ")") : "") + ",请勿拔盘";
  244. setStatus("正在备份,数据量大时可能需要较长时间...", "run");
  245. } else {
  246. $("btnBackup").disabled = false;
  247. $("bar").className = "bar";
  248. if (j.result === "ok") {
  249. $("barInner").style.width = "100%";
  250. $("progHint").textContent = "完成,文件约 " + mb(written) + " MB:" + (j.fileName || "");
  251. setStatus("备份完成", "ok");
  252. } else if (j.result === "error") {
  253. setStatus(j.error || "备份失败", "bad");
  254. }
  255. if (polling) { clearInterval(polling); polling = null; }
  256. }
  257. }).catch(function () {});
  258. }
  259. $("btnBrowse").onclick = function () {
  260. setStatus("请在弹出的系统窗口中选择文件夹(窗口可能在浏览器后面)", "run");
  261. api("/api/browse", { method: "POST" }).then(function (j) {
  262. if (j.cancelled) { setStatus("", ""); return; }
  263. setPicked(j.path);
  264. showSpace(j.freeBytes || 0);
  265. setStatus("已选择保存位置", "ok");
  266. }).catch(function (e) {
  267. setStatus(e.message, "bad");
  268. });
  269. };
  270. $("btnBackup").onclick = function () {
  271. if (!selectedPath) { setStatus("请先点「浏览文件夹」选择保存位置", "bad"); return; }
  272. $("btnBackup").disabled = true;
  273. setStatus("正在启动备份...", "run");
  274. $("bar").className = "bar indeterminate";
  275. api("/api/backup", {
  276. method: "POST",
  277. headers: { "Content-Type": "application/json" },
  278. body: JSON.stringify(Object.assign(dbBody(), {
  279. path: selectedPath,
  280. keepDays: $("keepOld").checked ? 7 : 0
  281. }))
  282. }).then(function () {
  283. if (polling) clearInterval(polling);
  284. polling = setInterval(pollStatus, 1000);
  285. pollStatus();
  286. }).catch(function (e) {
  287. $("btnBackup").disabled = false;
  288. $("bar").className = "bar";
  289. setStatus(e.message, "bad");
  290. });
  291. };
  292. $("btnTest").onclick = function () {
  293. setCfgStatus("正在测试连接,请稍候…", "run");
  294. var b = dbBody();
  295. var q = "host=" + encodeURIComponent(b.host)
  296. + "&port=" + encodeURIComponent(b.port)
  297. + "&database=" + encodeURIComponent(b.database)
  298. + "&user=" + encodeURIComponent(b.user)
  299. + "&password=" + encodeURIComponent(b.password);
  300. api("/api/test?" + q, { timeoutMs: 15000 }).then(function (j) {
  301. setCfgStatus(j.message || "连接成功", "ok");
  302. }).catch(function (e) {
  303. setCfgStatus(e.message || "连接失败", "bad");
  304. });
  305. };
  306. $("btnSaveCfg").onclick = function () {
  307. setCfgStatus("正在保存...", "run");
  308. api("/api/config", {
  309. method: "POST",
  310. headers: { "Content-Type": "application/json" },
  311. body: JSON.stringify(dbBody())
  312. }).then(function (j) {
  313. setCfgStatus(j.message || "已保存", "ok");
  314. }).catch(function (e) {
  315. setCfgStatus(e.message, "bad");
  316. });
  317. };
  318. loadConfig();
  319. </script>
  320. </body>
  321. </html>