fix(backup): 宿主采集器按 du 口径去重硬链接并拒绝清单符号链接

备份目录 root:root 0700,API 不再遍历;宿主采集器只读 stat/du,原子写快照。统计包含目录自身、树内 (dev,ino) 硬链接去重,单库与整批各自独立与 du -sB1 对齐;MANIFEST 仅接受目录内常规非符号链接文件并 O_NOFOLLOW 打开。快照缺失/陈旧/不完整时批次与子库字节为 null,页面不画容量图、不使用部分字节。
This commit is contained in:
姚章浩
2026-11-20 01:41:28 +08:00
parent 033f702d41
commit 63984dbfbe
17 changed files with 1798 additions and 937 deletions
+4 -4
View File
@@ -7,15 +7,15 @@ import (
"knowledge-governance/internal/backupinventory"
)
// BackupInventoryReader 只读盘点备份主机目录。它与 SSH 主机计划读回完全独立:
// 挂载没接线不应拖垮计划读回,计划读回失败也不应把历史批次说成"没有"。
// BackupInventoryReader 读取宿主采集器写出的元数据快照。它与 SSH 主机计划读回完全独立:
// 快照缺失/陈旧不应拖垮计划读回,计划读回失败也不应把历史批次说成"没有"。
type BackupInventoryReader interface {
Available() bool
Snapshot(context.Context) backupinventory.Snapshot
}
// handleBackupInventory 返回真实批次、子库路径与实测容量。
// 未接线/目录不可读/清单损坏都作为 200 的字段返回,由页面区分原因;
// handleBackupInventory 返回宿主采集器快照里的批次、子库路径与容量。
// 未接线/快照缺失/陈旧/不完整/路径不一致都作为 200 的字段返回,由页面区分原因;
// 只有投影本身失败才 500。
func (config Config) handleBackupInventory(w http.ResponseWriter, r *http.Request) {
snapshot := config.BackupInventory.Snapshot(r.Context())
+9 -6
View File
@@ -21,30 +21,33 @@ func (fake fakeBackupInventory) Snapshot(context.Context) backupinventory.Snapsh
}
func TestBackupInventoryProjectionPassesRealFields(t *testing.T) {
actual := int64(1024)
databaseActual := int64(512)
config := Config{BackupInventory: fakeBackupInventory{snapshot: backupinventory.Snapshot{
Wired: true,
Root: "/var/lib/knowledge-governance/backup-host/rds-logical",
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},
Mount: backupinventory.MountInfo{
MountVerified: true,
ReadOnly: true,
Evidence: "挂载设备 20 与容器根盘 10 不同",
Evidence: "宿主采集器:/dev/sdb1 挂在 /srv/kb-backups(ext4)",
},
Capacity: backupinventory.Capacity{Measured: true, TotalBytes: 100, FreeBytes: 40, AvailableBytes: 35, UsedBytes: 60},
Batches: []backupinventory.Batch{{
Stamp: "20261118T193001Z",
HostPath: "/srv/kb-backups/rds-logical/20261118T193001Z",
ManifestState: backupinventory.ManifestOK,
ActualBytes: 1024,
ActualBytes: &actual,
Databases: []backupinventory.Database{{
Name: "weknora_v090prod",
HostPath: "/srv/kb-backups/rds-logical/20261118T193001Z/weknora_v090prod.dir",
Present: true,
ActualBytes: 512,
ActualBytes: &databaseActual,
}},
}},
Totals: backupinventory.Totals{BatchCount: 1, LatestStamp: "20261118T193001Z"},
Totals: backupinventory.Totals{BatchCount: 1, ActualBytes: &actual, LatestStamp: "20261118T193001Z", Complete: true},
}}}
recorder := httptest.NewRecorder()
config.handleBackupInventory(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/backup-schedule/inventory", nil))
@@ -64,7 +67,7 @@ func TestBackupInventoryProjectionPassesRealFields(t *testing.T) {
payload.Data.Batches[0].Databases[0].HostPath != "/srv/kb-backups/rds-logical/20261118T193001Z/weknora_v090prod.dir" {
t.Fatalf("批次/子库真实路径丢失: %+v", payload.Data.Batches)
}
if payload.Data.Batches[0].ActualBytes != 1024 {
if payload.Data.Batches[0].ActualBytes == nil || *payload.Data.Batches[0].ActualBytes != 1024 {
t.Fatalf("实际占盘字段丢失: %+v", payload.Data.Batches[0])
}
}