feat(settings): 存储与备份单页对齐真实接口

为什么:原型里的单库立备、示例容量、历史批次与目录检查在本仓接口中都不存在;
把整批 run 包装成单库按钮会误触发整批,填示例读数会把演示值当产品事实。

怎么改:按真实契约重排单页——范围/读写方向、四项告警、策略、最近批次、运行进度与
恢复验证。立即备份明确标注整批并禁用单库动作;目录/配额/预留、子库路径、历史批次、
恢复验证保留原型位置并注明未接线;归档与数据库备份分开计量。整盘色带与告警抽成
纯函数,用隔离夹具覆盖正常/不足/缺读数分支。

怎么验:tsc、vitest 618 项、vite build、prettier 通过;本地页面夹具在 1440/1024/768
无横向溢出,未接线与夹具读数两种形态均按预期渲染。
This commit is contained in:
姚章浩
2026-11-19 23:31:16 +08:00
parent 6ae5150fdf
commit 6444814123
6 changed files with 1147 additions and 175 deletions
+607 -151
View File
@@ -9,6 +9,7 @@ import {
saveBackupSchedule,
} from './backupSchedule'
import type { BackupSchedule } from './backupSchedule'
import './storageBackup.css'
export const formatBackupBytes = (bytes: number): string => {
if (!bytes) return '—'
@@ -33,6 +34,170 @@ export const backupTimeValid = (value: string): boolean => /^([01][0-9]|2[0-3]):
export const backupDaysValid = (value: number): boolean => Number.isInteger(value) && value >= 1 && value <= 365
export const backupMonthsValid = (value: number): boolean => Number.isInteger(value) && value >= 0 && value <= 24
const GIB = 1024 * 1024 * 1024
// 整盘读数是"主机 + 卷 + 备份目录"三件事实的组合,当前后端没有对应接口:
// 页面只保留原型位置并写明原因,不在产品里填任何示例容量。测试夹具通过这个
// 只读结构验证色带与告警的完整分支,生产容器恒传 null。
export interface StorageDiskObservation {
total_bytes: number
free_bytes: number
// 与策略目录同一卷上的备份占用
backup_bytes: number
// 其他磁盘上历史备份的占用(合计口径也要吃总配额)
other_volume_backup_bytes: number
quota_bytes: number
reserve_bytes: number
// 下次任务预计新增;未知时必须为空,不能拿上一次体积冒充
estimate_bytes: number | null
sampled_at: string
}
export type StorageAlertCode = 'disk_low' | 'quota_low' | 'both_low' | 'quota_over_physical'
export interface StorageAlert {
code: StorageAlertCode
label: string
active: boolean
known: boolean
detail: string
}
export interface StorageUsage {
total: number
free: number
system_other: number
backup: number
quota_free: number
beyond_quota_free: number
budget: number
used_total: number
allowance: number
writable: number
}
export function storageUsage(observation: StorageDiskObservation): StorageUsage {
const total = Math.max(0, observation.total_bytes)
const free = Math.max(0, observation.free_bytes)
const backup = Math.max(0, observation.backup_bytes)
const otherBackups = Math.max(0, observation.other_volume_backup_bytes)
const reserve = Math.max(0, observation.reserve_bytes)
const budget = Math.max(0, observation.quota_bytes)
const usedTotal = backup + otherBackups
const allowance = Math.max(0, budget - usedTotal)
const writable = Math.max(0, free - reserve)
const systemOther = Math.max(0, total - free - backup)
const onOtherVolumes = Math.max(0, usedTotal - backup)
const localQuota = Math.max(0, budget - onOtherVolumes)
const inQuota = Math.min(backup, localQuota)
// 配额允许但盘上画不下的部分不进入色带,避免把配额当成物理空间。
const quotaFree = Math.min(free, allowance)
return {
total,
free,
system_other: systemOther,
backup: inQuota,
quota_free: quotaFree,
beyond_quota_free: Math.max(0, free - quotaFree),
budget,
used_total: usedTotal,
allowance,
writable,
}
}
// 四种告警与原型一致;读数缺失时每条都只能是"无法判定",不能默认正常。
export function storageAlerts(observation: StorageDiskObservation | null): StorageAlert[] {
if (!observation) {
return [
{ code: 'disk_low', label: '磁盘不足', active: false, known: false, detail: '缺主机磁盘读数' },
{ code: 'quota_low', label: '配额不足', active: false, known: false, detail: '缺备份占用量与配额读数' },
{ code: 'both_low', label: '磁盘与配额同时不足', active: false, known: false, detail: '缺主机磁盘读数' },
{ code: 'quota_over_physical', label: '配额超出物理空闲', active: false, known: false, detail: '缺主机磁盘读数' },
]
}
const usage = storageUsage(observation)
const estimate = observation.estimate_bytes
if (estimate === null) {
return [
{
code: 'disk_low',
label: '磁盘不足',
active: false,
known: false,
detail: '缺下次新增体积,不能判定是否装得下',
},
{
code: 'quota_low',
label: '配额不足',
active: false,
known: false,
detail: '缺下次新增体积,不能判定配额是否够',
},
{ code: 'both_low', label: '磁盘与配额同时不足', active: false, known: false, detail: '缺下次新增体积' },
{
code: 'quota_over_physical',
label: '配额超出物理空闲',
active: usage.allowance > usage.free,
known: true,
detail: `配额余量 ${formatGib(usage.allowance)} GiB,盘上实际空闲 ${formatGib(usage.free)} GiB`,
},
]
}
const diskLow = usage.writable < estimate
const quotaLow = usage.allowance < estimate
return [
{
code: 'disk_low',
label: '磁盘不足',
active: diskLow,
known: true,
detail: diskLow
? `扣除预留后可写 ${formatGib(usage.writable)} GiB,本次预计新增 ${formatGib(estimate)} GiB,差 ${formatGib(estimate - usage.writable)} GiB`
: `扣除预留后可写 ${formatGib(usage.writable)} GiB,够本次 ${formatGib(estimate)} GiB`,
},
{
code: 'quota_low',
label: '配额不足',
active: quotaLow,
known: true,
detail: quotaLow
? `配额余量 ${formatGib(usage.allowance)} GiB,本次预计新增 ${formatGib(estimate)} GiB,差 ${formatGib(estimate - usage.allowance)} GiB`
: `配额余量 ${formatGib(usage.allowance)} GiB,够本次 ${formatGib(estimate)} GiB`,
},
{
code: 'both_low',
label: '磁盘与配额同时不足',
active: diskLow && quotaLow,
known: true,
detail: diskLow && quotaLow ? '两项都不够,本次备份会失败' : '未同时触发',
},
{
code: 'quota_over_physical',
label: '配额超出物理空闲',
active: usage.allowance > usage.free,
known: true,
detail:
usage.allowance > usage.free
? `配额还允许 ${formatGib(usage.allowance)} GiB,但盘上只剩 ${formatGib(usage.free)} GiB;超出部分画不进磁盘`
: '配额余量未超过物理空闲',
},
]
}
const formatGib = (bytes: number): string => String(Number((bytes / GIB).toFixed(2)))
const percent = (part: number, total: number): string => (total > 0 ? `${(100 * part) / total}%` : '0%')
export const storageAlertSummary = (alerts: StorageAlert[]): string => {
const unknown = alerts.filter((item) => !item.known)
if (unknown.length) return '无法判定(缺主机磁盘读数)'
const active = alerts.filter((item) => item.active)
return active.length ? active.map((item) => item.label).join('、') : '四项均正常'
}
// 备份执行主机上的整库逻辑备份(全部业务库一次 pg_dump)。
// 页面只展示接口真实给出的读数;目录、配额、预留、子库路径、历史批次与恢复
// 验证在后端契约里还没有字段,保留原型位置并写明"未接线"。
export interface BackupScheduleViewProps {
schedule: BackupSchedule
isAdmin: boolean
@@ -53,9 +218,10 @@ export interface BackupScheduleViewProps {
onApply: () => void
onRunNow: () => void
onReload: () => void
// 仅测试夹具提供;生产容器不传,磁盘区显示"未接线"。
disk?: StorageDiskObservation | null
}
// 纯视图:所有状态由 props 驱动,便于在测试里直接渲染各种状态(管理员/只读、已一致/待下发、在途)。
export function BackupScheduleView(props: BackupScheduleViewProps) {
const {
schedule,
@@ -77,6 +243,7 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
onApply,
onRunNow,
onReload,
disk = null,
} = props
const days = Number(daysDraft)
const months = Number(monthsDraft)
@@ -97,6 +264,12 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
: schedule.host_script_present
? '存在'
: '缺失'
const databaseCount = schedule.last_run_databases.length
const failedCount = schedule.last_run_failed_databases.length
const hasLastRun = Boolean(schedule.last_run_stamp)
const runFailed = failedCount > 0 || Boolean(schedule.last_run_error)
const usage = disk ? storageUsage(disk) : null
const alerts = storageAlerts(disk)
return (
<section className="settings-panel wide storage-settings-panel" id="settings-storage">
@@ -104,8 +277,8 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
<div>
<h2>存储与备份</h2>
<p>
整库逻辑备份(pg_dump 目录格式)在<strong>备份执行主机</strong>(同 VPC 直连 RDS)
上本地执行:这里设置定时时间与储存天数,保存即下发到主机 cron,并可读回主机上生效的计划与最近一次结果。
本页管的是<strong>数据库整库逻辑备份</strong>(pg_dump 目录格式,在备份执行主机本地写盘);
下方「归档占用」是版本快照的归档文件,两者不是同一种数据,体积不能相加。
</p>
</div>
<button onClick={onReload} disabled={loading || probing}>
@@ -117,161 +290,443 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
{error}
</p>
) : null}
<div className="storage-settings-grid">
<dl>
<dt>目标主机</dt>
<dd>{hostLine}</dd>
<section className="storage-block" aria-labelledby="storage-scope-heading">
<div className="storage-block-heading">
<h3 id="storage-scope-heading">范围与读写方向</h3>
<span className={`storage-chip ${schedule.host_configured ? 'ok' : 'off'}`}>
{schedule.host_configured ? '主机已接线' : '主机未接线'}
</span>
</div>
<dl className="storage-facts">
<dt>读取源</dt>
<dd>
{`备份执行主机 ${schedule.host_target || '(未配置)'} 本机直连 RDS`}
{databaseCount ? ` · 最近批次含 ${databaseCount} 个数据库` : ' · 数据库清单未从主机读到'}
</dd>
<dt>写入方向</dt>
<dd>
{`${schedule.host_target || '(未配置)'} 本地备份根目录`}
<span className="storage-inline-note">具体根目录未由接口返回,不在页面猜测</span>
</dd>
<dt>执行范围</dt>
<dd>
{databaseCount
? `整批 · ${databaseCount} 个数据库(${schedule.last_run_databases.join('、')})`
: '整批 · 数据库清单未读到,无法列出库名'}
</dd>
<dt>拨号地址</dt>
<dd>{schedule.host_endpoint || '—'}</dd>
<dt>备份脚本</dt>
<dd>{scriptLine}</dd>
<dt>主机 cron</dt>
<dd>
{schedule.host_cron_service ? `服务 ${schedule.host_cron_service}` : '—'}
{schedule.host_checked_at
? ` · 状态读取于 ${formatBackupMoment(schedule.host_checked_at)}`
: ' · 状态读取时间未知'}
</dd>
<dt>下次计划(主机读回)</dt>
<dd>
{schedule.host_next_run
? schedule.host_next_run
: !schedule.host_configured
? '未接通备份执行主机:当前计划不会执行'
: schedule.host_schedule_enabled
? `已配置每日 ${schedule.host_schedule_time};尚未核验下一次实际执行(不以历史下发时间推算)`
: '未启用定时或未从主机读到计划'}
</dd>
<dt>最近一次运行</dt>
<dd>
{schedule.last_run_stamp || '—'}
{runMoment(schedule) ? ` · ${runMoment(schedule)}` : ''}
{schedule.last_run_stamp
? schedule.last_run_failed_databases.length === 0 && !schedule.last_run_error
? ' · 成功'
: ' · 有失败/读取错误'
: ''}
</dd>
<dt>最近一次成功</dt>
<dd>未从主机读取(不能用最近一次运行代替成功;失败库/读取错误见下方)</dd>
<dt>备份耗时 / 体积</dt>
<dd>
{schedule.last_run_stamp
? `${schedule.last_run_duration_seconds} 秒 · ${formatBackupBytes(schedule.last_run_total_bytes)}`
: '—'}
</dd>
</dl>
<dl>
<dt>覆盖的库</dt>
<dd>{schedule.last_run_databases.length ? schedule.last_run_databases.join('、') : '—'}</dd>
<dt>失败的库</dt>
<dd>
{schedule.last_run_failed_databases.length
? schedule.last_run_failed_databases.join('、')
: schedule.last_run_stamp
? '无'
: '未知(还没有读到运行记录,不能据此判断没有失败)'}
</dd>
<dt>主机上生效的计划</dt>
<dd>
{schedule.host_schedule_enabled
? `每天 ${schedule.host_schedule_time} · 保留 ${schedule.host_keep_days} 天 · 每月 ${schedule.host_keep_months} 份`
: schedule.host_configured
? '未启用定时'
<div className="storage-block-actions">
<button type="button" disabled title="接口没有单库参数,单库范围未经证实">
仅备份该库(不可用)
</button>
<p className="hint">
<code>POST /api/v1/backup-schedule/run</code> 没有单库参数,一次就是该主机全部业务库的整批
pg_dump。用单库按钮触发会连整批一起跑,因此本页不开放单库动作;单库恢复只能用批次里该库的子目录,
但子目录路径当前接口没有返回。
</p>
</div>
</section>
<section className="storage-block" aria-labelledby="storage-run-heading">
<div className="storage-block-heading">
<h3 id="storage-run-heading">立即备份</h3>
<span className={`storage-chip ${hasLastRun && !runFailed ? 'ok' : hasLastRun ? 'warn' : 'off'}`}>
{hasLastRun ? (runFailed ? '最近一批有失败' : '最近一批成功') : '尚无批次读数'}
</span>
</div>
<p>
{hasLastRun
? `最近一批 ${schedule.last_run_stamp}${runMoment(schedule) ? ` · ${runMoment(schedule)}` : ''} · ${databaseCount} 个数据库 · ${formatBackupBytes(schedule.last_run_total_bytes)} · ${schedule.last_run_duration_seconds} 秒`
: '还没有从主机读到任何批次。'}
</p>
{schedule.running ? (
<div className="storage-progress" role="status">
<p className="storage-running">
正在备份({formatBackupMoment(schedule.running_started_at)} 开始,整批 {databaseCount || '未知'}{' '}
个数据库)。 没有真实百分比接口,这里只报在途状态,完成后自动刷新。
</p>
<progress className="storage-progress-bar" aria-label="备份进行中,无百分比" />
</div>
) : null}
{schedule.run_last_error && !schedule.running ? (
<p className="error-message">上一次立即备份失败:{schedule.run_last_error}</p>
) : null}
{isAdmin ? (
<div className="storage-block-actions">
<button
className="primary"
onClick={onRunNow}
disabled={busy !== null || !schedule.host_configured || schedule.running}
>
{schedule.running
? '备份进行中…'
: busy === 'run'
? '正在触发…'
: `立即备份一次(整批${databaseCount ? ` ${databaseCount} 个数据库` : ''})`}
</button>
<span className="hint">触发的是该主机全部业务库的整批备份,不是 scope 快照,也不是单库。</span>
</div>
) : (
<p className="hint">立即备份仅管理员可触发;当前为只读。</p>
)}
</section>
<section
className={`storage-block storage-disk ${alerts.some((item) => item.known && item.active) ? 'storage-disk-low' : ''}`}
aria-labelledby="storage-disk-heading"
>
<div className="storage-block-heading">
<h3 id="storage-disk-heading">整盘占用与告警</h3>
<span className="storage-chip off">{usage ? '已读到主机读数' : '未接线'}</span>
</div>
{usage && disk ? (
<>
<div
className="storage-disk-track"
role="img"
aria-label={`整盘 ${formatGib(usage.total)} GiB:系统其他占用 ${formatGib(usage.system_other)} GiB、备份已用 ${formatGib(usage.backup)} GiB、配额内空闲 ${formatGib(usage.quota_free)} GiB、配额外空闲 ${formatGib(usage.beyond_quota_free)} GiB`}
>
<div className="storage-disk-other" style={{ width: percent(usage.system_other, usage.total) }} />
<div
className="storage-disk-zone"
style={{ width: percent(usage.backup + usage.quota_free, usage.total) }}
>
<div
className="storage-disk-used"
style={{ width: percent(usage.backup, usage.backup + usage.quota_free) }}
/>
</div>
<div className="storage-disk-blank" style={{ width: percent(usage.beyond_quota_free, usage.total) }} />
</div>
<div className="storage-disk-legend">
<span>
<i className="storage-dot other" />
系统其他占用 {formatGib(usage.system_other)} GiB
</span>
<span>
<i className="storage-dot used" />
备份已用 {formatGib(usage.backup)} GiB
</span>
<span>
<i className="storage-dot zone" />
配额内空闲 {formatGib(usage.quota_free)} GiB
</span>
<span>
<i className="storage-dot blank" />
配额外空闲 {formatGib(usage.beyond_quota_free)} GiB
</span>
</div>
<p className="storage-metric">
备份已用 {formatGib(usage.used_total)} / {formatGib(usage.budget)} GiB(
{usage.budget > 0 ? `${((100 * usage.used_total) / usage.budget).toFixed(2)}%` : '—'})· 总配额占当前整盘{' '}
{usage.total > 0 ? `${((100 * usage.budget) / usage.total).toFixed(2)}%` : '—'}
</p>
<small>
读数采集于 {formatBackupMoment(disk.sampled_at)} · 本盘实际空闲 {formatGib(usage.free)} GiB,系统预留{' '}
{formatGib(disk.reserve_bytes)} GiB
{disk.other_volume_backup_bytes > 0
? ` · 其他磁盘的历史备份占用总配额 ${formatGib(disk.other_volume_backup_bytes)} GiB`
: ''}
</small>
</>
) : (
<>
<div
className="storage-disk-track storage-disk-track-unknown"
role="img"
aria-label="整盘占用未接线:后端未提供磁盘总量、空闲量与备份占用读数"
>
<span>未接线 · 不显示估算容量</span>
</div>
<div className="storage-disk-legend">
{['系统其他占用', '备份已用', '配额内空闲', '配额外空闲'].map((label, index) => (
<span key={label}>
<i className={`storage-dot ${['other', 'used', 'zone', 'blank'][index]}`} />
{label} —
</span>
))}
</div>
<p className="storage-metric">整盘总量 / 空闲 / 备份占用:未接线</p>
</>
)}
<p className="hint">
后端目前没有「备份主机磁盘总量、空闲、备份目录占用」接口:这里保留单条整盘图的位置,
不填示例容量、不用配额推算磁盘。以下四项告警按同一份真实读数判定。
</p>
<p className="storage-alert-summary">{storageAlertSummary(alerts)}</p>
<ul className="storage-alerts">
{alerts.map((alert) => (
<li key={alert.code} className={alert.known ? (alert.active ? 'alert-on' : 'alert-ok') : 'alert-unknown'}>
<strong>{alert.label}</strong>
<span>{alert.known ? (alert.active ? '触发' : '正常') : '无法判定'}</span>
<small>{alert.detail}</small>
</li>
))}
</ul>
</section>
<section className="storage-block" aria-labelledby="storage-policy-heading">
<div className="storage-block-heading">
<h3 id="storage-policy-heading">备份策略</h3>
<span className="hint">北京时间 · 主机读回</span>
</div>
<p className="storage-plan">
{!schedule.host_configured
? '主机未接线:定时计划不会执行。'
: !schedule.host_reachable
? '主机连不上:以下"主机生效"状态未知,不能按设置推定。'
: schedule.host_schedule_enabled
? `主机生效:每天 ${schedule.host_schedule_time} · 保留 ${schedule.host_keep_days} 天 · 每月 ${schedule.host_keep_months} 份 · ${
schedule.in_sync ? '与设置已一致' : '与设置不一致(待下发)'
}`
: '主机生效:定时备份已关闭(已有备份保留)'}
{schedule.host_next_run ? ` · 下次 ${schedule.host_next_run}` : ' · 下一次实际执行未由主机返回'}
</p>
<p className="storage-plan">
编辑草稿:{enabledDraft ? `每天 ${timeDraft || '—'} · 保留 ${daysDraft || '—'} 天` : '定时备份关闭'} ·
每月归档 {monthsDraft || '—'} 份{dirty ? ' · 有未保存修改,主机仍按生效计划执行' : ' · 与已保存设置一致'}
</p>
{isAdmin ? (
<div className="storage-policy-editor">
<label>
备份主机上的目录
<input
type="text"
value=""
readOnly
disabled
placeholder="接口未提供备份目录"
aria-label="备份主机上的目录(未接线)"
/>
</label>
<div className="storage-block-actions">
<button type="button" disabled title="没有目录检查接口">
检查目录(不可用)
</button>
<span className="hint">目录检查接口未接线:无法证明目录可写、挂在哪块盘。</span>
</div>
<label className="storage-checkbox">
<input
type="checkbox"
checked={enabledDraft}
onChange={(event) => onEnabledChange(event.target.checked)}
/>
启用定时备份
</label>
<div className="storage-policy-grid">
<label>
每日运行时间
<input
type="time"
value={timeDraft}
onChange={(event) => onTimeChange(event.target.value)}
aria-label="定时时间"
/>
</label>
<label>
备份总配额(GiB)
<input
type="number"
min={1}
value=""
disabled
readOnly
placeholder="接口未提供配额"
aria-label="备份总配额(未接线)"
/>
</label>
<label>
磁盘预留(GiB)
<input
type="number"
min={0}
value=""
disabled
readOnly
placeholder="接口未提供预留"
aria-label="磁盘预留(未接线)"
/>
</label>
<label>
储存天数
<input
type="number"
min={1}
max={365}
value={daysDraft}
onChange={(event) => onDaysChange(event.target.value)}
aria-label="储存天数"
/>
</label>
<label>
每月归档保留
<input
type="number"
min={0}
max={24}
value={monthsDraft}
onChange={(event) => onMonthsChange(event.target.value)}
aria-label="每月归档保留"
/>
</label>
</div>
<p className="hint">
保存只提交后端契约里的四项:启用、每日时间、储存天数、每月归档份数。目录、总配额、磁盘预留没有对应字段,
这里只占原型位置,不会被保存,也不会显示假的保存成功。
</p>
{!valid ? (
<p className="error-message">定时时间需为 HH:MM(00:00~23:59),储存天数 1~365,每月归档 0~24。</p>
) : null}
<div className="storage-block-actions">
<button onClick={onSave} disabled={busy !== null || !valid || !dirty}>
{busy === 'save' ? '正在保存并下发…' : '保存并下发到主机'}
</button>
<button onClick={onApply} disabled={busy !== null || !schedule.host_configured}>
{busy === 'apply' ? '正在下发…' : '重新下发当前设置'}
</button>
{message ? (
<span className="success-message" role="status">
{message}
</span>
) : null}
</div>
<p className="hint">
保存后从下次任务起使用新计划;历史文件保留在原路径。设置已保存但下发失败时,页面如实显示失败原因。
</p>
</div>
) : (
<p className="hint">
定时备份设置仅管理员可修改;当前设置:
{schedule.enabled ? `每天 ${schedule.schedule_time} · 保留 ${schedule.retention_days} 天` : '未启用'}。
</p>
)}
</section>
<section className="storage-block" aria-labelledby="storage-runs-heading">
<div className="storage-block-heading">
<h3 id="storage-runs-heading">备份记录</h3>
<span className="hint">接口只返回最近一批</span>
</div>
<p className="hint">
历史批次列表、每批的数据库明细与子库目录都没有接口:这里只能展示主机 MANIFEST 里的最近一批,
不补造历史记录,也不从时间戳推算目录。
</p>
{hasLastRun ? (
<details className="storage-batch" open>
<summary>
<span>{schedule.last_run_created_at || schedule.last_run_stamp}</span>
<span>{databaseCount} 个数据库 · 整批</span>
<strong className={runFailed ? 'off' : 'ok'}>
{runFailed ? '有失败/读取错误' : '成功 · 共 ' + formatBackupBytes(schedule.last_run_total_bytes)}
</strong>
</summary>
<p>
批次戳 {schedule.last_run_stamp} · 耗时 {schedule.last_run_duration_seconds} 秒 · 总占盘{' '}
{formatBackupBytes(schedule.last_run_total_bytes)} · 单库体积未由清单提供
</p>
<ul className="storage-sublibraries">
{schedule.last_run_databases.map((name) => {
const failed = schedule.last_run_failed_databases.includes(name)
return (
<li key={name}>
<code>{name}</code>
<span className={failed ? 'off' : 'ok'}>{failed ? '失败' : '已写入'}</span>
<small>子库目录与单库字节未由接口返回</small>
</li>
)
})}
</ul>
{schedule.last_run_failed_databases.length ? (
<p className="error-message">失败库:{schedule.last_run_failed_databases.join('、')}</p>
) : null}
<div className="storage-block-actions">
<button type="button" disabled title="接口未返回子库路径,无可复制内容">
复制目录(不可用)
</button>
<span className="hint">子库路径待接口返回后才能复制;当前不拼接猜测路径。</span>
</div>
<p className="storage-verify">
恢复验证:未接线(没有数据库备份恢复验证接口,不能把"备份成功"当成"可恢复")。
</p>
</details>
) : (
<p className="hint">
{schedule.last_run_error
? `最近一次结果读取失败:${schedule.last_run_error}`
: '还没有读到任何批次(主机未接线、连不上或尚未执行过);不能据此判断备份成功或失败。'}
</p>
)}
</section>
<section className="storage-block" aria-labelledby="storage-host-heading">
<div className="storage-block-heading">
<h3 id="storage-host-heading">主机状态(读回)</h3>
<span className="hint">
{schedule.host_checked_at ? `读取于 ${formatBackupMoment(schedule.host_checked_at)}` : '尚未探测'}
</span>
</div>
<div className="storage-settings-grid">
<dl>
<dt>目标主机</dt>
<dd>{hostLine}</dd>
<dt>拨号地址</dt>
<dd>{schedule.host_endpoint || '—'}</dd>
<dt>备份脚本</dt>
<dd>{scriptLine}</dd>
<dt>主机 cron</dt>
<dd>{schedule.host_cron_service ? `服务 ${schedule.host_cron_service}` : '—'}</dd>
<dt>下次计划(主机读回)</dt>
<dd>
{schedule.host_next_run
? schedule.host_next_run
: !schedule.host_configured
? '未接通备份执行主机:当前计划不会执行'
: schedule.host_schedule_enabled
? `已配置每日 ${schedule.host_schedule_time};尚未核验下一次实际执行(不以历史下发时间推算)`
: '未启用定时或未从主机读到计划'}
</dd>
</dl>
<dl>
<dt>最近一次运行</dt>
<dd>
{schedule.last_run_stamp || '—'}
{runMoment(schedule) ? ` · ${runMoment(schedule)}` : ''}
{schedule.last_run_stamp ? (runFailed ? ' · 有失败/读取错误' : ' · 成功') : ''}
</dd>
<dt>最近一次成功</dt>
<dd>未从主机读取(不能用最近一次运行代替成功;失败库/读取错误见上)</dd>
<dt>备份耗时 / 体积</dt>
<dd>
{schedule.last_run_stamp
? `${schedule.last_run_duration_seconds} 秒 · ${formatBackupBytes(schedule.last_run_total_bytes)}`
: '—'}
</dd>
<dt>与设置是否一致</dt>
<dd>{schedule.in_sync ? '已一致' : '待下发'}</dd>
</dl>
</div>
{schedule.host_error ? <p className="hint">主机探测:{schedule.host_error}</p> : null}
{schedule.last_run_error ? <p className="hint">最近一次结果读取:{schedule.last_run_error}</p> : null}
{schedule.host_cron_lines.length ? (
<p className="hint">
主机 cron 条目:<code>{schedule.host_cron_lines.join(' ; ')}</code>
</p>
) : null}
{schedule.last_applied_at ? (
<p className="hint">
最后下发:{formatBackupMoment(schedule.last_applied_at)}
{schedule.last_applied_summary ? ` · ${schedule.last_applied_summary}` : ''}
</p>
) : null}
</dd>
<dt>与设置是否一致</dt>
<dd>{schedule.in_sync ? '已一致' : '待下发'}</dd>
<dt>最后下发</dt>
<dd>
{schedule.last_applied_at
? `${formatBackupMoment(schedule.last_applied_at)}${schedule.last_applied_summary ? ` · ${schedule.last_applied_summary}` : ''}`
: '—'}
</dd>
</dl>
</div>
{schedule.host_error ? <p className="hint">主机探测:{schedule.host_error}</p> : null}
{schedule.last_run_error ? <p className="hint">最近一次结果读取:{schedule.last_run_error}</p> : null}
{schedule.host_cron_lines.length ? (
<p className="hint">
主机 cron 条目:<code>{schedule.host_cron_lines.join(' ; ')}</code>
</p>
) : null}
</section>
<p className="hint">
留档分层:PG 行级留档(发布前,含 ES 文档)+ ES 快照仓 + OSS 版本控制 + 本节的整库逻辑备份 + RDS
托管备份(控制台配置)。详见 <code>docs/PRODUCTION_BACKUP_RECOVERY.md</code>。
</p>
{isAdmin ? (
<div className="kb-binding-editor">
<label>
定时时间{' '}
<input
type="time"
value={timeDraft}
onChange={(event) => onTimeChange(event.target.value)}
aria-label="定时时间"
/>
</label>
<label>
储存天数{' '}
<input
type="number"
min={1}
max={365}
value={daysDraft}
onChange={(event) => onDaysChange(event.target.value)}
aria-label="储存天数"
/>
</label>
<label>
每月归档保留{' '}
<input
type="number"
min={0}
max={24}
value={monthsDraft}
onChange={(event) => onMonthsChange(event.target.value)}
aria-label="每月归档保留"
/>
</label>
<label className="checkbox-label">
<input type="checkbox" checked={enabledDraft} onChange={(event) => onEnabledChange(event.target.checked)} />{' '}
启用定时整库备份
</label>
{!valid ? (
<p className="error-message">定时时间需为 HH:MM(00:00~23:59),储存天数 1~365,每月归档 0~24。</p>
) : null}
<button onClick={onSave} disabled={busy !== null || !valid || !dirty}>
{busy === 'save' ? '正在保存并下发…' : '保存并下发到主机'}
</button>
<button onClick={onApply} disabled={busy !== null || !schedule.host_configured}>
{busy === 'apply' ? '正在下发…' : '重新下发当前设置'}
</button>
<button onClick={onRunNow} disabled={busy !== null || !schedule.host_configured || schedule.running}>
{schedule.running ? '备份进行中…' : '立即备份一次(全库,不是 scope 快照)'}
</button>
<p className="hint">
「立即备份一次」执行的是主机上的整库逻辑备份(该主机上全部业务库的 pg_dump 目录),
<strong>不是</strong>本次对内/对外 scope 的整库快照;scope 快照在「发布管理 → 整库版本」里封存。
</p>
{message ? (
<p className="success-message" role="status">
{message}
</p>
) : null}
{schedule.running ? (
<p className="hint">正在备份({formatBackupMoment(schedule.running_started_at)} 开始),完成后自动刷新。</p>
) : null}
{schedule.run_last_error && !schedule.running ? (
<p className="error-message">上一次立即备份失败:{schedule.run_last_error}</p>
) : null}
</div>
) : (
<p className="hint">
定时备份设置仅管理员可修改;当前设置:
{schedule.enabled ? `每天 ${schedule.schedule_time} · 保留 ${schedule.retention_days} 天` : '未启用'}。
</p>
)}
</section>
)
}
@@ -399,7 +854,7 @@ export function BackupScheduleSection() {
const start = await runBackupNow()
setMessage(
start.started
? '已在目标主机上开始备份(整库 pg_dump,约 30~60 秒),下方会自动刷新结果。'
? '已在目标主机上开始整批备份(全部业务库 pg_dump,约 30~60 秒),下方会自动刷新结果。'
: '已有一个备份任务在执行中。'
)
adopt(await fetchBackupScheduleHost())
@@ -449,6 +904,7 @@ export function BackupScheduleSection() {
onApply={() => void apply()}
onRunNow={() => void runNow()}
onReload={() => void load()}
// disk 不传:后端还没有磁盘读数接口,磁盘区显示"未接线",绝不填示例容量。
/>
)
}