- 采集器上报 backup_root/usage/quota(加性字段),API 用 BACKUP_HOST_ROOT 校验并分别降级占用/配额 - 页面配额行改为备份占用 X / 配额 50 GB,4 条告警改用备份口径,解除本页归档依赖 - logical-backup.sh 预检按字节清理(保留最新、空间不足零删除),dump 后复查保护新批次 - backup_layout 写入 KB_BACKUP_QUOTA_BYTES;--show 输出根占用;新增 prune 工具与自检
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package backuphost
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 配额清理工具是唯一会删历史备份的代码。它的 self-test 覆盖「最旧优先 / 预检保留最新 /
|
||||
// 空间不够零删除 / 非批次日录与符号链接不动 / dump 后保护新批次」,必须随 Go 测试一起跑。
|
||||
func TestPruneBackupBatchesSelfTest(t *testing.T) {
|
||||
python, err := exec.LookPath("python3")
|
||||
if err != nil {
|
||||
t.Skip("python3 not available")
|
||||
}
|
||||
command := exec.Command(python, "../../scripts/prune_backup_batches.py", "--self-test")
|
||||
output, err := command.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("prune self-test failed: %v\n%s", err, output)
|
||||
}
|
||||
if !strings.Contains(string(output), "self-test ok") {
|
||||
t.Fatalf("prune self-test output unexpected: %s", output)
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,9 @@ type Config struct {
|
||||
MetadataPath string
|
||||
// HostRoot 是宿主上的 rds-logical 绝对路径,用于校验快照里的 root/批次路径。
|
||||
HostRoot string
|
||||
// BackupRoot 是宿主上的备份根(KB_BACKUP_ROOT),用于校验快照里的根占用范围。
|
||||
// 它必须与 BACKUP_HOST_ROOT 同源;不同源时占用不可信,整份快照拒绝。
|
||||
BackupRoot string
|
||||
// Source 是展示来源;快照里的 source 非空时以快照为准。
|
||||
Source string
|
||||
// MaxAge 超过即视为陈旧,旧快照的批次与容量一律不使用。
|
||||
@@ -85,6 +88,19 @@ type Capacity struct {
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// Usage 是**备份根累计占用**(配额约束的对象),不是 rds-logical 批次之和。
|
||||
// Measured=false 时所有字节为 null:部分遍历永远不能当占盘。
|
||||
type Usage struct {
|
||||
Root string `json:"root"`
|
||||
Measured bool `json:"measured"`
|
||||
Complete bool `json:"complete"`
|
||||
ActualBytes *int64 `json:"actual_bytes"`
|
||||
ApparentBytes *int64 `json:"apparent_bytes"`
|
||||
FileCount *int `json:"file_count"`
|
||||
DirCount *int `json:"dir_count"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type Database struct {
|
||||
Name string `json:"name"`
|
||||
DirName string `json:"dir_name"`
|
||||
@@ -138,12 +154,16 @@ type Snapshot struct {
|
||||
Metadata MetadataInfo `json:"metadata"`
|
||||
Root string `json:"root"`
|
||||
HostRoot string `json:"host_root"`
|
||||
BackupRoot string `json:"backup_root"`
|
||||
Source string `json:"source"`
|
||||
ObservedAt string `json:"observed_at"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Measurement string `json:"measurement"`
|
||||
Mount MountInfo `json:"mount"`
|
||||
Capacity Capacity `json:"capacity"`
|
||||
Usage Usage `json:"usage"`
|
||||
QuotaBytes *int64 `json:"quota_bytes"`
|
||||
QuotaError string `json:"quota_error,omitempty"`
|
||||
Batches []Batch `json:"batches"`
|
||||
Totals Totals `json:"totals"`
|
||||
IncompleteCount int `json:"incomplete_count"`
|
||||
@@ -181,6 +201,10 @@ type metadata struct {
|
||||
Host string `json:"host"`
|
||||
Source string `json:"source"`
|
||||
Root string `json:"root"`
|
||||
BackupRoot string `json:"backup_root"`
|
||||
Usage metadataUsage `json:"usage"`
|
||||
QuotaBytes *int64 `json:"quota_bytes"`
|
||||
QuotaError string `json:"quota_error"`
|
||||
Complete bool `json:"complete"`
|
||||
Errors []string `json:"errors"`
|
||||
Filesystem metadataFilesystem `json:"filesystem"`
|
||||
@@ -190,6 +214,16 @@ type metadata struct {
|
||||
Batches []metadataBatch `json:"batches"`
|
||||
}
|
||||
|
||||
type metadataUsage struct {
|
||||
Root string `json:"root"`
|
||||
Complete bool `json:"complete"`
|
||||
ActualBytes *int64 `json:"actual_bytes"`
|
||||
ApparentBytes *int64 `json:"apparent_bytes"`
|
||||
FileCount *int `json:"file_count"`
|
||||
DirCount *int `json:"dir_count"`
|
||||
Errors []string `json:"errors"`
|
||||
}
|
||||
|
||||
type metadataFilesystem struct {
|
||||
MountPoint string `json:"mount_point"`
|
||||
MountSource string `json:"mount_source"`
|
||||
@@ -313,6 +347,14 @@ func (r *Reader) Snapshot(ctx context.Context) Snapshot {
|
||||
return invalid(snapshot, fmt.Sprintf("元数据 root=%s 与配置 %s 不一致", root, expectedRoot))
|
||||
}
|
||||
snapshot.Root = root
|
||||
backupRoot, usage, quotaBytes, quotaError, err := projectUsageAndQuota(r.config.BackupRoot, root, file)
|
||||
if err != nil {
|
||||
return invalid(snapshot, err.Error())
|
||||
}
|
||||
snapshot.BackupRoot = backupRoot
|
||||
snapshot.Usage = usage
|
||||
snapshot.QuotaBytes = quotaBytes
|
||||
snapshot.QuotaError = quotaError
|
||||
if err := validateBatches(root, file.Batches); err != nil {
|
||||
return invalid(snapshot, err.Error())
|
||||
}
|
||||
@@ -366,6 +408,66 @@ func (r *Reader) observedAt() string {
|
||||
return r.now().UTC().Format(time.RFC3339)
|
||||
}
|
||||
|
||||
// projectUsageAndQuota 校验「备份根占用」与「配额」并把它们投影给页面。
|
||||
//
|
||||
// 三条独立边界:
|
||||
// 1. 旧采集器没有 usage/quota —— 批次照旧可用,占用与配额显式「未上报」,绝不用批次和冒充;
|
||||
// 2. 路径必须与部署配置的备份根完全一致,批次根必须在其下 —— 否则整份快照拒绝;
|
||||
// 3. 遍历不完整/配额非法 —— 只把对应字段降为未知,不牵连批次表与磁盘容量。
|
||||
func projectUsageAndQuota(configuredRoot, batchRoot string, file metadata) (string, Usage, *int64, string, error) {
|
||||
expected := path.Clean(strings.TrimSpace(configuredRoot))
|
||||
reported := strings.TrimSpace(file.Usage.Root)
|
||||
declared := strings.TrimSpace(file.BackupRoot)
|
||||
usageReported := reported != "" || declared != "" || file.Usage.Complete ||
|
||||
file.Usage.ActualBytes != nil || len(file.Usage.Errors) > 0
|
||||
if !usageReported {
|
||||
quotaError := strings.TrimSpace(file.QuotaError)
|
||||
if quotaError == "" {
|
||||
quotaError = "采集器未上报备份配额(需要升级采集器)"
|
||||
}
|
||||
return expected, Usage{Error: "采集器未上报备份根占用(需要升级采集器)"}, nil, quotaError, nil
|
||||
}
|
||||
if expected == "" || !path.IsAbs(expected) {
|
||||
return "", Usage{}, nil, "", errors.New("备份根未配置:无法校验快照里的根占用")
|
||||
}
|
||||
if reported != expected || declared != expected {
|
||||
return "", Usage{}, nil, "", fmt.Errorf("快照 backup_root=%q/usage.root=%q 与配置 %q 不一致", declared, reported, expected)
|
||||
}
|
||||
if batchRoot != expected && !strings.HasPrefix(batchRoot, expected+"/") {
|
||||
return "", Usage{}, nil, "", fmt.Errorf("批次根 %s 不在备份根 %s 下", batchRoot, expected)
|
||||
}
|
||||
usage := Usage{Root: reported, Complete: file.Usage.Complete}
|
||||
if file.Usage.Complete && file.Usage.ActualBytes != nil {
|
||||
usage.Measured = true
|
||||
usage.ActualBytes = file.Usage.ActualBytes
|
||||
usage.ApparentBytes = file.Usage.ApparentBytes
|
||||
usage.FileCount = file.Usage.FileCount
|
||||
usage.DirCount = file.Usage.DirCount
|
||||
} else {
|
||||
usage.Complete = false
|
||||
usage.ActualBytes = nil
|
||||
usage.ApparentBytes = nil
|
||||
usage.FileCount = nil
|
||||
usage.DirCount = nil
|
||||
reasons := limit(file.Usage.Errors, 3)
|
||||
if len(reasons) == 0 {
|
||||
usage.Error = "备份根占用未知:采集器未给出完整字节"
|
||||
} else {
|
||||
usage.Error = "备份根占用未知:" + strings.Join(reasons, ";")
|
||||
}
|
||||
}
|
||||
quota := file.QuotaBytes
|
||||
quotaError := strings.TrimSpace(file.QuotaError)
|
||||
if quota != nil && *quota <= 0 {
|
||||
quota = nil
|
||||
quotaError = fmt.Sprintf("备份配额必须为正整数:%d", *file.QuotaBytes)
|
||||
}
|
||||
if quota == nil && quotaError == "" {
|
||||
quotaError = "采集器未上报备份配额(需要升级采集器)"
|
||||
}
|
||||
return expected, usage, quota, quotaError, nil
|
||||
}
|
||||
|
||||
func invalid(snapshot Snapshot, reason string) Snapshot {
|
||||
snapshot.Metadata.Valid = false
|
||||
snapshot.Metadata.Complete = false
|
||||
@@ -374,6 +476,8 @@ func invalid(snapshot Snapshot, reason string) Snapshot {
|
||||
snapshot.Batches = []Batch{}
|
||||
snapshot.Capacity = Capacity{Error: reason}
|
||||
snapshot.Totals = Totals{}
|
||||
snapshot.Usage = Usage{Error: reason}
|
||||
snapshot.QuotaBytes = nil
|
||||
return snapshot
|
||||
}
|
||||
|
||||
|
||||
@@ -28,8 +28,14 @@ func validMetadata() metadata {
|
||||
Host: "weknora-test",
|
||||
Source: "weknora-test:" + hostRoot,
|
||||
Root: hostRoot,
|
||||
Complete: true,
|
||||
Errors: []string{},
|
||||
BackupRoot: "/srv/kb-backups",
|
||||
Usage: metadataUsage{
|
||||
Root: "/srv/kb-backups", Complete: true,
|
||||
ActualBytes: int64p(20000), ApparentBytes: int64p(18000), FileCount: intp(12), DirCount: intp(4),
|
||||
},
|
||||
QuotaBytes: int64p(53687091200),
|
||||
Complete: true,
|
||||
Errors: []string{},
|
||||
Filesystem: metadataFilesystem{
|
||||
MountPoint: "/srv/kb-backups",
|
||||
MountSource: "/dev/sdb1",
|
||||
@@ -108,7 +114,7 @@ func writeMetadata(t *testing.T, value metadata) string {
|
||||
}
|
||||
|
||||
func readerAt(filename string, now time.Time) *Reader {
|
||||
reader := New(Config{MetadataPath: filename, HostRoot: hostRoot, Source: "configured", MaxAge: 15 * time.Minute})
|
||||
reader := New(Config{MetadataPath: filename, HostRoot: hostRoot, BackupRoot: "/srv/kb-backups", Source: "configured", MaxAge: 15 * time.Minute})
|
||||
reader.now = func() time.Time { return now }
|
||||
return reader
|
||||
}
|
||||
@@ -156,7 +162,82 @@ func TestSnapshotReadsHostMetadataAndPaths(t *testing.T) {
|
||||
t.Fatalf("汇总不对: %+v", snapshot.Totals)
|
||||
}
|
||||
if snapshot.Source != "weknora-test:"+hostRoot || snapshot.Measurement != "host_collector" {
|
||||
t.Fatalf("来源/测量方式不对: %+v", snapshot)
|
||||
t.Fatalf("来源/测量方式不对: %+v", snapshot.Source)
|
||||
}
|
||||
if snapshot.BackupRoot != "/srv/kb-backups" || !snapshot.Usage.Measured || snapshot.Usage.ActualBytes == nil || *snapshot.Usage.ActualBytes != 20000 {
|
||||
t.Fatalf("备份根占用没有投影: %+v", snapshot.Usage)
|
||||
}
|
||||
if snapshot.QuotaBytes == nil || *snapshot.QuotaBytes != 53687091200 {
|
||||
t.Fatalf("配额没有投影: %v", snapshot.QuotaBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotLegacyMetadataKeepsBatchesButOccupancyUnknown(t *testing.T) {
|
||||
value := validMetadata()
|
||||
value.BackupRoot = ""
|
||||
value.Usage = metadataUsage{}
|
||||
value.QuotaBytes = nil
|
||||
value.QuotaError = ""
|
||||
filename := writeMetadata(t, value)
|
||||
snapshot := readerAt(filename, time.Date(2026, 11, 20, 1, 5, 0, 0, time.UTC)).Snapshot(context.Background())
|
||||
if !snapshot.Metadata.Valid || !snapshot.Metadata.Complete {
|
||||
t.Fatalf("旧快照仍应可用: %+v", snapshot.Metadata)
|
||||
}
|
||||
if len(snapshot.Batches) != 3 || snapshot.Totals.ActualBytes == nil {
|
||||
t.Fatalf("旧快照的批次不能丢: %+v", snapshot.Batches)
|
||||
}
|
||||
if snapshot.Usage.Measured || snapshot.Usage.ActualBytes != nil || snapshot.Usage.Error == "" {
|
||||
t.Fatalf("缺少 usage 必须显式未上报: %+v", snapshot.Usage)
|
||||
}
|
||||
if snapshot.QuotaBytes != nil || snapshot.QuotaError == "" {
|
||||
t.Fatalf("缺少配额必须显式未上报: %v/%q", snapshot.QuotaBytes, snapshot.QuotaError)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotRejectsUsageRootMismatch(t *testing.T) {
|
||||
value := validMetadata()
|
||||
value.Usage.Root = "/data/kb-backups"
|
||||
filename := writeMetadata(t, value)
|
||||
snapshot := readerAt(filename, time.Date(2026, 11, 20, 1, 5, 0, 0, time.UTC)).Snapshot(context.Background())
|
||||
if snapshot.Metadata.Valid || len(snapshot.Batches) != 0 || snapshot.Usage.Measured {
|
||||
t.Fatalf("占用根与配置不一致必须整块拒绝: %+v", snapshot)
|
||||
}
|
||||
if !strings.Contains(snapshot.Error, "不一致") {
|
||||
t.Fatalf("不一致原因不清: %q", snapshot.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotIncompleteUsageDoesNotBlankBatches(t *testing.T) {
|
||||
value := validMetadata()
|
||||
value.Usage.Complete = false
|
||||
value.Usage.ActualBytes = int64p(123) // 部分计数:必须被丢弃
|
||||
value.Usage.Errors = []string{"遍历失败:PermissionError: Permission denied"}
|
||||
filename := writeMetadata(t, value)
|
||||
snapshot := readerAt(filename, time.Date(2026, 11, 20, 1, 5, 0, 0, time.UTC)).Snapshot(context.Background())
|
||||
if !snapshot.Metadata.Valid || !snapshot.Metadata.Complete {
|
||||
t.Fatalf("根占用不完整不能牵连整份快照: %+v", snapshot.Metadata)
|
||||
}
|
||||
if len(snapshot.Batches) != 3 || snapshot.Totals.ActualBytes == nil || !snapshot.Capacity.Measured {
|
||||
t.Fatalf("批次与磁盘容量必须保留: %+v", snapshot)
|
||||
}
|
||||
if snapshot.Usage.Measured || snapshot.Usage.ActualBytes != nil || !strings.Contains(snapshot.Usage.Error, "Permission") {
|
||||
t.Fatalf("占用必须为 null 并带原因: %+v", snapshot.Usage)
|
||||
}
|
||||
if snapshot.QuotaBytes == nil || *snapshot.QuotaBytes != 53687091200 {
|
||||
t.Fatalf("配额是配置事实,应保留: %v", snapshot.QuotaBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotRejectsInvalidQuota(t *testing.T) {
|
||||
value := validMetadata()
|
||||
value.QuotaBytes = int64p(0)
|
||||
filename := writeMetadata(t, value)
|
||||
snapshot := readerAt(filename, time.Date(2026, 11, 20, 1, 5, 0, 0, time.UTC)).Snapshot(context.Background())
|
||||
if snapshot.QuotaBytes != nil || !strings.Contains(snapshot.QuotaError, "正整数") {
|
||||
t.Fatalf("非法配额必须降为未知: %v/%q", snapshot.QuotaBytes, snapshot.QuotaError)
|
||||
}
|
||||
if !snapshot.Metadata.Valid {
|
||||
t.Fatalf("配额非法不应否掉批次: %+v", snapshot.Metadata)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,11 +24,16 @@ func TestBackupInventoryProjectionPassesRealFields(t *testing.T) {
|
||||
actual := int64(1024)
|
||||
databaseActual := int64(512)
|
||||
config := Config{BackupInventory: fakeBackupInventory{snapshot: backupinventory.Snapshot{
|
||||
Wired: true,
|
||||
Root: "/srv/kb-backups/rds-logical",
|
||||
HostRoot: "/srv/kb-backups/rds-logical",
|
||||
Source: "weknora-test:/srv/kb-backups/rds-logical",
|
||||
Metadata: backupinventory.MetadataInfo{Configured: true, Present: true, Valid: true, Fresh: true, Complete: true},
|
||||
Wired: true,
|
||||
Root: "/srv/kb-backups/rds-logical",
|
||||
HostRoot: "/srv/kb-backups/rds-logical",
|
||||
Source: "weknora-test:/srv/kb-backups/rds-logical",
|
||||
Metadata: backupinventory.MetadataInfo{Configured: true, Present: true, Valid: true, Fresh: true, Complete: true},
|
||||
BackupRoot: "/srv/kb-backups",
|
||||
Usage: backupinventory.Usage{
|
||||
Root: "/srv/kb-backups", Measured: true, Complete: true, ActualBytes: &actual,
|
||||
},
|
||||
QuotaBytes: func() *int64 { value := int64(53687091200); return &value }(),
|
||||
Mount: backupinventory.MountInfo{
|
||||
MountVerified: true,
|
||||
ReadOnly: true,
|
||||
@@ -70,6 +75,13 @@ func TestBackupInventoryProjectionPassesRealFields(t *testing.T) {
|
||||
if payload.Data.Batches[0].ActualBytes == nil || *payload.Data.Batches[0].ActualBytes != 1024 {
|
||||
t.Fatalf("实际占盘字段丢失: %+v", payload.Data.Batches[0])
|
||||
}
|
||||
if payload.Data.BackupRoot != "/srv/kb-backups" || !payload.Data.Usage.Measured ||
|
||||
payload.Data.Usage.ActualBytes == nil || *payload.Data.Usage.ActualBytes != 1024 {
|
||||
t.Fatalf("备份根占用字段丢失: %+v", payload.Data.Usage)
|
||||
}
|
||||
if payload.Data.QuotaBytes == nil || *payload.Data.QuotaBytes != 53687091200 {
|
||||
t.Fatalf("配额字段丢失: %v", payload.Data.QuotaBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackupInventoryUnconfiguredIsNotAnError(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user