feat(backup): 系统设置里的存储与备份——定时时间与储存天数可配、下发到目标主机 cron

需求:整库逻辑备份做定时,但要在「系统设置 → 存储与备份」里设置定时时间与储存天数。

后端
- 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)。
This commit is contained in:
姚章浩
2026-10-09 13:50:23 +08:00
parent 4455d02262
commit 3f603aa293
25 changed files with 2353 additions and 11 deletions
+123
View File
@@ -44,6 +44,129 @@
}
],
"paths": {
"/api/v1/backup-schedule": {
"get": {
"tags": [
"Settings"
],
"operationId": "getBackupSchedule",
"summary": "读取整库逻辑备份的定时计划(不探测主机)",
"responses": {
"200": {
"$ref": "#/components/responses/Envelope"
},
"500": {
"$ref": "#/components/responses/InternalError"
},
"503": {
"$ref": "#/components/responses/ServiceUnavailable"
},
"400": {
"$ref": "#/components/responses/BadRequest"
}
}
},
"put": {
"tags": [
"Settings"
],
"operationId": "saveBackupSchedule",
"summary": "保存整库逻辑备份的定时时间与储存天数,并下发到目标主机(管理员)",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
},
"responses": {
"200": {
"$ref": "#/components/responses/Envelope"
},
"400": {
"$ref": "#/components/responses/BadRequest"
},
"500": {
"$ref": "#/components/responses/InternalError"
},
"503": {
"$ref": "#/components/responses/ServiceUnavailable"
}
}
}
},
"/api/v1/backup-schedule/host": {
"get": {
"tags": [
"Settings"
],
"operationId": "getBackupScheduleHost",
"summary": "读取目标主机上生效的 cron 计划与最近一次备份结果",
"responses": {
"200": {
"$ref": "#/components/responses/Envelope"
},
"500": {
"$ref": "#/components/responses/InternalError"
},
"503": {
"$ref": "#/components/responses/ServiceUnavailable"
},
"400": {
"$ref": "#/components/responses/BadRequest"
}
}
}
},
"/api/v1/backup-schedule/apply": {
"post": {
"tags": [
"Settings"
],
"operationId": "applyBackupSchedule",
"summary": "把当前设置重新下发到目标主机(管理员)",
"responses": {
"200": {
"$ref": "#/components/responses/Envelope"
},
"400": {
"$ref": "#/components/responses/BadRequest"
},
"500": {
"$ref": "#/components/responses/InternalError"
},
"503": {
"$ref": "#/components/responses/ServiceUnavailable"
}
}
}
},
"/api/v1/backup-schedule/run": {
"post": {
"tags": [
"Settings"
],
"operationId": "runBackupNow",
"summary": "在目标主机上立即执行一次整库备份(管理员,异步)",
"responses": {
"202": {
"$ref": "#/components/responses/Envelope"
},
"503": {
"$ref": "#/components/responses/ServiceUnavailable"
},
"400": {
"$ref": "#/components/responses/BadRequest"
},
"500": {
"$ref": "#/components/responses/InternalError"
}
}
}
},
"/health": {
"get": {
"tags": [