Enrich backup notifications

This commit is contained in:
Mikei386
2026-06-21 21:34:05 +02:00
parent cc6d370c05
commit 64412c6597
8 changed files with 77 additions and 24 deletions
+51 -14
View File
@@ -746,17 +746,24 @@ func (s *Service) executeRestore(ctx context.Context, run model.Run) model.Run {
func (s *Service) finishJob(run model.Run, job model.Job, result model.Run) model.Run {
if result.Status == "success" || result.Status == "warning" || result.Status == "failed" {
go s.notify(job.Name, result)
repoName := ""
if job.RepositoryID != "" {
if repo, ok := s.repository(job.RepositoryID); ok {
repoName = repo.Name
}
}
go s.notify(job.Name, repoName, result)
}
return result
}
func (s *Service) notify(name string, run model.Run) {
message := notificationMessage(run, time.Now().UTC())
func (s *Service) notify(name, repository string, run model.Run) {
title := notificationTitle(name, repository, run)
message := notificationMessage(name, repository, run, time.Now().UTC())
for _, target := range s.Config().Notifications {
for _, event := range target.Events {
if event == run.Status {
if err := s.notifier.Send(context.Background(), target, "URBM: "+name, message, run.Status); err != nil {
if err := s.notifier.Send(context.Background(), target, title, message, run.Status); err != nil {
s.log.Warn("send notification failed", "target", target.ID, "type", target.Type, "error", err)
}
break
@@ -765,12 +772,36 @@ func (s *Service) notify(name string, run model.Run) {
}
}
func notificationMessage(run model.Run, now time.Time) string {
func notificationTitle(name, repository string, run model.Run) string {
icon := map[string]string{"success": "✅", "warning": "⚠️", "failed": "❌"}[run.Status]
if icon == "" {
icon = "️"
}
task := map[string]string{"backup": "Backup", "rsync": "Rsync", "restore": "Restore", "check": "Prüfung", "prune": "Bereinigung"}[run.TaskType]
if task == "" {
task = run.TaskType
if task != "" {
task = strings.ToUpper(task[:1]) + task[1:]
}
}
if repository != "" {
return fmt.Sprintf("%s URBM %s: %s", icon, task, repository)
}
return fmt.Sprintf("%s URBM %s: %s", icon, task, name)
}
func notificationMessage(name, repository string, run model.Run, now time.Time) string {
status := map[string]string{"success": "Erfolgreich", "warning": "Mit Warnung", "failed": "Fehlgeschlagen"}[run.Status]
if status == "" {
status = run.Status
}
lines := []string{"Status: " + status}
lines := []string{"📊 URBM-Zusammenfassung", "", "Status: " + status}
if name != "" {
lines = append(lines, "Job: "+name)
}
if repository != "" {
lines = append(lines, "Repository: "+repository)
}
if run.StartedAt != nil {
finished := now
if run.FinishedAt != nil {
@@ -781,16 +812,22 @@ func notificationMessage(run model.Run, now time.Time) string {
}
}
if (run.TaskType == "backup" || run.TaskType == "rsync") && (run.Status == "success" || run.Status == "warning") {
lines = append(lines,
"Neu gespeichert: "+formatBytes(run.BytesAdded),
fmt.Sprintf("Neue Dateien: %d", run.FilesNew),
fmt.Sprintf("Geänderte Dateien: %d", run.FilesChanged),
"Gesamtgröße dieses Backups: "+formatBytes(run.BytesProcessed),
fmt.Sprintf("Dateien insgesamt: %d", run.FilesProcessed),
)
if run.SnapshotID != "" {
if run.TaskType == "backup" && run.SnapshotID != "" {
lines = append(lines, "Snapshot: "+run.SnapshotID)
}
lines = append(lines,
"Verarbeitete Daten: "+formatBytes(run.BytesProcessed),
fmt.Sprintf("Verarbeitete Dateien: %d", run.FilesProcessed),
)
if run.TaskType == "backup" {
lines = append(lines,
"Neu gespeichert: "+formatBytes(run.BytesAdded),
fmt.Sprintf("Neue Dateien: %d", run.FilesNew),
fmt.Sprintf("Geänderte Dateien: %d", run.FilesChanged),
)
} else {
lines = append(lines, "Kopierte Daten: "+formatBytes(run.BytesAdded))
}
}
if run.Message != "" && run.Message != "backup completed" && run.Message != "rsync completed" {
lines = append(lines, "Details: "+run.Message)