From 0c20d58a8cd2a1f1de5e10a61c746c2fe453ed4e Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Thu, 18 Jun 2026 15:01:17 +0800 Subject: [PATCH] feat(webui): backup/restore UI in system settings (Wave A2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A '配置备份 / 恢复' card in the 服务 tab: export downloads the JSON bundle (with an 包含登录凭据 toggle + red warning), restore picks a .json file and calls importBackup with a 一并恢复登录凭据 toggle (default off). Shows restored/skipped counts + restart-to-apply. Typed systemSettings.exportBackup/ importBackup client methods. View layer — verified by tsc + vite build. --- .../src/components/settings/system-panel.tsx | 94 ++++++++++++++++++- packages/webui/src/lib/api/client.ts | 6 +- packages/webui/src/lib/api/types.ts | 6 ++ packages/webui/src/types.ts | 10 ++ 4 files changed, 113 insertions(+), 3 deletions(-) diff --git a/packages/webui/src/components/settings/system-panel.tsx b/packages/webui/src/components/settings/system-panel.tsx index 9737bb02..df264164 100644 --- a/packages/webui/src/components/settings/system-panel.tsx +++ b/packages/webui/src/components/settings/system-panel.tsx @@ -4,9 +4,9 @@ // self-restart, so we never offer a "restart now" button, only a banner. // Fields currently pinned by SNOWLUMA_* env vars are flagged (edits to them // won't take effect until the env var is removed). -import { useEffect, useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import { motion } from 'motion/react'; -import { AlertTriangle, Loader2, Lock, Pencil, Save, ShieldCheck, Trash2 } from 'lucide-react'; +import { AlertTriangle, Database, Download, Loader2, Lock, Pencil, Save, ShieldCheck, Trash2, Upload } from 'lucide-react'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; @@ -37,6 +37,11 @@ export function SystemPanel() { // show a "已安装" summary + 更改 button; editing reveals the inputs again. const [editingCert, setEditingCert] = useState(false); + // backup / restore + const [exportCreds, setExportCreds] = useState(false); + const [restoreCreds, setRestoreCreds] = useState(false); + const fileRef = useRef(null); + const load = async () => { try { const res = await api.systemSettings.get(); @@ -108,6 +113,43 @@ export function SystemPanel() { } }; + const exportBackup = async () => { + setSaving(true); + try { + const bundle = await api.systemSettings.exportBackup(exportCreds); + const blob = new Blob([JSON.stringify(bundle, null, 2)], { type: 'application/json' }); + const url = URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + a.download = `snowluma-backup-${new Date().toISOString().replace(/[:.]/g, '-')}.json`; + a.click(); + URL.revokeObjectURL(url); + flash('ok', '已导出备份'); + } catch (e) { + flash('err', e instanceof Error ? e.message : '导出失败'); + } finally { + setSaving(false); + } + }; + + const importBackup = async (file: File) => { + setSaving(true); + try { + const text = await file.text(); + let parsed: Record; + try { parsed = JSON.parse(text); } catch { flash('err', '文件不是有效的 JSON'); return; } + const res = await api.systemSettings.importBackup(parsed, restoreCreds); + const parts = [`已恢复 ${res.restored.length} 项`]; + if (res.skipped.length) parts.push(`跳过 ${res.skipped.length} 项(凭据未恢复)`); + flash('ok', `${parts.join(',')},重启后生效`); + } catch (e) { + flash('err', e instanceof Error ? e.message : '恢复失败'); + } finally { + setSaving(false); + if (fileRef.current) fileRef.current.value = ''; + } + }; + if (loading) { return
加载中…
; } @@ -220,6 +262,54 @@ export function SystemPanel() { )} + + + + 配置备份 / 恢复 + 导出/导入全部配置(OneBot、外观、通知、系统设置、证书)。不含消息数据库。 + + +
+
+ + +
+ {exportCreds && ( +

⚠ 备份文件将包含登录口令哈希与 TLS 私钥,请妥善保管。

+ )} +
+ +
+
+ +
+ +
+
+ + +
+ {restoreCreds && ( +

⚠ 将覆盖当前登录口令与私钥;若备份口令未知可能登不进。

+ )} + { const f = e.target.files?.[0]; if (f) void importBackup(f); }} + /> +
+ +
+

恢复前会自动把当前配置快照到 config/.restore-backup-*;恢复后需重启生效。

+
+ + ); } diff --git a/packages/webui/src/lib/api/client.ts b/packages/webui/src/lib/api/client.ts index 17cb285a..faf7aea5 100644 --- a/packages/webui/src/lib/api/client.ts +++ b/packages/webui/src/lib/api/client.ts @@ -1,4 +1,4 @@ -import type { AccountConnections, HookProcessInfo, LogEntry, LogLevel, NotificationDeliveryRecord, NotificationsConfig, QQInfo, SystemInfo, SystemSettingsPatch, SystemSettingsResponse, UiAppearance, UiConfig, UpdateInfo } from '@/types'; +import type { AccountConnections, BackupBundle, BackupImportResult, HookProcessInfo, LogEntry, LogLevel, NotificationDeliveryRecord, NotificationsConfig, QQInfo, SystemInfo, SystemSettingsPatch, SystemSettingsResponse, UiAppearance, UiConfig, UpdateInfo } from '@/types'; import type { PasswordRule } from '@/components/pages/change-password-page'; import { normalizeOneBotConfig } from '@/lib/onebot-config'; import { @@ -112,6 +112,10 @@ class HttpApiClient implements ApiClient { deleteCert: async () => { await this.fetchJson<{ success: boolean }>('/api/system/tls/cert', { method: 'DELETE' }); }, + exportBackup: (includeCredentials: boolean) => + this.getJson(`/api/system/backup/export${includeCredentials ? '?credentials=1' : ''}`), + importBackup: (backup: BackupBundle, restoreCredentials: boolean) => + this.postJson('/api/system/backup/import', { backup, restoreCredentials }), }; this.ui = { diff --git a/packages/webui/src/lib/api/types.ts b/packages/webui/src/lib/api/types.ts index 8257a480..b8b34686 100644 --- a/packages/webui/src/lib/api/types.ts +++ b/packages/webui/src/lib/api/types.ts @@ -1,5 +1,7 @@ import type { AccountConnections, + BackupBundle, + BackupImportResult, HookProcessInfo, LogEntry, LogLevel, @@ -84,6 +86,10 @@ export interface ApiClient { /** Validate + write config/cert.pem + key.pem (restart to apply). */ uploadCert(cert: string, key: string): Promise; deleteCert(): Promise; + /** Download the config backup bundle (credentials gated). */ + exportBackup(includeCredentials: boolean): Promise; + /** Validate + restore a bundle (snapshots current config first). */ + importBackup(backup: BackupBundle, restoreCredentials: boolean): Promise; }; // ---- notifications (account up/down webhooks) ---- diff --git a/packages/webui/src/types.ts b/packages/webui/src/types.ts index 0de3ea05..c0aa34da 100644 --- a/packages/webui/src/types.ts +++ b/packages/webui/src/types.ts @@ -158,6 +158,16 @@ export interface SystemSettingsResponse { restartRequiredToApply: boolean; } +// Config backup/restore (Wave A2). The bundle is an opaque JSON object the +// server validates; the client never inspects its internals. +export type BackupBundle = Record; +export interface BackupImportResult { + restored: string[]; + skipped: string[]; + snapshotDir: string; + restartRequiredToApply: boolean; +} + export interface SystemInfo { hostname: string; platform: string;