feat(webui): expose transactional backup restore
Replace route-local restore writes with the core transaction and map preflight, rollback, and post-commit cleanup failures to precise HTTP responses. Treat only ENOENT as a missing export file and return transaction, restored, skipped, migrated, and restart facts. Update the settings panel to describe the full sensitive configuration group, avoid restart guidance for empty plans, and reload settings after both successful and failed imports so the UI reflects committed disk state. Add HTTP regression coverage for export and restore error semantics.
This commit is contained in:
7 files changed
+305
-102
No files matched your search
@@ -43,7 +43,7 @@ export function SystemPanel() {
|
||||
const [restoreCreds, setRestoreCreds] = useState(false);
|
||||
const fileRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const load = async () => {
|
||||
const load = async (): Promise<boolean> => {
|
||||
try {
|
||||
const res = await api.systemSettings.get();
|
||||
setData(res);
|
||||
@@ -51,8 +51,10 @@ export function SystemPanel() {
|
||||
setHost(res.settings.webuiHost);
|
||||
setTrustProxy(res.settings.trustProxy);
|
||||
setTlsEnabled(res.settings.tlsEnabled);
|
||||
return true;
|
||||
} catch {
|
||||
setMsg({ kind: 'err', text: '加载系统设置失败' });
|
||||
return false;
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@@ -136,11 +138,19 @@ export function SystemPanel() {
|
||||
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(',')},重启后生效`);
|
||||
const parts = res.restored.length ? [`已恢复 ${res.restored.length} 项`] : ['没有可恢复的配置'];
|
||||
if (res.skipped.length) parts.push(`跳过 ${res.skipped.length} 项(敏感配置,含 OneBot 配置)`);
|
||||
if (res.migrated.length) parts.push(`兼容转换 ${res.migrated.length} 项`);
|
||||
const summary = `${parts.join(',')}${res.restartRequiredToApply ? ',重启后生效' : ''}`;
|
||||
const refreshed = await load();
|
||||
flash(
|
||||
refreshed ? 'ok' : 'err',
|
||||
refreshed ? summary : `${summary};但界面状态刷新失败,请手动刷新页面`,
|
||||
);
|
||||
} catch (e) {
|
||||
flash('err', e instanceof Error ? e.message : '恢复失败');
|
||||
const message = e instanceof Error ? e.message : '恢复失败';
|
||||
const refreshed = await load();
|
||||
flash('err', refreshed ? message : `${message};界面状态刷新失败,请手动刷新页面`);
|
||||
} finally {
|
||||
setSaving(false);
|
||||
if (fileRef.current) fileRef.current.value = '';
|
||||
@@ -263,16 +273,16 @@ export function SystemPanel() {
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2 text-sm"><Database className="h-4 w-4" /> 配置备份 / 恢复</CardTitle>
|
||||
<CardDescription>导出/导入全部配置(OneBot、外观、通知、系统设置、证书)。不含消息数据库。</CardDescription>
|
||||
<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="导出包含登录凭据" />
|
||||
<Label>导出时包含敏感配置</Label>
|
||||
<ToggleSwitch value={exportCreds} onChange={setExportCreds} ariaLabel="导出包含敏感配置" />
|
||||
</div>
|
||||
{exportCreds && (
|
||||
<p className="text-[11px] text-red-600 dark:text-red-400">⚠ 备份文件将包含登录口令哈希与 TLS 私钥,请妥善保管。</p>
|
||||
<p className="text-[11px] text-red-600 dark:text-red-400">⚠ 将额外包含 WebUI 登录状态、TLS 私钥,以及带访问令牌的 OneBot 配置,请妥善保管。</p>
|
||||
)}
|
||||
<div>
|
||||
<Button onClick={exportBackup} disabled={saving} className="gap-1.5">
|
||||
@@ -285,11 +295,11 @@ export function SystemPanel() {
|
||||
|
||||
<div className="flex flex-col gap-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<Label>恢复时一并恢复登录凭据</Label>
|
||||
<ToggleSwitch value={restoreCreds} onChange={setRestoreCreds} ariaLabel="恢复包含登录凭据" />
|
||||
<Label>恢复时一并恢复敏感配置</Label>
|
||||
<ToggleSwitch value={restoreCreds} onChange={setRestoreCreds} ariaLabel="恢复包含敏感配置" />
|
||||
</div>
|
||||
{restoreCreds && (
|
||||
<p className="text-[11px] text-red-600 dark:text-red-400">⚠ 将覆盖当前登录口令与私钥;若备份口令未知可能登不进。</p>
|
||||
<p className="text-[11px] text-red-600 dark:text-red-400">⚠ 将覆盖当前 WebUI 登录状态、TLS 私钥与 OneBot 配置(含访问令牌);若备份口令未知可能登不进。</p>
|
||||
)}
|
||||
<input
|
||||
ref={fileRef}
|
||||
@@ -303,7 +313,7 @@ export function SystemPanel() {
|
||||
{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>
|
||||
<p className="text-[11px] text-muted-foreground">恢复前会校验本次要恢复的配置;写入过程中出错会自动回滚,成功后临时快照会删除。恢复后需重启生效。</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
Reference in new issue
Block a user