Files
knowledge-governance/internal/governance/backup_schedule.go
T
姚章浩 3f603aa293 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)。
2026-10-09 13:50:23 +08:00

84 lines
3.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package governance
import (
"fmt"
"regexp"
"strings"
"time"
)
// 整库逻辑备份的定时计划(平台「系统设置 → 存储与备份」)。校验集中在领域层:
// 非法值一律拒绝,绝不把半成品下发到数据面主机。
const (
BackupScheduleDefaultTime = "03:30"
BackupScheduleMinDays = 1
BackupScheduleMaxDays = 365
BackupScheduleMinMonths = 0
BackupScheduleMaxMonths = 24
)
// backupScheduleTime 形如 03:30;cron 用 24 小时制,不接受 24:00 及秒位。
var backupScheduleTime = regexp.MustCompile(`^([01][0-9]|2[0-3]):[0-5][0-9]$`)
// BackupSchedule 是一份可下发的备份计划(时间 + 保留策略)。
type BackupSchedule struct {
ID string
Enabled bool
ScheduleTime string
RetentionDays int
RetentionMonths int
LastAppliedAt *time.Time
LastAppliedSummary string
UpdatedBy string
UpdatedAt time.Time
}
// Validate 校验时间格式与保留范围;返回的错误直接可展示给操作者。
func (schedule BackupSchedule) Validate() error {
if !backupScheduleTime.MatchString(strings.TrimSpace(schedule.ScheduleTime)) {
return fmt.Errorf("定时时间必须是 HH:MM(00:00~23:59),收到 %q", schedule.ScheduleTime)
}
if schedule.RetentionDays < BackupScheduleMinDays || schedule.RetentionDays > BackupScheduleMaxDays {
return fmt.Errorf("储存天数需在 %d~%d 之间,收到 %d",
BackupScheduleMinDays, BackupScheduleMaxDays, schedule.RetentionDays)
}
if schedule.RetentionMonths < BackupScheduleMinMonths || schedule.RetentionMonths > BackupScheduleMaxMonths {
return fmt.Errorf("每月归档保留需在 %d~%d 之间,收到 %d",
BackupScheduleMinMonths, BackupScheduleMaxMonths, schedule.RetentionMonths)
}
return nil
}
// Normalize 填默认值并去掉时间两侧空白,便于比较"是否变化"。
func (schedule BackupSchedule) Normalize() BackupSchedule {
schedule.ScheduleTime = strings.TrimSpace(schedule.ScheduleTime)
if schedule.ScheduleTime == "" {
schedule.ScheduleTime = BackupScheduleDefaultTime
}
return schedule
}
// SameHostPlan 判断两份计划在数据面主机上是否等价(只看会写进 cron 的字段)。
func (schedule BackupSchedule) SameHostPlan(other BackupSchedule) bool {
return schedule.Enabled == other.Enabled &&
schedule.ScheduleTime == other.ScheduleTime &&
schedule.RetentionDays == other.RetentionDays &&
schedule.RetentionMonths == other.RetentionMonths
}
// DescribesNextRunUtc 仅用于日志:给出下一次执行的 UTC 时刻(按当天算)。
func (schedule BackupSchedule) NextRunAfter(now time.Time) (time.Time, error) {
if err := schedule.Validate(); err != nil {
return time.Time{}, err
}
hour, minute := 0, 0
if _, err := fmt.Sscanf(schedule.ScheduleTime, "%d:%d", &hour, &minute); err != nil {
return time.Time{}, err
}
next := time.Date(now.Year(), now.Month(), now.Day(), hour, minute, 0, 0, now.Location())
if !next.After(now) {
next = next.Add(24 * time.Hour)
}
return next, nil
}