#!/usr/bin/env bash # gniza/lib/notify.sh — Email notifications send_notification() { local subject="$1" local body="$2" local success="${3:-true}" # Check if notifications are configured [[ -z "${NOTIFY_EMAIL:-}" ]] && return 0 case "${NOTIFY_ON:-$DEFAULT_NOTIFY_ON}" in never) return 0 ;; failure) [[ "$success" == "true" ]] && return 0 ;; always) ;; esac local hostname; hostname=$(hostname -f) local full_subject="[gniza] [$hostname] $subject" log_debug "Sending notification to $NOTIFY_EMAIL: $full_subject" if command -v mail &>/dev/null; then echo "$body" | mail -s "$full_subject" "$NOTIFY_EMAIL" elif command -v sendmail &>/dev/null; then { echo "To: $NOTIFY_EMAIL" echo "Subject: $full_subject" echo "Content-Type: text/plain; charset=UTF-8" echo "" echo "$body" } | sendmail -t else log_warn "No mail command available, cannot send notification" return 1 fi log_debug "Notification sent" return 0 } send_backup_report() { local total="$1" local succeeded="$2" local failed="$3" local duration="$4" local failed_accounts="$5" local success="true" local status="SUCCESS" if (( failed > 0 )); then if (( succeeded > 0 )); then status="PARTIAL FAILURE" else status="FAILURE" fi success="false" fi local body="" body+="Backup Report: $status"$'\n' body+="=============================="$'\n' body+="Hostname: $(hostname -f)"$'\n' body+="Timestamp: $(date -u +"%Y-%m-%d %H:%M:%S UTC")"$'\n' body+="Duration: $(human_duration "$duration")"$'\n' body+=""$'\n' body+="Accounts: $total total, $succeeded succeeded, $failed failed"$'\n' if [[ -n "$failed_accounts" ]]; then body+=""$'\n' body+="Failed accounts:"$'\n' body+="$failed_accounts"$'\n' fi if [[ -n "$LOG_FILE" ]]; then body+=""$'\n' body+="Log file: $LOG_FILE"$'\n' fi send_notification "Backup $status ($succeeded/$total)" "$body" "$success" }