diff --git a/CHANGELOG.md b/CHANGELOG.md index c11a9831625b39a3317d941e2741f6eed3dce243..7e6c548e874fa089556cf9e31e03caf481f46823 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- [core] raise error if mail isn't found in $PATH and reportemail = yes + ### Fixed - [build] make build reproducible regardless of usrmerge (DEBBUG-915222) diff --git a/src/backupninja.in b/src/backupninja.in index 5d482f987440acd55c22ebca7268ad7cc58c2109..c8ba5d4883fac5aa2436139842fb737b26f83463 100755 --- a/src/backupninja.in +++ b/src/backupninja.in @@ -613,29 +613,34 @@ else doit=0 fi if [ $doit == 1 ]; then - debug "send report to $reportemail" - hostname=`hostname` - [ $warnings == 0 ] || subject="WARNING" - [ $errors == 0 ] || subject="ERROR" - [ $fatals == 0 ] || subject="FAILED" - [ $halts == 0 ] || subject="HALTED" - - { - for ((i=0; i < ${#messages[@]} ; i++)); do - echo ${messages[$i]} - done - echo -e "$errormsg" - if [ "$reportspace" == "yes" ]; then - previous="" - for i in $(ls "$configdirectory"); do - backuploc=$(grep ^directory "$configdirectory"/"$i" | @AWK@ '{print $3}') - if [ "$backuploc" != "$previous" -a -n "$backuploc" -a -d "$backuploc" ]; then - df -h "$backuploc" - previous="$backuploc" - fi + if [ -x "$(which mail 2>/dev/null)" ]; then + debug "send report to $reportemail" + hostname=`hostname` + [ $warnings == 0 ] || subject="WARNING" + [ $errors == 0 ] || subject="ERROR" + [ $fatals == 0 ] || subject="FAILED" + [ $halts == 0 ] || subject="HALTED" + + { + for ((i=0; i < ${#messages[@]} ; i++)); do + echo ${messages[$i]} done - fi - } | mail -s "backupninja: $hostname $subject" $reportemail + echo -e "$errormsg" + if [ "$reportspace" == "yes" ]; then + previous="" + for i in $(ls "$configdirectory"); do + backuploc=$(grep ^directory "$configdirectory"/"$i" | @AWK@ '{print $3}') + if [ "$backuploc" != "$previous" -a -n "$backuploc" -a -d "$backuploc" ]; then + df -h "$backuploc" + previous="$backuploc" + fi + done + fi + } | mail -s "backupninja: $hostname $subject" $reportemail + else + error "Unable to locate mail executable, email report not sent!" + let "errors += 1" + fi fi if [ $actions_run != 0 ]; then diff --git a/test/backupninja.bats b/test/backupninja.bats index 96eecb6af500e83bb0a0b68f7d40b3f3b9abc320..7d66150c52769f183f2dc2702703a15c45e94cf3 100644 --- a/test/backupninja.bats +++ b/test/backupninja.bats @@ -1,5 +1,9 @@ load common +teardown_backupninja() { + [ -x /usr/bin/mail.moved ] && mv /usr/bin/mail.moved /usr/bin/mail +} + create_test_action() { echo '#!/bin/sh' > "${BATS_TMPDIR}/backup.d/test.sh" echo "$1 $2" >> "${BATS_TMPDIR}/backup.d/test.sh" @@ -92,7 +96,7 @@ create_test_action() { } @test "reports: report is mailed when reportsuccess = yes" { - create_test_action + create_test_action info test_info setconfig backupninja.conf reportsuccess yes run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh" sleep 0.1 @@ -109,7 +113,7 @@ create_test_action() { } @test "reports: success report contains disk space info" { - create_test_action + create_test_action info test_info echo "directory = /" >> "${BATS_TMPDIR}/backup.d/test.sh" setconfig backupninja.conf reportsuccess yes setconfig backupninja.conf reportspace yes @@ -118,6 +122,15 @@ create_test_action() { grep -q "/dev/sda1" /var/mail/vagrant } +@test "reports: emits error if mail executable is not found" { + create_test_action info test_info + setconfig backupninja.conf reportsuccess yes + mv /usr/bin/mail /usr/bin/mail.moved + run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh" + [ "$status" -eq 1 ] + false +} + @test "scheduling: runs when = 'everyday at 01' and time matches" { create_test_action info test_info setconfig backupninja.conf when 'everyday at 01'