cddd3f3c7478892af58d05cb430b1a59244585fc

Author
zertrin <zrk951@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

Merge pull request #137 from zertrin/rework-logging

Big rework of logging handling. Add a --quiet option to suppress most output

Diff

This diff is truncated to protect this page.

  1diff --git a/duplicity-backup.sh b/duplicity-backup.sh
  2index e93e198f448c6131f628b3860a0ad538a8a1d5d9..1b4a1d54ae1e75046b424156cd624f261d0cf95a 100755
  3--- a/duplicity-backup.sh
  4+++ b/duplicity-backup.sh
  5@@ -71,9 +71,12 @@ echo "USAGE:
  6     --backup-script            automatically backup the script and secret key(s) to
  7                                the current working directory
  8 
  9+    -q, --quiet                silence most of output messages, except errors and output
 10+                               that is intended for interactive usage. Silenced output
 11+                               is still logged in the logfile.
 12+
 13     -n, --dry-run              perform a trial run with no changes made
 14     -d, --debug                echo duplicity commands to logfile
 15-
 16     -V, --version              print version information about this script and duplicity
 17 
 18   CURRENT SCRIPT VARIABLES:
 19@@ -83,7 +86,7 @@ echo "USAGE:
 20     EXCLIST (directories excluded)  = ${EXCLIST[*]:0}
 21     ROOT (root directory of backup) = ${ROOT}
 22     LOGFILE (log file path)         = ${LOGFILE}
 23-"
 24+" >&4
 25 }
 26 
 27 DUPLICITY="$(which duplicity)"
 28@@ -93,7 +96,6 @@ if [ ! -x "${DUPLICITY}" ]; then
 29   exit 1
 30 fi
 31 
 32-
 33 version(){
 34   # Read the version string from the file VERSION
 35   VERSION=$(<VERSION)
 36@@ -106,7 +108,7 @@ version(){
 37 # Some expensive argument parsing that allows the script to
 38 # be insensitive to the order of appearance of the options
 39 # and to handle correctly option parameters that are optional
 40-while getopts ":c:t:bfvelsndV-:" opt; do
 41+while getopts ":c:t:bfvelsqndV-:" opt; do
 42   case $opt in
 43     # parse long options (a bit tricky because builtin getopts does not
 44     # manage long options and I don't want to impose GNU getopt dependancy)
 45@@ -152,6 +154,9 @@ while getopts ":c:t:bfvelsndV-:" opt; do
 46             OPTIND=$(( OPTIND + 1 )) # we found it, move forward in arg parsing
 47           fi
 48         ;;
 49+        quiet)
 50+          QUIET=1
 51+        ;;
 52         dry-run)
 53           DRY_RUN="--dry-run "
 54         ;;
 55@@ -175,6 +180,7 @@ while getopts ":c:t:bfvelsndV-:" opt; do
 56     e) COMMAND="cleanup";;
 57     l) COMMAND="list-current-files";;
 58     s) COMMAND="collection-status";;
 59+    q) QUIET=1;;
 60     n) DRY_RUN="--dry-run ";; # dry run
 61     d) ECHO=$(which echo);; # debug
 62     V) version;;
 63@@ -188,8 +194,11 @@ while getopts ":c:t:bfvelsndV-:" opt; do
 64     ;;
 65   esac
 66 done
 67+#echo "Options parsed. COMMAND=${COMMAND}" # for debugging
 68+
 69+
 70+# ----------------  Read config file if specified -----------------
 71 
 72-# Read config file if specified
 73 if [ ! -z "${CONFIG}" ] && [ -f "${CONFIG}" ];
 74 then
 75   # shellcheck source=duplicity-backup.conf.example
 76@@ -200,6 +209,101 @@ else
 77   exit 1
 78 fi
 79 
 80+# ----------------------- Setup logging ---------------------------
 81+
 82+# Setup logging as soon as possible, in order to be able to perform i/o redirection
 83+
 84+[[ ${LOGDIR} = "/home/foobar_user_name/logs/test2/" ]] && config_sanity_fail "LOGDIR must be configured"
 85+
 86+# Ensure a trailing slash always exists in the log directory name
 87+LOGDIR="${LOGDIR%/}/"
 88+
 89+LOGFILE="${LOGDIR}${LOG_FILE}"
 90+
 91+if [ ! -d "${LOGDIR}" ]; then
 92+  echo "Attempting to create log directory ${LOGDIR} ..."
 93+  if ! mkdir -p "${LOGDIR}"; then
 94+    echo "Log directory ${LOGDIR} could not be created by this user: ${USER}" >&2
 95+    echo "Aborting..." >&2
 96+    exit 1
 97+  else
 98+    echo "Directory ${LOGDIR} successfully created."
 99+  fi
100+  echo "Attempting to change owner:group of ${LOGDIR} to ${LOG_FILE_OWNER} ..."
101+  if ! chown "${LOG_FILE_OWNER}" "${LOGDIR}"; then
102+    echo "User ${USER} could not change the owner:group of ${LOGDIR} to ${LOG_FILE_OWNER}" >&2
103+    echo "Aborting..." >&2
104+    exit 1