Include retention and prune details in notifications

This commit is contained in:
Mikei386
2026-07-10 11:42:06 +02:00
parent 5aa28cf43e
commit c2f0f0b277
9 changed files with 91 additions and 19 deletions
+65 -7
View File
@@ -449,6 +449,8 @@ func (s *Service) execute(ctx context.Context, run model.Run) (result model.Run)
return failed(run, "validation", "repository no longer exists")
}
maintenanceRepoName = repo.Name
var pruneStatsBefore restic.Stats
pruneStatsBeforeOK := false
live := run
appendLiveLog(&live, strings.ToUpper(run.TaskType)+" wird vorbereitet")
s.updateActive(live)
@@ -465,6 +467,13 @@ func (s *Service) execute(ctx context.Context, run model.Run) (result model.Run)
if run.TaskType == "check" {
err = s.restic.Check(ctx, mounted.Repository)
} else {
if stats, statsErr := s.restic.Stats(ctx, mounted.Repository, "raw-data"); statsErr == nil {
pruneStatsBefore, pruneStatsBeforeOK = stats, true
appendLiveLog(&live, "Repository-Größe vor Bereinigung: "+formatBytes(stats.TotalSize))
s.updateActive(live)
} else {
appendLiveLog(&live, "Repository-Größe vor Bereinigung konnte nicht ermittelt werden: "+statsErr.Error())
}
err = s.restic.Prune(ctx, mounted.Repository)
}
if err != nil {
@@ -474,6 +483,18 @@ func (s *Service) execute(ctx context.Context, run model.Run) (result model.Run)
return failedRun
}
run.Status, run.Message = "success", run.TaskType+" completed"
if run.TaskType == "prune" && pruneStatsBeforeOK {
run.RepositoryBefore = pruneStatsBefore.TotalSize
if stats, statsErr := s.restic.Stats(ctx, mounted.Repository, "raw-data"); statsErr == nil {
run.RepositoryAfter = stats.TotalSize
if run.RepositoryBefore > run.RepositoryAfter {
run.RepositoryFreed = run.RepositoryBefore - run.RepositoryAfter
}
appendLiveLog(&live, fmt.Sprintf("Repository-Größe nach Bereinigung: %s, freigegeben: %s", formatBytes(run.RepositoryAfter), formatBytes(run.RepositoryFreed)))
} else {
appendLiveLog(&live, "Repository-Größe nach Bereinigung konnte nicht ermittelt werden: "+statsErr.Error())
}
}
appendLiveLog(&live, strings.ToUpper(run.TaskType)+" abgeschlossen")
run.LiveLog = live.LiveLog
return run
@@ -586,7 +607,8 @@ func (s *Service) executeBackup(ctx context.Context, run model.Run) (result mode
result.Status, result.ErrorCode, result.Message = "warning", "connectivity", result.Message+"; repository unmount failed: "+err.Error()
}
}()
previousSnapshot, previousSnapshotOK, previousSnapshotErr := s.latestJobSnapshot(ctx, mounted.Repository, job.ID)
previousSnapshots, previousSnapshotErr := s.jobSnapshots(ctx, mounted.Repository, job.ID)
previousSnapshot, previousSnapshotOK := latestSnapshot(previousSnapshots)
if previousSnapshotErr != nil {
appendLiveLog(&live, "Änderungsliste: vorheriger Snapshot konnte nicht ermittelt werden: "+previousSnapshotErr.Error())
s.updateActive(live)
@@ -686,6 +708,20 @@ func (s *Service) executeBackup(ctx context.Context, run model.Run) (result mode
run.Status, run.Message, run.ErrorCode = "warning", "backup succeeded but retention failed: "+err.Error(), "repository"
} else {
run.Status, run.Message = "success", "backup completed"
if summary.SnapshotID != "" && previousSnapshotErr == nil {
run.RetentionBefore = len(previousSnapshots)
if remainingSnapshots, remainingErr := s.jobSnapshots(ctx, mounted.Repository, job.ID); remainingErr == nil {
run.RetentionAfter = len(remainingSnapshots)
expectedAfter := run.RetentionBefore + 1
if expectedAfter > run.RetentionAfter {
run.RetentionRemoved = expectedAfter - run.RetentionAfter
}
appendLiveLog(&live, fmt.Sprintf("Aufbewahrung angewendet: %d Snapshot(s) entfernt, %d bleiben", run.RetentionRemoved, run.RetentionAfter))
s.updateActive(live)
} else {
appendLiveLog(&live, "Aufbewahrung angewendet; verbleibende Snapshots konnten nicht ermittelt werden: "+remainingErr.Error())
}
}
}
run.SnapshotID = summary.SnapshotID
run.BytesAdded = summary.DataAdded + imageSummary.DataAdded
@@ -701,24 +737,31 @@ func (s *Service) executeBackup(ctx context.Context, run model.Run) (result mode
return run
}
func (s *Service) latestJobSnapshot(ctx context.Context, repo model.Repository, jobID string) (model.Snapshot, bool, error) {
func (s *Service) jobSnapshots(ctx context.Context, repo model.Repository, jobID string) ([]model.Snapshot, error) {
snapshots, err := s.restic.Snapshots(ctx, repo)
if err != nil {
return model.Snapshot{}, false, err
return nil, err
}
tag := "job:" + jobID
filtered := make([]model.Snapshot, 0, len(snapshots))
for _, snapshot := range snapshots {
if hasTag(snapshot.Tags, tag) && !hasTag(snapshot.Tags, "usb-image") {
filtered = append(filtered, snapshot)
}
}
return filtered, nil
}
func latestSnapshot(snapshots []model.Snapshot) (model.Snapshot, bool) {
var latest model.Snapshot
found := false
for _, snapshot := range snapshots {
if !hasTag(snapshot.Tags, tag) || hasTag(snapshot.Tags, "usb-image") {
continue
}
if !found || snapshot.Time.After(latest.Time) {
latest = snapshot
found = true
}
}
return latest, found, nil
return latest, found
}
func hasTag(tags []string, wanted string) bool {
@@ -934,6 +977,21 @@ func notificationMessage(name, repository string, run model.Run, now time.Time)
lines = append(lines, "📤 Kopiert: "+formatBytes(run.BytesAdded))
}
}
if run.TaskType == "backup" && run.RetentionAfter > 0 {
lines = append(lines,
"",
fmt.Sprintf("🧹 Aufbewahrung: %d Snapshot(s) entfernt", run.RetentionRemoved),
fmt.Sprintf("📸 Verbleibende Snapshots: %d", run.RetentionAfter),
)
}
if run.TaskType == "prune" && run.RepositoryBefore > 0 && run.RepositoryAfter > 0 {
lines = append(lines,
"",
"📦 Vorher: "+formatBytes(run.RepositoryBefore),
"📦 Nachher: "+formatBytes(run.RepositoryAfter),
"🧹 Freigegeben: "+formatBytes(run.RepositoryFreed),
)
}
lines = append(lines, "", "🔐 Ziel: "+notificationTargetLabel(run.TaskType), statusIcon(run.Status)+" Status: "+status)
if run.Message != "" && run.Message != run.TaskType+" completed" {
lines = append(lines, "⚠️ Details: "+run.Message)