feat(webui): backup/restore UI in system settings (Wave A2)
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.
This commit is contained in:
4 files changed
+113
-3
No files matched your search
@@ -4,9 +4,9 @@
|
|||||||
// self-restart, so we never offer a "restart now" button, only a banner.
|
// 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
|
// Fields currently pinned by SNOWLUMA_* env vars are flagged (edits to them
|
||||||
// won't take effect until the env var is removed).
|
// 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 { 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 { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
import { Badge } from '@/components/ui/badge';
|
import { Badge } from '@/components/ui/badge';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
@@ -37,6 +37,11 @@ export function SystemPanel() {
|
|||||||
// show a "已安装" summary + 更改 button; editing reveals the inputs again.
|
// show a "已安装" summary + 更改 button; editing reveals the inputs again.
|
||||||
const [editingCert, setEditingCert] = useState(false);
|
const [editingCert, setEditingCert] = useState(false);
|
||||||
|
|
||||||
|
// backup / restore
|
||||||
|
const [exportCreds, setExportCreds] = useState(false);
|
||||||
|
const [restoreCreds, setRestoreCreds] = useState(false);
|
||||||
|
const fileRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
const load = async () => {
|
const load = async () => {
|
||||||
try {
|
try {
|
||||||
const res = await api.systemSettings.get();
|
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<string, unknown>;
|
||||||
|
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) {
|
if (loading) {
|
||||||
return <div className="flex items-center gap-2 px-1 py-8 text-sm text-muted-foreground"><Loader2 className="h-4 w-4 animate-spin" /> 加载中…</div>;
|
return <div className="flex items-center gap-2 px-1 py-8 text-sm text-muted-foreground"><Loader2 className="h-4 w-4 animate-spin" /> 加载中…</div>;
|
||||||
}
|
}
|
||||||
@@ -220,6 +262,54 @@ export function SystemPanel() {
|
|||||||
)}
|
)}
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle className="flex items-center gap-2 text-sm"><Database className="h-4 w-4" /> 配置备份 / 恢复</CardTitle>
|
||||||
|
<CardDescription>导出/导入全部配置(OneBot、外观、通知、系统设置、证书)。不含消息数据库。</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="flex flex-col gap-4">
|
||||||
|
<div className="flex flex-col gap-2">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<Label>导出时包含登录凭据</Label>
|
||||||
|
<ToggleSwitch value={exportCreds} onChange={setExportCreds} ariaLabel="导出包含登录凭据" />
|
||||||
|
</div>
|
||||||
|
{exportCreds && (
|
||||||
|
<p className="text-[11px] text-red-600 dark:text-red-400">⚠ 备份文件将包含登录口令哈希与 TLS 私钥,请妥善保管。</p>
|
||||||
|
)}
|
||||||
|
<div>
|
||||||
|
<Button onClick={exportBackup} disabled={saving} className="gap-1.5">
|
||||||
|
{saving ? <Loader2 className="h-4 w-4 animate-spin" /> : <Download className="h-4 w-4" />} 导出备份
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="h-px bg-border" />
|
||||||
|
|
||||||
|
<div className="flex flex-col gap-2">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<Label>恢复时一并恢复登录凭据</Label>
|
||||||
|
<ToggleSwitch value={restoreCreds} onChange={setRestoreCreds} ariaLabel="恢复包含登录凭据" />
|
||||||
|
</div>
|
||||||
|
{restoreCreds && (
|
||||||
|
<p className="text-[11px] text-red-600 dark:text-red-400">⚠ 将覆盖当前登录口令与私钥;若备份口令未知可能登不进。</p>
|
||||||
|
)}
|
||||||
|
<input
|
||||||
|
ref={fileRef}
|
||||||
|
type="file"
|
||||||
|
accept="application/json,.json"
|
||||||
|
className="hidden"
|
||||||
|
onChange={(e) => { const f = e.target.files?.[0]; if (f) void importBackup(f); }}
|
||||||
|
/>
|
||||||
|
<div>
|
||||||
|
<Button variant="outline" onClick={() => fileRef.current?.click()} disabled={saving} className="gap-1.5">
|
||||||
|
{saving ? <Loader2 className="h-4 w-4 animate-spin" /> : <Upload className="h-4 w-4" />} 选择备份文件并恢复
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<p className="text-[11px] text-muted-foreground">恢复前会自动把当前配置快照到 config/.restore-backup-*;恢复后需重启生效。</p>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
</motion.div>
|
</motion.div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -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 type { PasswordRule } from '@/components/pages/change-password-page';
|
||||||
import { normalizeOneBotConfig } from '@/lib/onebot-config';
|
import { normalizeOneBotConfig } from '@/lib/onebot-config';
|
||||||
import {
|
import {
|
||||||
@@ -112,6 +112,10 @@ class HttpApiClient implements ApiClient {
|
|||||||
deleteCert: async () => {
|
deleteCert: async () => {
|
||||||
await this.fetchJson<{ success: boolean }>('/api/system/tls/cert', { method: 'DELETE' });
|
await this.fetchJson<{ success: boolean }>('/api/system/tls/cert', { method: 'DELETE' });
|
||||||
},
|
},
|
||||||
|
exportBackup: (includeCredentials: boolean) =>
|
||||||
|
this.getJson<BackupBundle>(`/api/system/backup/export${includeCredentials ? '?credentials=1' : ''}`),
|
||||||
|
importBackup: (backup: BackupBundle, restoreCredentials: boolean) =>
|
||||||
|
this.postJson<BackupImportResult>('/api/system/backup/import', { backup, restoreCredentials }),
|
||||||
};
|
};
|
||||||
|
|
||||||
this.ui = {
|
this.ui = {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import type {
|
import type {
|
||||||
AccountConnections,
|
AccountConnections,
|
||||||
|
BackupBundle,
|
||||||
|
BackupImportResult,
|
||||||
HookProcessInfo,
|
HookProcessInfo,
|
||||||
LogEntry,
|
LogEntry,
|
||||||
LogLevel,
|
LogLevel,
|
||||||
@@ -84,6 +86,10 @@ export interface ApiClient {
|
|||||||
/** Validate + write config/cert.pem + key.pem (restart to apply). */
|
/** Validate + write config/cert.pem + key.pem (restart to apply). */
|
||||||
uploadCert(cert: string, key: string): Promise<void>;
|
uploadCert(cert: string, key: string): Promise<void>;
|
||||||
deleteCert(): Promise<void>;
|
deleteCert(): Promise<void>;
|
||||||
|
/** Download the config backup bundle (credentials gated). */
|
||||||
|
exportBackup(includeCredentials: boolean): Promise<BackupBundle>;
|
||||||
|
/** Validate + restore a bundle (snapshots current config first). */
|
||||||
|
importBackup(backup: BackupBundle, restoreCredentials: boolean): Promise<BackupImportResult>;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---- notifications (account up/down webhooks) ----
|
// ---- notifications (account up/down webhooks) ----
|
||||||
|
|||||||
@@ -158,6 +158,16 @@ export interface SystemSettingsResponse {
|
|||||||
restartRequiredToApply: boolean;
|
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<string, unknown>;
|
||||||
|
export interface BackupImportResult {
|
||||||
|
restored: string[];
|
||||||
|
skipped: string[];
|
||||||
|
snapshotDir: string;
|
||||||
|
restartRequiredToApply: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export interface SystemInfo {
|
export interface SystemInfo {
|
||||||
hostname: string;
|
hostname: string;
|
||||||
platform: string;
|
platform: string;
|
||||||
|
|||||||
Reference in new issue
Block a user