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:
@@ -0,0 +1,83 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package governance
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestBackupScheduleValidateRejectsBadInput(t *testing.T) {
|
||||
valid := BackupSchedule{Enabled: true, ScheduleTime: "03:30", RetentionDays: 7, RetentionMonths: 3}
|
||||
if err := valid.Validate(); err != nil {
|
||||
t.Fatalf("合法计划被拒: %v", err)
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
schedule BackupSchedule
|
||||
}{
|
||||
{"时间缺前导零", BackupSchedule{ScheduleTime: "3:30", RetentionDays: 7}},
|
||||
{"时间越界", BackupSchedule{ScheduleTime: "24:00", RetentionDays: 7}},
|
||||
{"时间带秒", BackupSchedule{ScheduleTime: "03:30:00", RetentionDays: 7}},
|
||||
{"时间空", BackupSchedule{ScheduleTime: "", RetentionDays: 7}},
|
||||
{"天数为零", BackupSchedule{ScheduleTime: "03:30", RetentionDays: 0}},
|
||||
{"天数过大", BackupSchedule{ScheduleTime: "03:30", RetentionDays: 366}},
|
||||
{"月数越界", BackupSchedule{ScheduleTime: "03:30", RetentionDays: 7, RetentionMonths: 25}},
|
||||
}
|
||||
for _, item := range cases {
|
||||
if err := item.schedule.Validate(); err == nil {
|
||||
t.Errorf("%s:应当拒绝 %+v", item.name, item.schedule)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackupScheduleNormalizeFillsDefaultTime(t *testing.T) {
|
||||
normalized := BackupSchedule{Enabled: false, ScheduleTime: " "}.Normalize()
|
||||
if normalized.ScheduleTime != BackupScheduleDefaultTime {
|
||||
t.Fatalf("空时间应回落到默认 %q,得到 %q", BackupScheduleDefaultTime, normalized.ScheduleTime)
|
||||
}
|
||||
if got := (BackupSchedule{ScheduleTime: " 03:30 "}).Normalize().ScheduleTime; got != "03:30" {
|
||||
t.Fatalf("应去掉两侧空白,得到 %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// 页面靠 SameHostPlan 判断"库里的设置"和"主机上生效的计划"是否一致(不一致显示"待下发")。
|
||||
func TestBackupScheduleSameHostPlan(t *testing.T) {
|
||||
base := BackupSchedule{Enabled: true, ScheduleTime: "03:30", RetentionDays: 7, RetentionMonths: 3}
|
||||
if !base.SameHostPlan(base) {
|
||||
t.Fatal("同一份计划应判定一致")
|
||||
}
|
||||
if base.SameHostPlan(BackupSchedule{Enabled: true, ScheduleTime: "03:30", RetentionDays: 7, RetentionMonths: 3, UpdatedBy: "someone"}) == false {
|
||||
t.Fatal("非下发字段(updated_by)不应影响一致性")
|
||||
}
|
||||
changes := []BackupSchedule{
|
||||
{Enabled: false, ScheduleTime: "03:30", RetentionDays: 7, RetentionMonths: 3},
|
||||
{Enabled: true, ScheduleTime: "04:30", RetentionDays: 7, RetentionMonths: 3},
|
||||
{Enabled: true, ScheduleTime: "03:30", RetentionDays: 14, RetentionMonths: 3},
|
||||
{Enabled: true, ScheduleTime: "03:30", RetentionDays: 7, RetentionMonths: 6},
|
||||
}
|
||||
for _, changed := range changes {
|
||||
if base.SameHostPlan(changed) {
|
||||
t.Errorf("以下变化应判定不一致: %+v", changed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackupScheduleNextRunAfter(t *testing.T) {
|
||||
schedule := BackupSchedule{ScheduleTime: "03:30", RetentionDays: 7}
|
||||
morning := time.Date(2026, 10, 9, 1, 0, 0, 0, time.UTC)
|
||||
next, err := schedule.NextRunAfter(morning)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if next.Hour() != 3 || next.Minute() != 30 || next.Day() != 9 {
|
||||
t.Fatalf("当天还没到点应给当天,得到 %s", next)
|
||||
}
|
||||
evening := time.Date(2026, 10, 9, 22, 0, 0, 0, time.UTC)
|
||||
next, err = schedule.NextRunAfter(evening)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if next.Day() != 10 {
|
||||
t.Fatalf("已过点应给次日,得到 %s", next)
|
||||
}
|
||||
if _, err := (BackupSchedule{ScheduleTime: "99:99", RetentionDays: 7}).NextRunAfter(evening); err == nil {
|
||||
t.Fatal("非法时间应返回错误")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user