- 采集器上报 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:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user