From 25c3a9f421d49ecb4463d94888cf5779edcd75d9 Mon Sep 17 00:00:00 2001
From: Jerome Charaoui <jerome@riseup.net>
Date: Sun, 24 Jan 2021 12:47:27 -0500
Subject: [PATCH] improve error handling around reporthost feature

---
 CHANGELOG.md       |  1 +
 src/backupninja.in | 20 ++++++++++++++++++--
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index e5b030a..48510f5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - [core] silence exit code message unless --debug is used
 - [core] backup halt should trigger email report if enabled
 - [core] wrap report email body to 1000 characters by default (DEBBUG-871793)
+- [core] improve error handling around reporthost feature
 
 ## [1.2.0] - 2021-01-21
 
diff --git a/src/backupninja.in b/src/backupninja.in
index 635d127..a8afc54 100755
--- a/src/backupninja.in
+++ b/src/backupninja.in
@@ -652,8 +652,24 @@ if [ $actions_run != 0 ]; then
 fi
 
 if [ -n "$reporthost" ]; then
-   debug "send $logfile to $reportuser@$reporthost:$reportdirectory"
-   rsync -qt $logfile $reportuser@$reporthost:$reportdirectory
+   if [ -z "$reportuser" ] || [ -z "$reportdirectory" ]; then
+      error "Unable to send report, reportuser and reportdirectory must be specified."
+      let "errors += 1"
+   elif [ ! -x "$(which rsync 2>/dev/null)" ]; then
+      error "Unable to locate rsync executable, report could not be sent to ${reporthost}."
+      let "errors += 1"
+   else
+      info "Sending $logfile to $reportuser@$reporthost:$reportdirectory"
+      execstr="rsync -qt $logfile $reportuser@$reporthost:$reportdirectory"
+      debug $execstr
+      output=$(eval $execstr 2>&1)
+      ret=$?
+      if [ $ret -ne 0 ]; then
+         error $output
+         error "An error was encountered attempting to send report to ${reporthost}."
+         let "errors += 1"
+      fi
+   fi
 fi
 
 # return exit code
-- 
GitLab