3f603aa293
需求:整库逻辑备份做定时,但要在「系统设置 → 存储与备份」里设置定时时间与储存天数。 后端 - migration 000045 backup_schedule(单行:enabled/schedule_time/retention_days/retention_months/ last_applied_*),store + 领域校验(HH:MM、1~365 天、0~24 月,非法值 400 且不落库不下发) - internal/backuphost:通过 ssh 在目标主机上读回/下发计划并立即备份(未接线时 Available=false, 页面降级提示;主机侧错误带输出回传)。最近一次只认有 MANIFEST.json 的完整目录,跳 *.incomplete - 接口 GET/PUT /api/v1/backup-schedule、GET …/host、POST …/apply、POST …/run; 写操作经 classifyRole 限管理员;run 为异步 202 + 在途单飞(http 写超时 30s,备份约 35s) - openapi.yaml 补齐 5 条路由(契约测试要求每条都写 400/500) 主机侧 - scripts/install_backup_schedule.sh(幂等):写 /etc/knowledge-governance/backup-schedule.env 与 /etc/cron.d/kb-logical-backup,参数校验,--show/--disable,最后一行 SCHEDULE_APPLIED 供平台核对 - logical-backup.sh 从该文件读保留策略;失败或被中断把目录改名 *.incomplete,不留'像成功'的半成品 - install --show 只回显计划行(原先把 SHELL=/PATH= 当计划显示) 前端 - web/src/backupSchedule.ts(严格解析:缺计划字段即报错,不把默认值伪装成设置) - BackupScheduleSection:容器 + 纯视图(管理员表单/只读、已一致/待下发、在途轮询) 运行前提(已落实):运行镜像加 openssh-client,且容器 uid 1000 在 /etc/passwd 有名字 (否则 ssh 报 No user exists for uid 1000);凭据只读挂载 secrets/backup-host(git 忽略) 实测:主机 cron 每天 03:30 / 保留 7 天 / 每月 3 份;立即备份一次 34 秒 / 742.7 MiB / 失败 0; Go 与前端测试全绿(170 vitest)。
49 lines
2.6 KiB
TypeScript
49 lines
2.6 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { fetchBackupSchedule, fetchBackupScheduleHost, runBackupNow, saveBackupSchedule } from './backupSchedule'
|
|
|
|
const payload = {
|
|
enabled: true, schedule_time: '03:30', retention_days: 7, retention_months: 3,
|
|
updated_by: 'yaozhanghao', updated_at: '2026-10-09T12:00:00Z', last_applied_at: '', last_applied_summary: '',
|
|
host_configured: true, host_target: 'weknora-test', host_reachable: true, host_error: '',
|
|
host_schedule_enabled: true, host_schedule_time: '03:30', host_keep_days: 7, host_keep_months: 3,
|
|
host_cron_lines: ['30 03 * * * root …'], host_cron_service: 'active', host_script_present: true,
|
|
last_run_stamp: '20261009T052909Z', last_run_created_at: '2026-10-09T05:29:09Z',
|
|
last_run_duration_seconds: 33, last_run_total_bytes: 778502144,
|
|
last_run_databases: ['weknora_test'], last_run_failed_databases: [], last_run_error: '',
|
|
in_sync: true, applied: true, apply_error: '', running: false, running_started_at: '', run_last_error: '',
|
|
}
|
|
|
|
const jsonResponse = (body: unknown, status = 200) => new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } })
|
|
|
|
afterEach(() => { vi.unstubAllGlobals() })
|
|
|
|
describe('backupSchedule 客户端', () => {
|
|
it('解析计划三要素与主机回执', async () => {
|
|
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ data: payload })))
|
|
const schedule = await fetchBackupSchedule()
|
|
expect(schedule.schedule_time).toBe('03:30')
|
|
expect(schedule.retention_days).toBe(7)
|
|
expect(schedule.host_cron_lines).toHaveLength(1)
|
|
expect(schedule.last_run_failed_databases).toEqual([])
|
|
})
|
|
|
|
it('缺计划字段就报错,绝不把默认值伪装成真实设置', async () => {
|
|
const broken = { ...payload } as Record<string, unknown>
|
|
delete broken.retention_days
|
|
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ data: broken })))
|
|
await expect(fetchBackupScheduleHost()).rejects.toThrow('存储与备份响应缺少计划字段')
|
|
})
|
|
|
|
it('保存失败时把后端的中文原因透出来', async () => {
|
|
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ error: '储存天数需在 1~365 之间,收到 0' }, 400)))
|
|
await expect(saveBackupSchedule({ enabled: true, schedule_time: '03:30', retention_days: 0, retention_months: 3 }))
|
|
.rejects.toThrow('储存天数需在 1~365 之间,收到 0')
|
|
})
|
|
|
|
it('立即备份返回 202 时识别为已启动', async () => {
|
|
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ data: { started: true, running: true } }, 202)))
|
|
await expect(runBackupNow()).resolves.toEqual({ started: true, running: true })
|
|
})
|
|
})
|