Add detailed backup notifications
This commit is contained in:
@@ -286,7 +286,7 @@ func (s *Service) executeBackup(ctx context.Context, run model.Run) (result mode
|
||||
} else {
|
||||
run.Status, run.Message = "success", "backup completed"
|
||||
}
|
||||
run.SnapshotID, run.BytesAdded, run.FilesNew = summary.SnapshotID, summary.DataAdded, summary.FilesNew
|
||||
run.SnapshotID, run.BytesAdded, run.FilesNew, run.FilesChanged = summary.SnapshotID, summary.DataAdded, summary.FilesNew, summary.FilesChanged
|
||||
run.BytesProcessed, run.FilesProcessed = summary.TotalBytesProcessed, summary.TotalFilesProcessed
|
||||
return run
|
||||
}
|
||||
@@ -324,10 +324,11 @@ func (s *Service) finishJob(run model.Run, job model.Job, result model.Run) mode
|
||||
}
|
||||
|
||||
func (s *Service) notify(name string, run model.Run) {
|
||||
message := notificationMessage(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, "Backupper: "+name, run.Message, run.Status); err != nil {
|
||||
if err := s.notifier.Send(context.Background(), target, "Backupper: "+name, message, run.Status); err != nil {
|
||||
s.log.Warn("send notification failed", "target", target.ID, "type", target.Type, "error", err)
|
||||
}
|
||||
break
|
||||
@@ -336,6 +337,66 @@ func (s *Service) notify(name string, run model.Run) {
|
||||
}
|
||||
}
|
||||
|
||||
func notificationMessage(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}
|
||||
if run.StartedAt != nil {
|
||||
finished := now
|
||||
if run.FinishedAt != nil {
|
||||
finished = *run.FinishedAt
|
||||
}
|
||||
if duration := finished.Sub(*run.StartedAt); duration >= 0 {
|
||||
lines = append(lines, "Dauer: "+formatDuration(duration))
|
||||
}
|
||||
}
|
||||
if run.TaskType == "backup" && (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 != "" {
|
||||
lines = append(lines, "Snapshot: "+run.SnapshotID)
|
||||
}
|
||||
}
|
||||
if run.Message != "" && run.Message != "backup completed" {
|
||||
lines = append(lines, "Details: "+run.Message)
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func formatBytes(value int64) string {
|
||||
const unit = int64(1024)
|
||||
if value < unit {
|
||||
return fmt.Sprintf("%d B", value)
|
||||
}
|
||||
div, exp := unit, 0
|
||||
for n := value / unit; n >= unit && exp < 4; n /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%.1f %ciB", float64(value)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
|
||||
func formatDuration(duration time.Duration) string {
|
||||
duration = duration.Round(time.Second)
|
||||
if duration < time.Minute {
|
||||
return fmt.Sprintf("%d Sekunden", int(duration.Seconds()))
|
||||
}
|
||||
hours := int(duration / time.Hour)
|
||||
minutes := int(duration/time.Minute) % 60
|
||||
seconds := int(duration/time.Second) % 60
|
||||
if hours > 0 {
|
||||
return fmt.Sprintf("%d Std. %d Min. %d Sek.", hours, minutes, seconds)
|
||||
}
|
||||
return fmt.Sprintf("%d Min. %d Sek.", minutes, seconds)
|
||||
}
|
||||
|
||||
func (s *Service) job(id string) (model.Job, bool) {
|
||||
for _, item := range s.Config().Jobs {
|
||||
if item.ID == id {
|
||||
|
||||
Reference in New Issue
Block a user