feat(backup): 只读盘点真实批次、子库路径与容量
为什么:28189 验收里历史批次/子库路径/容量都没有真实接口,页面只能占位; 用户要求修实际接口而不是继续改文案,同时不得写主机或触发备份。 怎么改:新增 GET /api/v1/backup-schedule/inventory,只读盘点 statfs 实测容量、 st_blocks 实际占盘、全部批次与 MANIFEST 状态、每个子库完整主机路径。目录必须 只读挂载;用设备号区分挂载文件系统与容器根盘,同设备时不给主机容量。清单 missing/invalid、未接线、不可读分别展示。SSH 计划读回与盘点独立加载。 前端消费该接口:整盘色带、批次列表、子库路径复制、清单失败与容器根盘提示。 怎么验:Go 单测覆盖清单/占盘/符号链接/容器根盘;handler 契约;appconfig 接线校验; vitest 627 项、tsc、vite build、prettier 通过;本地页面夹具核对 8 批与路径复制, 1440/1024/768 无横向溢出。未连接真实主机、未执行备份、未改真实计划。
This commit is contained in:
@@ -9,6 +9,8 @@ import {
|
||||
saveBackupSchedule,
|
||||
} from './backupSchedule'
|
||||
import type { BackupSchedule } from './backupSchedule'
|
||||
import { fetchBackupInventory } from './backupInventory'
|
||||
import type { BackupInventory, BackupInventoryBatch } from './backupInventory'
|
||||
import './storageBackup.css'
|
||||
|
||||
export const formatBackupBytes = (bytes: number): string => {
|
||||
@@ -190,11 +192,122 @@ const percent = (part: number, total: number): string => (total > 0 ? `${(100 *
|
||||
|
||||
export const storageAlertSummary = (alerts: StorageAlert[]): string => {
|
||||
const unknown = alerts.filter((item) => !item.known)
|
||||
if (unknown.length) return '无法判定(缺主机磁盘读数)'
|
||||
if (unknown.length === alerts.length) return '无法判定(缺读数与配额字段)'
|
||||
if (unknown.length) return `部分无法判定(${unknown.map((item) => item.label).join('、')})`
|
||||
const active = alerts.filter((item) => item.active)
|
||||
return active.length ? active.map((item) => item.label).join('、') : '四项均正常'
|
||||
}
|
||||
|
||||
export interface InventoryUsage {
|
||||
total: number
|
||||
used: number
|
||||
free: number
|
||||
backup: number
|
||||
system_other: number
|
||||
}
|
||||
|
||||
// 只读挂载 + 实测容量 + 文件系统与容器根盘分离,三者同时成立才画整盘图。
|
||||
// 备份实际占盘取所有批次的 st_blocks 合计,不是 MANIFEST 的表观字节。
|
||||
export function inventoryUsage(inventory: BackupInventory | null): InventoryUsage | null {
|
||||
if (
|
||||
!inventory ||
|
||||
!inventory.wired ||
|
||||
!inventory.mount.mount_verified ||
|
||||
!inventory.capacity.measured ||
|
||||
inventory.capacity.total_bytes <= 0
|
||||
) {
|
||||
return null
|
||||
}
|
||||
const total = inventory.capacity.total_bytes
|
||||
const used = Math.max(0, inventory.capacity.used_bytes)
|
||||
const backup = Math.max(0, inventory.totals.actual_bytes)
|
||||
return {
|
||||
total,
|
||||
used,
|
||||
free: Math.max(0, inventory.capacity.available_bytes),
|
||||
backup,
|
||||
system_other: Math.max(0, used - backup),
|
||||
}
|
||||
}
|
||||
|
||||
// 配额与策略预留在后端仍是缺字段,不能因为容量读到了就假装能判定配额;
|
||||
// 磁盘告警只拿"最近一批实际占盘"做参考,并注明不含预留。
|
||||
export function inventoryAlerts(inventory: BackupInventory | null): StorageAlert[] {
|
||||
const usage = inventoryUsage(inventory)
|
||||
const latest = inventory?.batches[0]
|
||||
const capacityKnown = Boolean(usage)
|
||||
const latestBytes = latest?.actual_bytes ?? 0
|
||||
const diskKnown = capacityKnown && latestBytes > 0
|
||||
const diskActive = diskKnown && latestBytes > usage!.free
|
||||
const diskDetail = !inventory
|
||||
? '尚未读取备份目录'
|
||||
: !inventory.wired
|
||||
? '只读盘点未接线'
|
||||
: !inventory.mount.mount_verified
|
||||
? '目录位于容器根盘,读数不代表备份主机磁盘'
|
||||
: !inventory.capacity.measured
|
||||
? inventory.capacity.error || '未读到文件系统容量'
|
||||
: latestBytes <= 0
|
||||
? '没有可参考的批次实际占盘'
|
||||
: `剩余可用 ${formatGib(usage!.free)} GiB,最近一批实际占盘 ${formatGib(latestBytes)} GiB(参考值,不含策略预留)`
|
||||
return [
|
||||
{
|
||||
code: 'disk_low',
|
||||
label: '磁盘不足',
|
||||
active: diskActive,
|
||||
known: diskKnown,
|
||||
detail: diskDetail,
|
||||
},
|
||||
{
|
||||
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: '缺配额字段,无法判定',
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
export async function copyBackupPath(value: string, button?: HTMLButtonElement | null): Promise<boolean> {
|
||||
if (!value) return false
|
||||
try {
|
||||
await navigator.clipboard.writeText(value)
|
||||
if (button) button.textContent = '已复制'
|
||||
return true
|
||||
} catch {
|
||||
// 内网 http 页面没有 clipboard API 权限时退回选中,不谎报“已复制”。
|
||||
const area = document.createElement('textarea')
|
||||
area.value = value
|
||||
area.setAttribute('readonly', '')
|
||||
area.style.position = 'fixed'
|
||||
area.style.opacity = '0'
|
||||
document.body.append(area)
|
||||
area.focus()
|
||||
area.select()
|
||||
const copied = document.execCommand('copy')
|
||||
area.remove()
|
||||
if (button) button.textContent = copied ? '已复制' : '路径已选中,可复制'
|
||||
return copied
|
||||
}
|
||||
}
|
||||
|
||||
const manifestLabel = (batch: BackupInventoryBatch): string =>
|
||||
batch.manifest_state === 'ok' ? '清单完整' : batch.manifest_state === 'missing' ? '清单缺失' : '清单损坏'
|
||||
|
||||
// 备份执行主机上的整库逻辑备份(全部业务库一次 pg_dump)。
|
||||
// 页面只展示接口真实给出的读数;目录、配额、预留、子库路径、历史批次与恢复
|
||||
// 验证在后端契约里还没有字段,保留原型位置并写明"未接线"。
|
||||
@@ -218,8 +331,12 @@ export interface BackupScheduleViewProps {
|
||||
onApply: () => void
|
||||
onRunNow: () => void
|
||||
onReload: () => void
|
||||
// 仅测试夹具提供;生产容器不传,磁盘区显示"未接线"。
|
||||
// 仅测试夹具提供;生产容器不传,磁盘区改用真实盘点的容量。
|
||||
disk?: StorageDiskObservation | null
|
||||
inventory?: BackupInventory | null
|
||||
inventoryLoading?: boolean
|
||||
inventoryError?: string | null
|
||||
onReloadInventory?: () => void
|
||||
}
|
||||
|
||||
export function BackupScheduleView(props: BackupScheduleViewProps) {
|
||||
@@ -244,6 +361,10 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
|
||||
onRunNow,
|
||||
onReload,
|
||||
disk = null,
|
||||
inventory = null,
|
||||
inventoryLoading = false,
|
||||
inventoryError = null,
|
||||
onReloadInventory,
|
||||
} = props
|
||||
const days = Number(daysDraft)
|
||||
const months = Number(monthsDraft)
|
||||
@@ -283,7 +404,16 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
|
||||
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)
|
||||
const alerts = disk ? storageAlerts(disk) : inventoryAlerts(inventory)
|
||||
const latestBatch: BackupInventoryBatch | null = inventory?.batches[0] ?? null
|
||||
const inventoryFigure = inventoryUsage(inventory)
|
||||
const inventoryReady = Boolean(inventory?.wired && !inventory.error)
|
||||
const readyInventory = inventory && inventory.wired && !inventory.error ? inventory : null
|
||||
const inventoryDatabases = latestBatch?.databases.map((item) => item.name) ?? []
|
||||
const scopeDatabases = inventoryDatabases.length ? inventoryDatabases : schedule.last_run_databases
|
||||
const scopeDatabaseCount = scopeDatabases.length
|
||||
const scopeFailed = latestBatch ? latestBatch.databases.filter((item) => item.failed).length : failedCount
|
||||
const hasScopeBatch = Boolean(latestBatch) || hasLastRun
|
||||
|
||||
return (
|
||||
<section className="settings-panel wide storage-settings-panel" id="settings-storage">
|
||||
@@ -311,22 +441,33 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
|
||||
<span className={`storage-chip ${schedule.host_configured ? 'ok' : 'off'}`}>
|
||||
{schedule.host_configured ? '平台已接主机凭据' : '平台未接主机凭据'}
|
||||
</span>
|
||||
<span className={`storage-chip ${inventoryReady ? 'ok' : 'off'}`}>
|
||||
{inventoryLoading
|
||||
? '文件盘点读取中'
|
||||
: inventoryReady
|
||||
? '文件盘点已接线'
|
||||
: inventory?.error
|
||||
? '文件盘点不可读'
|
||||
: '文件盘点未接线'}
|
||||
</span>
|
||||
</div>
|
||||
<dl className="storage-facts">
|
||||
<dt>读取源</dt>
|
||||
<dd>
|
||||
{`备份执行主机 ${schedule.host_target || '(未配置)'} 本机直连 RDS`}
|
||||
{databaseCount ? ` · 最近批次含 ${databaseCount} 个数据库` : ' · 数据库清单未从主机读到'}
|
||||
{inventory?.source || `备份执行主机 ${schedule.host_target || '(未配置)'} 本机直连 RDS`}
|
||||
{scopeDatabaseCount ? ` · 最近批次含 ${scopeDatabaseCount} 个数据库` : ' · 数据库清单未读到'}
|
||||
</dd>
|
||||
<dt>写入方向</dt>
|
||||
<dd>
|
||||
{`${schedule.host_target || '(未配置)'} 本地备份根目录`}
|
||||
<span className="storage-inline-note">具体根目录未由接口返回,不在页面猜测</span>
|
||||
{inventory?.host_root || `${schedule.host_target || '(未配置)'} 本地备份根目录`}
|
||||
<span className="storage-inline-note">
|
||||
{inventoryReady ? '只读挂载盘点;平台没有写路径' : '备份目录未接线或不可读,不在页面猜测'}
|
||||
</span>
|
||||
</dd>
|
||||
<dt>执行范围</dt>
|
||||
<dd>
|
||||
{databaseCount
|
||||
? `整批 · ${databaseCount} 个数据库(${schedule.last_run_databases.join('、')})`
|
||||
{scopeDatabaseCount
|
||||
? `整批 · ${scopeDatabaseCount} 个数据库(${scopeDatabases.join('、')})`
|
||||
: '整批 · 数据库清单未读到,无法列出库名'}
|
||||
</dd>
|
||||
<dt>拨号地址</dt>
|
||||
@@ -338,8 +479,8 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
|
||||
</button>
|
||||
<p className="hint">
|
||||
<code>POST /api/v1/backup-schedule/run</code> 没有单库参数,一次就是该主机全部业务库的整批
|
||||
pg_dump。用单库按钮触发会连整批一起跑,因此本页不开放单库动作;单库恢复只能用批次里该库的子目录,
|
||||
但子目录路径当前接口没有返回。
|
||||
pg_dump。用单库按钮触发会连整批一起跑,因此本页不开放单库动作;单库恢复请复制「备份记录」里该库的
|
||||
完整子目录(来自只读盘点),不要自己拼路径。
|
||||
</p>
|
||||
</div>
|
||||
</section>
|
||||
@@ -347,19 +488,23 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
|
||||
<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
|
||||
className={`storage-chip ${hasScopeBatch && scopeFailed === 0 ? 'ok' : hasScopeBatch ? 'warn' : 'off'}`}
|
||||
>
|
||||
{hasScopeBatch ? (scopeFailed > 0 ? '最近一批有失败' : '最近一批成功') : '尚无批次读数'}
|
||||
</span>
|
||||
</div>
|
||||
<p>
|
||||
{hasLastRun
|
||||
? `最近一批 ${schedule.last_run_stamp}${runMoment(schedule) ? ` · ${runMoment(schedule)}` : ''} · ${databaseCount} 个数据库 · ${formatBackupBytes(schedule.last_run_total_bytes)} · ${schedule.last_run_duration_seconds} 秒`
|
||||
: '还没有从主机读到任何批次。'}
|
||||
{latestBatch
|
||||
? `最近一批 ${latestBatch.stamp} · ${formatBackupMoment(latestBatch.created_at)} · ${scopeDatabaseCount} 个数据库 · 表观 ${formatBackupBytes(latestBatch.apparent_bytes)} · 实际占盘 ${formatBackupBytes(latestBatch.actual_bytes)} · ${latestBatch.duration_seconds} 秒`
|
||||
: hasLastRun
|
||||
? `SSH 读回最近一次 ${schedule.last_run_stamp} · ${databaseCount} 个数据库 · ${formatBackupBytes(schedule.last_run_total_bytes)}(文件盘点未接线,以下仅 SSH 结果)`
|
||||
: '还没有从主机读到任何批次。'}
|
||||
</p>
|
||||
{schedule.running ? (
|
||||
<div className="storage-progress" role="status">
|
||||
<p className="storage-running">
|
||||
正在备份({formatBackupMoment(schedule.running_started_at)} 开始,整批 {databaseCount || '未知'}{' '}
|
||||
正在备份({formatBackupMoment(schedule.running_started_at)} 开始,整批 {scopeDatabaseCount || '未知'}{' '}
|
||||
个数据库)。 没有真实百分比接口,这里只报在途状态,完成后自动刷新。
|
||||
</p>
|
||||
<progress className="storage-progress-bar" aria-label="备份进行中,无百分比" />
|
||||
@@ -394,7 +539,17 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
|
||||
>
|
||||
<div className="storage-block-heading">
|
||||
<h3 id="storage-disk-heading">整盘占用与告警</h3>
|
||||
<span className="storage-chip off">{usage ? '已读到主机读数' : '未接线'}</span>
|
||||
<span className="storage-chip off">
|
||||
{usage
|
||||
? '已读到主机读数'
|
||||
: inventoryFigure
|
||||
? inventory?.mount.read_only
|
||||
? '只读挂载实测'
|
||||
: '实测(挂载可写)'
|
||||
: inventory?.capacity.measured
|
||||
? '容器根盘(不可作主机容量)'
|
||||
: '未接线'}
|
||||
</span>
|
||||
</div>
|
||||
{usage && disk ? (
|
||||
<>
|
||||
@@ -446,29 +601,102 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
|
||||
: ''}
|
||||
</small>
|
||||
</>
|
||||
) : inventoryFigure && inventory ? (
|
||||
<>
|
||||
<div
|
||||
className="storage-disk-track"
|
||||
role="img"
|
||||
aria-label={`挂载盘 ${formatGib(inventoryFigure.total)} GiB:系统其他占用 ${formatGib(inventoryFigure.system_other)} GiB、备份实际占盘 ${formatGib(inventoryFigure.backup)} GiB、可用 ${formatGib(inventoryFigure.free)} GiB`}
|
||||
>
|
||||
<div
|
||||
className="storage-disk-other"
|
||||
style={{ width: percent(inventoryFigure.system_other, inventoryFigure.total) }}
|
||||
/>
|
||||
<div
|
||||
className="storage-disk-used"
|
||||
style={{ width: percent(inventoryFigure.backup, inventoryFigure.total) }}
|
||||
/>
|
||||
<div
|
||||
className="storage-disk-blank"
|
||||
style={{
|
||||
width: percent(
|
||||
Math.max(0, inventoryFigure.total - inventoryFigure.used - inventoryFigure.free),
|
||||
inventoryFigure.total
|
||||
),
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<div className="storage-disk-legend">
|
||||
<span>
|
||||
<i className="storage-dot other" />
|
||||
系统其他占用 {formatGib(inventoryFigure.system_other)} GiB
|
||||
</span>
|
||||
<span>
|
||||
<i className="storage-dot used" />
|
||||
备份实际占盘 {formatGib(inventoryFigure.backup)} GiB
|
||||
</span>
|
||||
<span>
|
||||
<i className="storage-dot blank" />
|
||||
可用 {formatGib(inventoryFigure.free)} GiB
|
||||
</span>
|
||||
<span>
|
||||
<i className="storage-dot zone" />
|
||||
配额内/外 —(无配额字段)
|
||||
</span>
|
||||
</div>
|
||||
<p className="storage-metric">
|
||||
备份实际占盘 {formatGib(inventoryFigure.backup)} / 整盘 {formatGib(inventoryFigure.total)} GiB(
|
||||
{inventoryFigure.total > 0
|
||||
? `${((100 * inventoryFigure.backup) / inventoryFigure.total).toFixed(2)}%`
|
||||
: '—'}
|
||||
)· 文件系统已用 {formatGib(inventoryFigure.used)} GiB · 可用 {formatGib(inventoryFigure.free)} GiB
|
||||
</p>
|
||||
<small>
|
||||
{inventory.mount.evidence} · 挂载点 {inventory.root} · 采集于 {formatBackupMoment(inventory.observed_at)}
|
||||
{inventory.mount.read_only ? ' · 挂载为只读' : ' · 挂载不是只读(部署需改 :ro)'}
|
||||
</small>
|
||||
</>
|
||||
) : inventory?.capacity.measured ? (
|
||||
<>
|
||||
<div
|
||||
className="storage-disk-track storage-disk-track-unknown"
|
||||
role="img"
|
||||
aria-label="目录位于容器根盘,容量不代表备份主机磁盘"
|
||||
>
|
||||
<span>容器根盘 · 不代表主机磁盘</span>
|
||||
</div>
|
||||
<p className="storage-metric">
|
||||
容器根盘实测 {formatGib(inventory.capacity.total_bytes)} GiB,可用{' '}
|
||||
{formatGib(inventory.capacity.available_bytes)} GiB:仅作排障,不据此判定备份主机磁盘。
|
||||
</p>
|
||||
<small>{inventory.mount.evidence}</small>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<div
|
||||
className="storage-disk-track storage-disk-track-unknown"
|
||||
role="img"
|
||||
aria-label="整盘占用未接线:后端未提供磁盘总量、空闲量与备份占用读数"
|
||||
aria-label="整盘占用未接线:没有可读的只读挂载"
|
||||
>
|
||||
<span>未接线 · 不显示估算容量</span>
|
||||
<span>{inventoryLoading ? '正在读取只读挂载…' : '未接线 · 不显示估算容量'}</span>
|
||||
</div>
|
||||
<div className="storage-disk-legend">
|
||||
{['系统其他占用', '备份已用', '配额内空闲', '配额外空闲'].map((label, index) => (
|
||||
{['系统其他占用', '备份实际占盘', '可用', '配额内/外'].map((label, index) => (
|
||||
<span key={label}>
|
||||
<i className={`storage-dot ${['other', 'used', 'zone', 'blank'][index]}`} />
|
||||
<i className={`storage-dot ${['other', 'used', 'blank', 'zone'][index]}`} />
|
||||
{label} —
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
<p className="storage-metric">整盘总量 / 空闲 / 备份占用:未接线</p>
|
||||
<p className="storage-metric">
|
||||
整盘总量 / 空闲 / 备份实际占盘:{inventoryError || inventory?.error || '未接线'}
|
||||
</p>
|
||||
</>
|
||||
)}
|
||||
<p className="hint">
|
||||
后端目前没有「备份主机磁盘总量、空闲、备份目录占用」接口:这里保留单条整盘图的位置,
|
||||
不填示例容量、不用配额推算磁盘。以下四项告警按同一份真实读数判定。
|
||||
容量来自只读挂载目录的 statfs 实测,并与容器根盘比较设备号;同一设备时不把容器容量当主机容量。 备份占盘按文件
|
||||
st_blocks 统计(实际分配),不是 MANIFEST 的表观字节。配额与策略预留仍无后端字段,
|
||||
四项告警中只有磁盘项能按实测判定。
|
||||
</p>
|
||||
<p className="storage-alert-summary">{storageAlertSummary(alerts)}</p>
|
||||
<ul className="storage-alerts">
|
||||
@@ -511,18 +739,22 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
|
||||
备份主机上的目录
|
||||
<input
|
||||
type="text"
|
||||
value=""
|
||||
value={inventory?.host_root || ''}
|
||||
readOnly
|
||||
disabled
|
||||
placeholder="接口未提供备份目录"
|
||||
aria-label="备份主机上的目录(未接线)"
|
||||
placeholder={inventoryReady ? '' : '只读盘点未接线'}
|
||||
aria-label="备份主机上的目录(只读展示)"
|
||||
/>
|
||||
</label>
|
||||
<div className="storage-block-actions">
|
||||
<button type="button" disabled title="没有目录检查接口">
|
||||
检查目录(不可用)
|
||||
</button>
|
||||
<span className="hint">目录检查接口未接线:无法证明目录可写、挂在哪块盘。</span>
|
||||
<span className="hint">
|
||||
{inventoryReady
|
||||
? '目录来自只读挂载来源,只读展示;修改目录需部署变更,平台没有写接口。'
|
||||
: '只读盘点未接线:目录不可证明,不能按设置推定。'}
|
||||
</span>
|
||||
</div>
|
||||
<label className="storage-checkbox">
|
||||
<input
|
||||
@@ -624,57 +856,141 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
|
||||
<section className="storage-block" aria-labelledby="storage-runs-heading">
|
||||
<div className="storage-block-heading">
|
||||
<h3 id="storage-runs-heading">备份记录</h3>
|
||||
<span className="hint">接口只返回最近一批</span>
|
||||
<span className="hint">
|
||||
{readyInventory
|
||||
? `${readyInventory.totals.batch_count} 批 · 只读盘点 · 实际占盘 ${formatBackupBytes(readyInventory.totals.actual_bytes)}`
|
||||
: '只读盘点'}
|
||||
</span>
|
||||
<button type="button" onClick={onReloadInventory} disabled={inventoryLoading}>
|
||||
{inventoryLoading ? '盘点中…' : '重新盘点'}
|
||||
</button>
|
||||
</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}`
|
||||
: '还没有读到任何批次(平台未接主机凭据、主机连不上或尚未执行过);不能据此判断备份成功或失败。'}
|
||||
{inventoryLoading && !inventory ? <p className="hint">正在读取只读挂载与 MANIFEST…</p> : null}
|
||||
{inventoryError ? (
|
||||
<p className="error-message" role="alert">
|
||||
盘点请求失败:{inventoryError}。不能用 SSH 的“最近一次”代替历史;请检查 API 到只读挂载的接线。
|
||||
</p>
|
||||
)}
|
||||
) : null}
|
||||
{inventory && !inventory.wired ? (
|
||||
<p className="hint">
|
||||
只读盘点未接线:{inventory.error}。部署需把备份目录以 <code>:ro</code> 挂进 API (见
|
||||
docs/STORAGE_BACKUP_READONLY_API.md);未读到批次不等于没有备份。
|
||||
</p>
|
||||
) : null}
|
||||
{inventory && inventory.wired && inventory.error ? (
|
||||
<p className="error-message" role="alert">
|
||||
只读目录不可读:{inventory.error}
|
||||
</p>
|
||||
) : null}
|
||||
{readyInventory ? (
|
||||
<>
|
||||
<p className="hint">
|
||||
最近批次 {readyInventory.totals.latest_stamp || '—'} · 共 {readyInventory.totals.batch_count} 批 /{' '}
|
||||
{readyInventory.totals.database_count} 个子库 · 表观{' '}
|
||||
{formatBackupBytes(readyInventory.totals.apparent_bytes)} · 实际占盘{' '}
|
||||
{formatBackupBytes(readyInventory.totals.actual_bytes)}。批次与路径来自只读目录,不依赖 SSH 计划读回。
|
||||
</p>
|
||||
{readyInventory.batches.length === 0 ? (
|
||||
<p className="hint">
|
||||
挂载可读,但没有批次目录(可能尚未执行,或挂载到了空目录);这不能证明备份成功或失败。
|
||||
</p>
|
||||
) : null}
|
||||
{readyInventory.batches.map((batch, index) => {
|
||||
const failedDatabases = batch.databases.filter((item) => item.failed)
|
||||
const batchPath = batch.host_path || batch.path
|
||||
return (
|
||||
<details className="storage-batch" key={batch.stamp} open={index === 0}>
|
||||
<summary>
|
||||
<span>{batch.created_at || batch.stamp}</span>
|
||||
<span>{batch.databases.length} 个数据库 · 整批</span>
|
||||
<span className={`storage-manifest ${batch.manifest_state}`}>{manifestLabel(batch)}</span>
|
||||
<strong className={failedDatabases.length || batch.manifest_state !== 'ok' ? 'off' : 'ok'}>
|
||||
{failedDatabases.length
|
||||
? `有失败 ${failedDatabases.length} 个`
|
||||
: `实际占盘 ${formatBackupBytes(batch.actual_bytes)}`}
|
||||
</strong>
|
||||
</summary>
|
||||
<p>
|
||||
批次戳 {batch.stamp} · 表观 {formatBackupBytes(batch.apparent_bytes)} · 实际占盘{' '}
|
||||
{formatBackupBytes(batch.actual_bytes)} · {batch.file_count} 个文件
|
||||
{batch.duration_seconds ? ` · 耗时 ${batch.duration_seconds} 秒` : ''}
|
||||
{batch.kind ? ` · ${batch.kind}` : ''}
|
||||
</p>
|
||||
{batch.manifest_state === 'ok' ? null : (
|
||||
<p className="error-message">
|
||||
{batch.manifest_state === 'missing'
|
||||
? '此批次没有 MANIFEST.json:只能按目录统计占盘,无法确认完整性、耗时与库列表。'
|
||||
: `此批次清单损坏:${batch.manifest_error || '无法解析'};只能按目录统计占盘。`}
|
||||
</p>
|
||||
)}
|
||||
{!batch.sizes_complete ? (
|
||||
<p className="error-message">
|
||||
实际占盘未扫全:{batch.sizes_error || '文件数超过上限'};显示的是已扫描部分。
|
||||
</p>
|
||||
) : null}
|
||||
<div className="storage-path-row">
|
||||
<small>批次目录</small>
|
||||
<code>{batchPath || '—'}</code>
|
||||
<button
|
||||
type="button"
|
||||
disabled={!batch.host_path}
|
||||
title={batch.host_path ? '复制完整主机路径' : '没有主机路径(只读盘点未配置主机根)'}
|
||||
onClick={(event) => void copyBackupPath(batch.host_path, event.currentTarget)}
|
||||
>
|
||||
复制目录
|
||||
</button>
|
||||
</div>
|
||||
<ul className="storage-sublibraries">
|
||||
{batch.databases.map((database) => (
|
||||
<li key={database.dir_name}>
|
||||
<code>{database.name}</code>
|
||||
<span className={database.failed || !database.present ? 'off' : 'ok'}>
|
||||
{database.failed ? '失败' : database.present ? '已写入' : '目录缺失'}
|
||||
</span>
|
||||
<small>
|
||||
实际 {formatBackupBytes(database.actual_bytes)} · 表观{' '}
|
||||
{formatBackupBytes(database.apparent_bytes)} · {database.file_count} 个文件
|
||||
{database.in_manifest ? '' : ' · 不在清单库列表'}
|
||||
</small>
|
||||
<div className="storage-path-row storage-sublibrary-path">
|
||||
<code>{database.host_path || database.path || '—'}</code>
|
||||
<button
|
||||
type="button"
|
||||
disabled={!database.host_path}
|
||||
title={database.host_path ? '复制子库完整路径' : '没有主机路径'}
|
||||
onClick={(event) => void copyBackupPath(database.host_path, event.currentTarget)}
|
||||
>
|
||||
复制子库目录
|
||||
</button>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<p className="storage-verify">
|
||||
恢复验证:未接线(没有数据库备份恢复验证接口,不能把“备份成功”当成“可恢复”)。
|
||||
</p>
|
||||
</details>
|
||||
)
|
||||
})}
|
||||
{readyInventory.incomplete_count > 0 ? (
|
||||
<p className="hint">
|
||||
有 {readyInventory.incomplete_count} 个 <code>*.incomplete</code> 目录未计入批次(被中断的运行)。
|
||||
</p>
|
||||
) : null}
|
||||
{readyInventory.skipped.length > 0 ? (
|
||||
<p className="hint">
|
||||
跳过 {readyInventory.skipped.length} 项:
|
||||
{readyInventory.skipped.map((item) => `${item.name}(${item.reason})`).join(';')}
|
||||
</p>
|
||||
) : null}
|
||||
{readyInventory.truncated ? <p className="hint">目录或文件数超过扫描上限,列表与占盘为部分结果。</p> : null}
|
||||
<p className="hint">
|
||||
盘点采集于 {formatBackupMoment(readyInventory.observed_at)} · 挂载点 {readyInventory.root || '—'} · 来源{' '}
|
||||
{readyInventory.source || '—'}
|
||||
</p>
|
||||
</>
|
||||
) : null}
|
||||
{!inventory && !inventoryLoading && !inventoryError ? <p className="hint">尚未读取只读盘点。</p> : null}
|
||||
</section>
|
||||
|
||||
<section className="storage-block" aria-labelledby="storage-host-heading">
|
||||
@@ -712,14 +1028,14 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
|
||||
</dd>
|
||||
</dl>
|
||||
<dl>
|
||||
<dt>最近一次运行</dt>
|
||||
<dt>SSH 最近一次结果(历史)</dt>
|
||||
<dd>
|
||||
{schedule.last_run_stamp || '—'}
|
||||
{runMoment(schedule) ? ` · ${runMoment(schedule)}` : ''}
|
||||
{schedule.last_run_stamp ? (runFailed ? ' · 有失败/读取错误' : ' · 成功') : ''}
|
||||
</dd>
|
||||
<dt>最近一次成功</dt>
|
||||
<dd>未从主机读取(不能用最近一次运行代替成功;失败库/读取错误见上)</dd>
|
||||
<dd>未从主机读取(不能用最近一次运行代替成功;历史批次以「备份记录」的只读盘点为准)</dd>
|
||||
<dt>备份耗时 / 体积</dt>
|
||||
<dd>
|
||||
{schedule.last_run_stamp
|
||||
@@ -738,6 +1054,9 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
|
||||
</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.last_run_failed_databases.length ? (
|
||||
<p className="hint">SSH 结果的失败库:{schedule.last_run_failed_databases.join('、')}</p>
|
||||
) : null}
|
||||
{schedule.host_cron_lines.length ? (
|
||||
<p className="hint">
|
||||
主机 cron 条目:<code>{schedule.host_cron_lines.join(' ; ')}</code>
|
||||
@@ -767,6 +1086,9 @@ export function BackupScheduleSection() {
|
||||
const [timeDraft, setTimeDraft] = useState('03:30')
|
||||
const [daysDraft, setDaysDraft] = useState('7')
|
||||
const [monthsDraft, setMonthsDraft] = useState('3')
|
||||
const [inventory, setInventory] = useState<BackupInventory | null>(null)
|
||||
const [inventoryLoading, setInventoryLoading] = useState(true)
|
||||
const [inventoryError, setInventoryError] = useState<string | null>(null)
|
||||
const polling = useRef<number | null>(null)
|
||||
|
||||
const adopt = useCallback((next: BackupSchedule) => {
|
||||
@@ -797,10 +1119,40 @@ export function BackupScheduleSection() {
|
||||
}
|
||||
}, [adopt])
|
||||
|
||||
const loadInventory = useCallback(async () => {
|
||||
setInventoryLoading(true)
|
||||
setInventoryError(null)
|
||||
try {
|
||||
setInventory(await fetchBackupInventory())
|
||||
} catch (cause) {
|
||||
setInventoryError(cause instanceof Error ? cause.message : '读取只读备份盘点失败')
|
||||
} finally {
|
||||
setInventoryLoading(false)
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
void loadInventory()
|
||||
}, [loadInventory])
|
||||
|
||||
useEffect(() => {
|
||||
void load()
|
||||
}, [load])
|
||||
|
||||
// 备份完成(running 转假)后再盘点一次,新批次才会出现在历史里;
|
||||
// 运行中不反复扫目录,避免把主机 IO 当成轮询接口。
|
||||
const wasRunning = useRef(false)
|
||||
useEffect(() => {
|
||||
if (schedule?.running) {
|
||||
wasRunning.current = true
|
||||
return
|
||||
}
|
||||
if (wasRunning.current) {
|
||||
wasRunning.current = false
|
||||
void loadInventory()
|
||||
}
|
||||
}, [schedule?.running, loadInventory])
|
||||
|
||||
// 立即备份约 30~60 秒:在途时轮询主机状态,直到 running 转假并出现新的最近一次结果。
|
||||
useEffect(() => {
|
||||
if (!schedule?.running) {
|
||||
@@ -925,8 +1277,15 @@ export function BackupScheduleSection() {
|
||||
onSave={() => void save()}
|
||||
onApply={() => void apply()}
|
||||
onRunNow={() => void runNow()}
|
||||
onReload={() => void load()}
|
||||
// disk 不传:后端还没有磁盘读数接口,磁盘区显示"未接线",绝不填示例容量。
|
||||
onReload={() => {
|
||||
void load()
|
||||
void loadInventory()
|
||||
}}
|
||||
inventory={inventory}
|
||||
inventoryLoading={inventoryLoading}
|
||||
inventoryError={inventoryError}
|
||||
onReloadInventory={() => void loadInventory()}
|
||||
// disk 不传:生产只认只读挂载盘点;disk 仅测试夹具用于验证告警分支。
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user