From 16fc04332246364cc11c041943a01f20c781c7e3 Mon Sep 17 00:00:00 2001 From: deajan Date: Thu, 17 Nov 2016 21:02:15 +0100 Subject: [PATCH] Rebuilt targets --- dev/debug_obackup.sh | 372 ++++++++++++++++--------------------------- obackup.sh | 362 ++++++++++++++++------------------------- 2 files changed, 271 insertions(+), 463 deletions(-) diff --git a/dev/debug_obackup.sh b/dev/debug_obackup.sh index 97ebd33..c2c0a99 100755 --- a/dev/debug_obackup.sh +++ b/dev/debug_obackup.sh @@ -9,21 +9,27 @@ PROGRAM="obackup" AUTHOR="(C) 2013-2016 by Orsiris de Jong" CONTACT="http://www.netpower.fr/obackup - ozy@netpower.fr" PROGRAM_VERSION=2.1-dev -PROGRAM_BUILD=2016111201 +PROGRAM_BUILD=2016111701 IS_STABLE=no #### MINIMAL-FUNCTION-SET BEGIN #### -## FUNC_BUILD=2016111002 +## FUNC_BUILD=2016111704 ## BEGIN Generic bash functions written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr ## To use in a program, define the following variables: ## PROGRAM=program-name ## INSTANCE_ID=program-instance-name ## _DEBUG=yes/no +## _LOGGER_LOGGER_SILENT=true/false +## _LOGGER_LOGGER_VERBOSE=true/false +## _LOGGER_ERR_ONLY=true/false +## _LOGGER_PREFIX="date"/"time"/"" -#TODO(high): Refactor GetRemoteOs into big oneliner for faster execution (if busybox else uname else uname -spio in one statement) -#TODO(high): Implement busybox support in SendEmail function +## Logger sets {ERROR|WARN}_ALERT variable when called with critical / error / warn loglevel +## When called from subprocesses, variable of main process can't be set. Status needs to be get via $RUN_DIR/$PROGRAM.Logger.{error|warn}.$SCRIPT_PID + +#TODO: Rewrite Logger so we can decide what to send to stdout, stderr and logfile #TODO: Windows checks, check sendmail & mailsend if ! type "$BASH" > /dev/null; then @@ -39,10 +45,10 @@ MAIL_ALERT_MSG="Execution of $PROGRAM instance $INSTANCE_ID on $(date) has warni # Environment variables that can be overriden by programs _DRYRUN=false -_SILENT=false -_VERBOSE=false +_LOGGER_SILENT=false +_LOGGER_VERBOSE=false +_LOGGER_ERR_ONLY=false _LOGGER_PREFIX="date" -_LOGGER_STDERR=false if [ "$KEEP_LOGGING" == "" ]; then KEEP_LOGGING=1801 fi @@ -51,9 +57,6 @@ fi ERROR_ALERT=false WARN_ALERT=false -# Log from current run -CURRENT_LOG="" - ## allow function call checks #__WITH_PARANOIA_DEBUG if [ "$_PARANOIA_DEBUG" == "yes" ];then #__WITH_PARANOIA_DEBUG _DEBUG=yes #__WITH_PARANOIA_DEBUG @@ -63,11 +66,13 @@ fi #__WITH_PARANOIA_DEBUG if [ ! "$_DEBUG" == "yes" ]; then _DEBUG=no SLEEP_TIME=.05 # Tested under linux and FreeBSD bash, #TODO tests on cygwin / msys - _VERBOSE=false + _LOGGER_VERBOSE=false else - SLEEP_TIME=1 + if [ "$SLEEP_TIME" == "" ]; then # Set SLEEP_TIME as environment variable when runinng with bash -x in order to avoid spamming console + SLEEP_TIME=.05 + fi trap 'TrapError ${LINENO} $?' ERR - _VERBOSE=true + _LOGGER_VERBOSE=true fi SCRIPT_PID=$$ @@ -114,28 +119,43 @@ function Dummy { # Sub function of Logger function _Logger { - local svalue="${1}" # What to log to stdout - local lvalue="${2:-$svalue}" # What to log to logfile, defaults to screen value - local evalue="${3}" # What to log to stderr + local logValue="${1}" # Log to file + local stdValue="${2}" # Log to screeen + local toStderr="${3:-false}" # Log to stderr instead of stdout - echo -e "$lvalue" >> "$LOG_FILE" - CURRENT_LOG="$CURRENT_LOG"$'\n'"$lvalue" + echo -e "$logValue" >> "$LOG_FILE" + # Current log file + echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" - if [ $_LOGGER_STDERR == true ] && [ "$evalue" != "" ]; then - cat <<< "$evalue" 1>&2 - elif [ "$_SILENT" == false ]; then - echo -e "$svalue" + if [ "$stdValue" != "" ] && [ "$_LOGGER_SILENT" != true ]; then + if [ $toStderr == true ]; then + # Force stderr color in subshell + (>&2 echo -e "$stdValue") + + else + echo -e "$stdValue" + fi fi } # General log function with log levels: -# CRITICAL, ERROR, WARN are colored in stdout, prefixed in stderr -# NOTICE is standard level -# VERBOSE is only sent to stdout / stderr if _VERBOSE=true -# DEBUG & PARANOIA_DEBUG are only sent if _DEBUG=yes + +# Environment variables +# _LOGGER_SILENT: Disables any output to stdout & stderr +# _LOGGER_STD_ERR: Disables any output to stdout except for ALWAYS loglevel +# _LOGGER_VERBOSE: Allows VERBOSE loglevel messages to be sent to stdout + +# Loglevels +# Except for VERBOSE, all loglevels are ALWAYS sent to log file + +# CRITICAL, ERROR, WARN sent to stderr, color depending on level, level also logged +# NOTICE sent to stdout +# VERBOSE sent to stdout if _LOGGER_VERBOSE = true +# ALWAYS is sent to stdout unless _LOGGER_SILENT = true +# DEBUG & PARANOIA_DEBUG are only sent to stdout if _DEBUG=yes function Logger { local value="${1}" # Sentence to log (in double quotes) - local level="${2}" # Log level: PARANOIA_DEBUG, DEBUG, VERBOSE, NOTICE, WARN, ERROR, CRITIAL + local level="${2}" # Log level if [ "$_LOGGER_PREFIX" == "time" ]; then prefix="TIME: $SECONDS - " @@ -146,35 +166,44 @@ function Logger { fi if [ "$level" == "CRITICAL" ]; then - _Logger "$prefix\e[41m$value\e[0m" "$prefix$level:$value" "$level:$value" + _Logger "$prefix($level):$value" "$prefix\e[41m$value\e[0m" true ERROR_ALERT=true + # ERROR_ALERT / WARN_ALERT isn't set in main when Logger is called from a subprocess. Need to keep this flag. + echo "1" > "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.error.$SCRIPT_PID" return elif [ "$level" == "ERROR" ]; then - _Logger "$prefix\e[91m$value\e[0m" "$prefix$level:$value" "$level:$value" + _Logger "$prefix($level):$value" "$prefix\e[91m$value\e[0m" true ERROR_ALERT=true + echo "1" > "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.error.$SCRIPT_PID" return elif [ "$level" == "WARN" ]; then - _Logger "$prefix\e[93m$value\e[0m" "$prefix$level:$value" "$level:$value" + _Logger "$prefix($level):$value" "$prefix\e[33m$value\e[0m" true WARN_ALERT=true + echo "1" > "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.warn.$SCRIPT_PID" return elif [ "$level" == "NOTICE" ]; then - _Logger "$prefix$value" + if [ "$_LOGGER_ERR_ONLY" != true ]; then + _Logger "$prefix$value" "$prefix$value" + fi return elif [ "$level" == "VERBOSE" ]; then - if [ $_VERBOSE == true ]; then - _Logger "$prefix$value" + if [ $_LOGGER_VERBOSE == true ]; then + _Logger "$prefix:$value" "$prefix$value" fi return + elif [ "$level" == "ALWAYS" ]; then + _Logger "$prefix$value" "$prefix$value" + return elif [ "$level" == "DEBUG" ]; then if [ "$_DEBUG" == "yes" ]; then - _Logger "$prefix$value" + _Logger "$prefix$value" "$prefix$value" return fi - elif [ "$level" == "PARANOIA_DEBUG" ]; then #__WITH_PARANOIA_DEBUG - if [ "$_PARANOIA_DEBUG" == "yes" ]; then #__WITH_PARANOIA_DEBUG - _Logger "$prefix$value" #__WITH_PARANOIA_DEBUG - return #__WITH_PARANOIA_DEBUG - fi #__WITH_PARANOIA_DEBUG + elif [ "$level" == "PARANOIA_DEBUG" ]; then #__WITH_PARANOIA_DEBUG + if [ "$_PARANOIA_DEBUG" == "yes" ]; then #__WITH_PARANOIA_DEBUG + _Logger "$prefix$value" "$prefix$value" #__WITH_PARANOIA_DEBUG + return #__WITH_PARANOIA_DEBUG + fi #__WITH_PARANOIA_DEBUG else _Logger "\e[41mLogger function called without proper loglevel [$level].\e[0m" _Logger "Value was: $prefix$value" @@ -201,7 +230,7 @@ function QuickLogger { __CheckArguments 1 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG - if [ $_SILENT == true ]; then + if [ $_LOGGER_SILENT == true ]; then _QuickLogger "$value" "log" else _QuickLogger "$value" "stdout" @@ -264,14 +293,10 @@ function SendAlert { __CheckArguments 0-1 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG - local mail_no_attachment= - local attachment_command= - local subject= - local body= - - # Windows specific settings - local encryption_string= - local auth_string= + local attachment + local attachmentFile + local subject + local body if [ "$DESTINATION_MAILS" == "" ]; then return 0 @@ -292,11 +317,14 @@ function SendAlert { eval "cat \"$LOG_FILE\" $COMPRESSION_PROGRAM > $ALERT_LOG_FILE" if [ $? != 0 ]; then Logger "Cannot create [$ALERT_LOG_FILE]" "WARN" - mail_no_attachment=1 + attachment=false else - mail_no_attachment=0 + attachment=true fi - body="$MAIL_ALERT_MSG"$'\n\n'"$CURRENT_LOG" + if [ -e "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID" ]; then + body="$MAIL_ALERT_MSG"$'\n\n'"$(cat $RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID)" + fi + exit if [ $ERROR_ALERT == true ]; then subject="Error alert for $INSTANCE_ID" @@ -312,133 +340,17 @@ function SendAlert { subject="Fnished run - $subject" fi - if [ "$mail_no_attachment" -eq 0 ]; then - attachment_command="-a $ALERT_LOG_FILE" + if [ "$attachment" == true ]; then + attachmentFile="$ALERT_LOG_FILE" fi - if [ "$LOCAL_OS" == "BUSYBOX" ]; then - if type sendmail > /dev/null 2>&1; then - if [ "$ENCRYPTION" == "tls" ]; then - echo -e "Subject:$subject\r\n$body" | $(type -p sendmail) -f "$SENDER_MAIL" -H "exec openssl s_client -quiet -tls1_2 -starttls smtp -connect $SMTP_SERVER:$SMTP_PORT" -au"$SMTP_USER" -ap"$SMTP_PASSWORD" $DESTINATION_MAILS - elif [ "$ENCRYPTION" == "ssl" ]; then - echo -e "Subject:$subject\r\n$body" | $(type -p sendmail) -f "$SENDER_MAIL" -H "exec openssl s_client -quiet -connect $SMTP_SERVER:$SMTP_PORT" -au"$SMTP_USER" -ap"$SMTP_PASSWORD" $DESTINATION_MAILS - else - echo -e "Subject:$subject\r\n$body" | $(type -p sendmail) -f "$SENDER_MAIL" -S "$SMTP_SERVER:$SMTP_PORT" -au"$SMTP_USER" -ap"$SMTP_PASSWORD" $DESTINATION_MAILS - fi - - if [ $? != 0 ]; then - Logger "Cannot send alert mail via $(type -p sendmail) !!!" "WARN" - # Don't bother try other mail systems with busybox - return 1 - else - return 0 - fi - else - Logger "Sendmail not present. Won't send any mail" "WARN" - return 1 - fi - fi - - if type mutt > /dev/null 2>&1 ; then - echo "$body" | $(type -p mutt) -x -s "$subject" $DESTINATION_MAILS $attachment_command - if [ $? != 0 ]; then - Logger "Cannot send alert mail via $(type -p mutt) !!!" "WARN" - else - Logger "Sent alert mail using mutt." "NOTICE" - return 0 - fi - fi - - if type mail > /dev/null 2>&1 ; then - if [ "$mail_no_attachment" -eq 0 ] && $(type -p mail) -V | grep "GNU" > /dev/null; then - attachment_command="-A $ALERT_LOG_FILE" - elif [ "$mail_no_attachment" -eq 0 ] && $(type -p mail) -V > /dev/null; then - attachment_command="-a$ALERT_LOG_FILE" - else - attachment_command="" - fi - echo "$body" | $(type -p mail) $attachment_command -s "$subject" $DESTINATION_MAILS - if [ $? != 0 ]; then - Logger "Cannot send alert mail via $(type -p mail) with attachments !!!" "WARN" - echo "$body" | $(type -p mail) -s "$subject" $DESTINATION_MAILS - if [ $? != 0 ]; then - Logger "Cannot send alert mail via $(type -p mail) without attachments !!!" "WARN" - else - Logger "Sent alert mail using mail command without attachment." "NOTICE" - return 0 - fi - else - Logger "Sent alert mail using mail command." "NOTICE" - return 0 - fi - fi - - if type sendmail > /dev/null 2>&1 ; then - echo -e "Subject:$subject\r\n$body" | $(type -p sendmail) $DESTINATION_MAILS - if [ $? != 0 ]; then - Logger "Cannot send alert mail via $(type -p sendmail) !!!" "WARN" - else - Logger "Sent alert mail using sendmail command without attachment." "NOTICE" - return 0 - fi - fi - - # Windows specific - if type "mailsend.exe" > /dev/null 2>&1 ; then - - if [ "$SMTP_ENCRYPTION" != "tls" ] && [ "$SMTP_ENCRYPTION" != "ssl" ] && [ "$SMTP_ENCRYPTION" != "none" ]; then - Logger "Bogus smtp encryption, assuming none." "WARN" - encryption_string= - elif [ "$SMTP_ENCRYPTION" == "tls" ]; then - encryption_string=-starttls - elif [ "$SMTP_ENCRYPTION" == "ssl" ]:; then - encryption_string=-ssl - fi - if [ "$SMTP_USER" != "" ] && [ "$SMTP_USER" != "" ]; then - auth_string="-auth -user \"$SMTP_USER\" -pass \"$SMTP_PASSWORD\"" - fi - $(type mailsend.exe) -f $SENDER_MAIL -t "$DESTINATION_MAILS" -sub "$subject" -M "$body" -attach "$attachment" -smtp "$SMTP_SERVER" -port "$SMTP_PORT" $encryption_string $auth_string - if [ $? != 0 ]; then - Logger "Cannot send mail via $(type mailsend.exe) !!!" "WARN" - else - Logger "Sent mail using mailsend.exe command with attachment." "NOTICE" - return 0 - fi - fi - - # Windows specific, kept for compatibility (sendemail from http://caspian.dotconf.net/menu/Software/SendEmail/) - if type sendemail > /dev/null 2>&1 ; then - if [ "$SMTP_USER" != "" ] && [ "$SMTP_PASSWORD" != "" ]; then - SMTP_OPTIONS="-xu $SMTP_USER -xp $SMTP_PASSWORD" - else - SMTP_OPTIONS="" - fi - $(type -p sendemail) -f $SENDER_MAIL -t "$DESTINATION_MAILS" -u "$subject" -m "$body" -s $SMTP_SERVER $SMTP_OPTIONS > /dev/null 2>&1 - if [ $? != 0 ]; then - Logger "Cannot send alert mail via $(type -p sendemail) !!!" "WARN" - else - Logger "Sent alert mail using sendemail command without attachment." "NOTICE" - return 0 - fi - fi - - # pfSense specific - if [ -f /usr/local/bin/mail.php ]; then - echo "$body" | /usr/local/bin/mail.php -s="$subject" - if [ $? != 0 ]; then - Logger "Cannot send alert mail via /usr/local/bin/mail.php (pfsense) !!!" "WARN" - else - Logger "Sent alert mail using pfSense mail.php." "NOTICE" - return 0 - fi - fi - - # If function has not returned 0 yet, assume it is critical that no alert can be sent - Logger "Cannot send alert (neither mutt, mail, sendmail, mailsend, sendemail or pfSense mail.php could be used)." "ERROR" # Is not marked critical because execution must continue + SendEmail "$subject" "$body" "$DESTINATION_MAILS" "$attachmentFile" "$SENDER_MAIL" "$SMTP_SERVER" "$SMTP_PORT" "$ENCRYPTION" "SMTP_USER" "$SMTP_PASSWORD" # Delete tmp log file - if [ -f "$ALERT_LOG_FILE" ]; then - rm -f "$ALERT_LOG_FILE" + if [ "$attachment" == true ]; then + if [ -f "$ALERT_LOG_FILE" ]; then + rm -f "$ALERT_LOG_FILE" + fi fi } @@ -600,8 +512,8 @@ function TrapError { local line="$1" local code="${2:-1}" - if [ $_SILENT == false ]; then - echo -e " /!\ ERROR in ${job}: Near line ${line}, exit code ${code}" + if [ $_LOGGER_SILENT == false ]; then + echo -e "\e[45m/!\ ERROR in ${job}: Near line ${line}, exit code ${code}\e[0m" fi } @@ -627,7 +539,7 @@ function LoadConfigFile { } function Spinner { - if [ $_SILENT == true ]; then + if [ $_LOGGER_SILENT == true ] || [ "$_LOGGER_ERR_ONLY" == true ]; then return 0 fi @@ -937,10 +849,10 @@ function IsNumericExpand { local re="^-?[0-9]+([.][0-9]+)?$" - if [[ $value =~ $re ]]; then - echo 1 && return 1 + if [[ $value =~ ^-?[0-9]+([.][0-9]+)?$ ]]; then + echo 1 else - echo 0 && return 0 + echo 0 fi } @@ -949,9 +861,9 @@ function IsNumeric { local value="${1}" if [[ $value =~ ^[0-9]+([.][0-9]+)?$ ]]; then - echo 1 && return 1 + echo 1 else - echo 0 && return 0 + echo 0 fi } @@ -959,9 +871,9 @@ function IsInteger { local value="${1}" if [[ $value =~ ^[0-9]+$ ]]; then - echo 1 && return 1 + echo 1 else - echo 0 && return 0 + echo 0 fi } @@ -1041,8 +953,7 @@ function GetLocalOS { local localOsVar # There's no good way to tell if currently running in BusyBox shell. Using sluggish way. - ls --help 2>&1 | grep -i BusyBox > /dev/null - if [ $? == 0 ]; then + if ls --help 2>&1 | grep -i "BusyBox" > /dev/null; then localOsVar="BusyBox" else localOsVar="$(uname -spio 2>&1)" @@ -1087,47 +998,35 @@ function GetLocalOS { function GetRemoteOS { __CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG - local retval - local cmd local remoteOsVar - if [ "$REMOTE_OPERATION" == "yes" ]; then - CheckConnectivity3rdPartyHosts - CheckConnectivityRemoteHost +$SSH_CMD bash -s << 'ENDSSH' >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" 2>&1 - cmd=$SSH_CMD' "ls --help 2>&1 | grep -i BusyBox > /dev/null"' - Logger "cmd: $cmd" "DEBUG" - eval "$cmd" & - WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-0" true $KEEP_LOGGING - retval=$? - if [ $retval == 0 ]; then - remoteOsVar="BusyBox" - else - cmd=$SSH_CMD' "uname -spio" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1' - Logger "cmd: $cmd" "DEBUG" - eval "$cmd" & - WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-1" true $KEEP_LOGGING - retval=$? - if [ $retval != 0 ]; then - cmd=$SSH_CMD' "uname -v" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1' - Logger "cmd: $cmd" "DEBUG" - eval "$cmd" & - WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-2" true $KEEP_LOGGING - retval=$? - if [ $retval != 0 ]; then - cmd=$SSH_CMD' "uname" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1' - Logger "cmd: $cmd" "DEBUG" - eval "$cmd" & - WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-3" true $KEEP_LOGGING - retval=$? - if [ $retval != 0 ]; then - Logger "Cannot Get remote OS type." "ERROR" - fi - fi +function GetOs { + local localOsVar + + # There's no good way to tell if currently running in BusyBox shell. Using sluggish way. + if ls --help 2>&1 | grep -i "BusyBox" > /dev/null; then + localOsVar="BusyBox" + else + localOsVar="$(uname -spio 2>&1)" + if [ $? != 0 ]; then + localOsVar="$(uname -v 2>&1)" + if [ $? != 0 ]; then + localOsVar="$(uname)" fi - remoteOsVar=$(cat "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID") fi + fi + echo "$localOsVar" +} + +GetOs + +ENDSSH + + if [ -f "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" ]; then + remoteOsVar=$(cat "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID") case $remoteOsVar in *"Linux"*) REMOTE_OS="Linux" @@ -1149,7 +1048,7 @@ function GetRemoteOS { exit 1 ;; *) - if [ "$IGNORE_OS_TYPE" == "yes" ]; then #DOC: Undocumented option + if [ "$IGNORE_OS_TYPE" == "yes" ]; then #DOC: Undocumented debug only setting Logger "Running on unknown remote OS [$remoteOsVar]." "WARN" return fi @@ -1157,8 +1056,9 @@ function GetRemoteOS { Logger "Remote OS said:\n$remoteOsVar" "CRITICAL" exit 1 esac - Logger "Remote OS: [$remoteOsVar]." "DEBUG" + else + Logger "Cannot get Remote OS" "CRITICAL" fi } @@ -1182,7 +1082,7 @@ function RunLocalCommand { Logger "Command failed." "ERROR" fi - if [ $_VERBOSE == true ] || [ $retval -ne 0 ]; then + if [ $_LOGGER_VERBOSE == true ] || [ $retval -ne 0 ]; then Logger "Command output:\n$(cat $RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID)" "NOTICE" fi @@ -1217,7 +1117,7 @@ function RunRemoteCommand { Logger "Command failed." "ERROR" fi - if [ -f "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" ] && ([ $_VERBOSE == true ] || [ $retval -ne 0 ]) + if [ -f "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" ] && ([ $_LOGGER_VERBOSE == true ] || [ $retval -ne 0 ]) then Logger "Command output:\n$(cat $RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID)" "NOTICE" fi @@ -1492,7 +1392,7 @@ function PreInit { RSYNC_ARGS="-rltD" if [ "$_DRYRUN" == true ]; then RSYNC_DRY_ARG="-n" - DRY_WARNING="/!\ DRY RUN" + DRY_WARNING="/!\ DRY RUN " else RSYNC_DRY_ARG="" fi @@ -1725,22 +1625,22 @@ function TrapQuit { if [ "$RUN_AFTER_CMD_ON_ERROR" == "yes" ]; then RunAfterHook fi - CleanUp Logger "$PROGRAM finished with errors." "ERROR" SendAlert + CleanUp exitcode=1 elif [ $WARN_ALERT == true ]; then if [ "$RUN_AFTER_CMD_ON_ERROR" == "yes" ]; then RunAfterHook fi - CleanUp Logger "$PROGRAM finished with warnings." "WARN" SendAlert + CleanUp exitcode=2 else RunAfterHook - CleanUp Logger "$PROGRAM finshed without errors." "NOTICE" + CleanUp exitcode=0 fi @@ -3175,7 +3075,7 @@ function Init { ## Add update to default RSYNC_ARGS RSYNC_ARGS=$RSYNC_ARGS" -u" - if [ $_VERBOSE == true ]; then + if [ $_LOGGER_VERBOSE == true ]; then RSYNC_ARGS=$RSYNC_ARGS" -i" fi @@ -3253,6 +3153,7 @@ function Usage { echo "OPTIONS:" echo "--dry will run obackup without actually doing anything, just testing" echo "--silent will run obackup without any output to stdout, usefull for cron backups" + echo "--errors-only Output only errors (can be combined with silent or verbose)" echo "--verbose adds command outputs" echo "--stats Adds rsync transfer statistics to verbose output" echo "--partial Allows rsync to keep partial downloads that can be resumed later (experimental)" @@ -3272,7 +3173,7 @@ function Usage { # Command line argument flags _DRYRUN=false -_SILENT=false +_LOGGER_SILENT=false no_maxtime=false stats=false PARTIAL=no @@ -3291,10 +3192,10 @@ function GetCommandlineArguments { _DRYRUN=true ;; --silent) - _SILENT=true + _LOGGER_SILENT=true ;; --verbose) - _VERBOSE=true + _LOGGER_VERBOSE=true ;; --stats) stats=false @@ -3334,6 +3235,9 @@ function GetCommandlineArguments { --recipient=*) GPG_RECIPIENT="${i##*=}" ;; + --errors-only) + _LOGGER_ERR_ONLY=true + ;; esac done } @@ -3377,7 +3281,7 @@ fi DATE=$(date) Logger "--------------------------------------------------------------------" "NOTICE" -Logger "$DRY_WARNING $DATE - $PROGRAM v$PROGRAM_VERSION $BACKUP_TYPE script begin." "NOTICE" +Logger "$DRY_WARNING$DATE - $PROGRAM v$PROGRAM_VERSION $BACKUP_TYPE script begin." "NOTICE" Logger "--------------------------------------------------------------------" "NOTICE" Logger "Backup instance [$INSTANCE_ID] launched as $LOCAL_USER@$LOCAL_HOST (PID $SCRIPT_PID)" "NOTICE" diff --git a/obackup.sh b/obackup.sh index 73c7a61..e28382b 100755 --- a/obackup.sh +++ b/obackup.sh @@ -9,21 +9,27 @@ PROGRAM="obackup" AUTHOR="(C) 2013-2016 by Orsiris de Jong" CONTACT="http://www.netpower.fr/obackup - ozy@netpower.fr" PROGRAM_VERSION=2.1-dev -PROGRAM_BUILD=2016111201 +PROGRAM_BUILD=2016111701 IS_STABLE=no #### MINIMAL-FUNCTION-SET BEGIN #### -## FUNC_BUILD=2016111002 +## FUNC_BUILD=2016111704 ## BEGIN Generic bash functions written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr ## To use in a program, define the following variables: ## PROGRAM=program-name ## INSTANCE_ID=program-instance-name ## _DEBUG=yes/no +## _LOGGER_LOGGER_SILENT=true/false +## _LOGGER_LOGGER_VERBOSE=true/false +## _LOGGER_ERR_ONLY=true/false +## _LOGGER_PREFIX="date"/"time"/"" -#TODO(high): Refactor GetRemoteOs into big oneliner for faster execution (if busybox else uname else uname -spio in one statement) -#TODO(high): Implement busybox support in SendEmail function +## Logger sets {ERROR|WARN}_ALERT variable when called with critical / error / warn loglevel +## When called from subprocesses, variable of main process can't be set. Status needs to be get via $RUN_DIR/$PROGRAM.Logger.{error|warn}.$SCRIPT_PID + +#TODO: Rewrite Logger so we can decide what to send to stdout, stderr and logfile #TODO: Windows checks, check sendmail & mailsend if ! type "$BASH" > /dev/null; then @@ -39,10 +45,10 @@ MAIL_ALERT_MSG="Execution of $PROGRAM instance $INSTANCE_ID on $(date) has warni # Environment variables that can be overriden by programs _DRYRUN=false -_SILENT=false -_VERBOSE=false +_LOGGER_SILENT=false +_LOGGER_VERBOSE=false +_LOGGER_ERR_ONLY=false _LOGGER_PREFIX="date" -_LOGGER_STDERR=false if [ "$KEEP_LOGGING" == "" ]; then KEEP_LOGGING=1801 fi @@ -51,19 +57,18 @@ fi ERROR_ALERT=false WARN_ALERT=false -# Log from current run -CURRENT_LOG="" - ## allow debugging from command line with _DEBUG=yes if [ ! "$_DEBUG" == "yes" ]; then _DEBUG=no SLEEP_TIME=.05 # Tested under linux and FreeBSD bash, #TODO tests on cygwin / msys - _VERBOSE=false + _LOGGER_VERBOSE=false else - SLEEP_TIME=1 + if [ "$SLEEP_TIME" == "" ]; then # Set SLEEP_TIME as environment variable when runinng with bash -x in order to avoid spamming console + SLEEP_TIME=.05 + fi trap 'TrapError ${LINENO} $?' ERR - _VERBOSE=true + _LOGGER_VERBOSE=true fi SCRIPT_PID=$$ @@ -109,28 +114,43 @@ function Dummy { # Sub function of Logger function _Logger { - local svalue="${1}" # What to log to stdout - local lvalue="${2:-$svalue}" # What to log to logfile, defaults to screen value - local evalue="${3}" # What to log to stderr + local logValue="${1}" # Log to file + local stdValue="${2}" # Log to screeen + local toStderr="${3:-false}" # Log to stderr instead of stdout - echo -e "$lvalue" >> "$LOG_FILE" - CURRENT_LOG="$CURRENT_LOG"$'\n'"$lvalue" + echo -e "$logValue" >> "$LOG_FILE" + # Current log file + echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" - if [ $_LOGGER_STDERR == true ] && [ "$evalue" != "" ]; then - cat <<< "$evalue" 1>&2 - elif [ "$_SILENT" == false ]; then - echo -e "$svalue" + if [ "$stdValue" != "" ] && [ "$_LOGGER_SILENT" != true ]; then + if [ $toStderr == true ]; then + # Force stderr color in subshell + (>&2 echo -e "$stdValue") + + else + echo -e "$stdValue" + fi fi } # General log function with log levels: -# CRITICAL, ERROR, WARN are colored in stdout, prefixed in stderr -# NOTICE is standard level -# VERBOSE is only sent to stdout / stderr if _VERBOSE=true -# DEBUG & PARANOIA_DEBUG are only sent if _DEBUG=yes + +# Environment variables +# _LOGGER_SILENT: Disables any output to stdout & stderr +# _LOGGER_STD_ERR: Disables any output to stdout except for ALWAYS loglevel +# _LOGGER_VERBOSE: Allows VERBOSE loglevel messages to be sent to stdout + +# Loglevels +# Except for VERBOSE, all loglevels are ALWAYS sent to log file + +# CRITICAL, ERROR, WARN sent to stderr, color depending on level, level also logged +# NOTICE sent to stdout +# VERBOSE sent to stdout if _LOGGER_VERBOSE = true +# ALWAYS is sent to stdout unless _LOGGER_SILENT = true +# DEBUG & PARANOIA_DEBUG are only sent to stdout if _DEBUG=yes function Logger { local value="${1}" # Sentence to log (in double quotes) - local level="${2}" # Log level: PARANOIA_DEBUG, DEBUG, VERBOSE, NOTICE, WARN, ERROR, CRITIAL + local level="${2}" # Log level if [ "$_LOGGER_PREFIX" == "time" ]; then prefix="TIME: $SECONDS - " @@ -141,28 +161,37 @@ function Logger { fi if [ "$level" == "CRITICAL" ]; then - _Logger "$prefix\e[41m$value\e[0m" "$prefix$level:$value" "$level:$value" + _Logger "$prefix($level):$value" "$prefix\e[41m$value\e[0m" true ERROR_ALERT=true + # ERROR_ALERT / WARN_ALERT isn't set in main when Logger is called from a subprocess. Need to keep this flag. + echo "1" > "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.error.$SCRIPT_PID" return elif [ "$level" == "ERROR" ]; then - _Logger "$prefix\e[91m$value\e[0m" "$prefix$level:$value" "$level:$value" + _Logger "$prefix($level):$value" "$prefix\e[91m$value\e[0m" true ERROR_ALERT=true + echo "1" > "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.error.$SCRIPT_PID" return elif [ "$level" == "WARN" ]; then - _Logger "$prefix\e[93m$value\e[0m" "$prefix$level:$value" "$level:$value" + _Logger "$prefix($level):$value" "$prefix\e[33m$value\e[0m" true WARN_ALERT=true + echo "1" > "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.warn.$SCRIPT_PID" return elif [ "$level" == "NOTICE" ]; then - _Logger "$prefix$value" + if [ "$_LOGGER_ERR_ONLY" != true ]; then + _Logger "$prefix$value" "$prefix$value" + fi return elif [ "$level" == "VERBOSE" ]; then - if [ $_VERBOSE == true ]; then - _Logger "$prefix$value" + if [ $_LOGGER_VERBOSE == true ]; then + _Logger "$prefix:$value" "$prefix$value" fi return + elif [ "$level" == "ALWAYS" ]; then + _Logger "$prefix$value" "$prefix$value" + return elif [ "$level" == "DEBUG" ]; then if [ "$_DEBUG" == "yes" ]; then - _Logger "$prefix$value" + _Logger "$prefix$value" "$prefix$value" return fi else @@ -189,7 +218,7 @@ function QuickLogger { local value="${1}" - if [ $_SILENT == true ]; then + if [ $_LOGGER_SILENT == true ]; then _QuickLogger "$value" "log" else _QuickLogger "$value" "stdout" @@ -249,14 +278,10 @@ function SendAlert { local runAlert="${1:-false}" # Specifies if current message is sent while running or at the end of a run - local mail_no_attachment= - local attachment_command= - local subject= - local body= - - # Windows specific settings - local encryption_string= - local auth_string= + local attachment + local attachmentFile + local subject + local body if [ "$DESTINATION_MAILS" == "" ]; then return 0 @@ -277,11 +302,14 @@ function SendAlert { eval "cat \"$LOG_FILE\" $COMPRESSION_PROGRAM > $ALERT_LOG_FILE" if [ $? != 0 ]; then Logger "Cannot create [$ALERT_LOG_FILE]" "WARN" - mail_no_attachment=1 + attachment=false else - mail_no_attachment=0 + attachment=true fi - body="$MAIL_ALERT_MSG"$'\n\n'"$CURRENT_LOG" + if [ -e "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID" ]; then + body="$MAIL_ALERT_MSG"$'\n\n'"$(cat $RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID)" + fi + exit if [ $ERROR_ALERT == true ]; then subject="Error alert for $INSTANCE_ID" @@ -297,133 +325,17 @@ function SendAlert { subject="Fnished run - $subject" fi - if [ "$mail_no_attachment" -eq 0 ]; then - attachment_command="-a $ALERT_LOG_FILE" + if [ "$attachment" == true ]; then + attachmentFile="$ALERT_LOG_FILE" fi - if [ "$LOCAL_OS" == "BUSYBOX" ]; then - if type sendmail > /dev/null 2>&1; then - if [ "$ENCRYPTION" == "tls" ]; then - echo -e "Subject:$subject\r\n$body" | $(type -p sendmail) -f "$SENDER_MAIL" -H "exec openssl s_client -quiet -tls1_2 -starttls smtp -connect $SMTP_SERVER:$SMTP_PORT" -au"$SMTP_USER" -ap"$SMTP_PASSWORD" $DESTINATION_MAILS - elif [ "$ENCRYPTION" == "ssl" ]; then - echo -e "Subject:$subject\r\n$body" | $(type -p sendmail) -f "$SENDER_MAIL" -H "exec openssl s_client -quiet -connect $SMTP_SERVER:$SMTP_PORT" -au"$SMTP_USER" -ap"$SMTP_PASSWORD" $DESTINATION_MAILS - else - echo -e "Subject:$subject\r\n$body" | $(type -p sendmail) -f "$SENDER_MAIL" -S "$SMTP_SERVER:$SMTP_PORT" -au"$SMTP_USER" -ap"$SMTP_PASSWORD" $DESTINATION_MAILS - fi - - if [ $? != 0 ]; then - Logger "Cannot send alert mail via $(type -p sendmail) !!!" "WARN" - # Don't bother try other mail systems with busybox - return 1 - else - return 0 - fi - else - Logger "Sendmail not present. Won't send any mail" "WARN" - return 1 - fi - fi - - if type mutt > /dev/null 2>&1 ; then - echo "$body" | $(type -p mutt) -x -s "$subject" $DESTINATION_MAILS $attachment_command - if [ $? != 0 ]; then - Logger "Cannot send alert mail via $(type -p mutt) !!!" "WARN" - else - Logger "Sent alert mail using mutt." "NOTICE" - return 0 - fi - fi - - if type mail > /dev/null 2>&1 ; then - if [ "$mail_no_attachment" -eq 0 ] && $(type -p mail) -V | grep "GNU" > /dev/null; then - attachment_command="-A $ALERT_LOG_FILE" - elif [ "$mail_no_attachment" -eq 0 ] && $(type -p mail) -V > /dev/null; then - attachment_command="-a$ALERT_LOG_FILE" - else - attachment_command="" - fi - echo "$body" | $(type -p mail) $attachment_command -s "$subject" $DESTINATION_MAILS - if [ $? != 0 ]; then - Logger "Cannot send alert mail via $(type -p mail) with attachments !!!" "WARN" - echo "$body" | $(type -p mail) -s "$subject" $DESTINATION_MAILS - if [ $? != 0 ]; then - Logger "Cannot send alert mail via $(type -p mail) without attachments !!!" "WARN" - else - Logger "Sent alert mail using mail command without attachment." "NOTICE" - return 0 - fi - else - Logger "Sent alert mail using mail command." "NOTICE" - return 0 - fi - fi - - if type sendmail > /dev/null 2>&1 ; then - echo -e "Subject:$subject\r\n$body" | $(type -p sendmail) $DESTINATION_MAILS - if [ $? != 0 ]; then - Logger "Cannot send alert mail via $(type -p sendmail) !!!" "WARN" - else - Logger "Sent alert mail using sendmail command without attachment." "NOTICE" - return 0 - fi - fi - - # Windows specific - if type "mailsend.exe" > /dev/null 2>&1 ; then - - if [ "$SMTP_ENCRYPTION" != "tls" ] && [ "$SMTP_ENCRYPTION" != "ssl" ] && [ "$SMTP_ENCRYPTION" != "none" ]; then - Logger "Bogus smtp encryption, assuming none." "WARN" - encryption_string= - elif [ "$SMTP_ENCRYPTION" == "tls" ]; then - encryption_string=-starttls - elif [ "$SMTP_ENCRYPTION" == "ssl" ]:; then - encryption_string=-ssl - fi - if [ "$SMTP_USER" != "" ] && [ "$SMTP_USER" != "" ]; then - auth_string="-auth -user \"$SMTP_USER\" -pass \"$SMTP_PASSWORD\"" - fi - $(type mailsend.exe) -f $SENDER_MAIL -t "$DESTINATION_MAILS" -sub "$subject" -M "$body" -attach "$attachment" -smtp "$SMTP_SERVER" -port "$SMTP_PORT" $encryption_string $auth_string - if [ $? != 0 ]; then - Logger "Cannot send mail via $(type mailsend.exe) !!!" "WARN" - else - Logger "Sent mail using mailsend.exe command with attachment." "NOTICE" - return 0 - fi - fi - - # Windows specific, kept for compatibility (sendemail from http://caspian.dotconf.net/menu/Software/SendEmail/) - if type sendemail > /dev/null 2>&1 ; then - if [ "$SMTP_USER" != "" ] && [ "$SMTP_PASSWORD" != "" ]; then - SMTP_OPTIONS="-xu $SMTP_USER -xp $SMTP_PASSWORD" - else - SMTP_OPTIONS="" - fi - $(type -p sendemail) -f $SENDER_MAIL -t "$DESTINATION_MAILS" -u "$subject" -m "$body" -s $SMTP_SERVER $SMTP_OPTIONS > /dev/null 2>&1 - if [ $? != 0 ]; then - Logger "Cannot send alert mail via $(type -p sendemail) !!!" "WARN" - else - Logger "Sent alert mail using sendemail command without attachment." "NOTICE" - return 0 - fi - fi - - # pfSense specific - if [ -f /usr/local/bin/mail.php ]; then - echo "$body" | /usr/local/bin/mail.php -s="$subject" - if [ $? != 0 ]; then - Logger "Cannot send alert mail via /usr/local/bin/mail.php (pfsense) !!!" "WARN" - else - Logger "Sent alert mail using pfSense mail.php." "NOTICE" - return 0 - fi - fi - - # If function has not returned 0 yet, assume it is critical that no alert can be sent - Logger "Cannot send alert (neither mutt, mail, sendmail, mailsend, sendemail or pfSense mail.php could be used)." "ERROR" # Is not marked critical because execution must continue + SendEmail "$subject" "$body" "$DESTINATION_MAILS" "$attachmentFile" "$SENDER_MAIL" "$SMTP_SERVER" "$SMTP_PORT" "$ENCRYPTION" "SMTP_USER" "$SMTP_PASSWORD" # Delete tmp log file - if [ -f "$ALERT_LOG_FILE" ]; then - rm -f "$ALERT_LOG_FILE" + if [ "$attachment" == true ]; then + if [ -f "$ALERT_LOG_FILE" ]; then + rm -f "$ALERT_LOG_FILE" + fi fi } @@ -584,8 +496,8 @@ function TrapError { local line="$1" local code="${2:-1}" - if [ $_SILENT == false ]; then - echo -e " /!\ ERROR in ${job}: Near line ${line}, exit code ${code}" + if [ $_LOGGER_SILENT == false ]; then + echo -e "\e[45m/!\ ERROR in ${job}: Near line ${line}, exit code ${code}\e[0m" fi } @@ -610,7 +522,7 @@ function LoadConfigFile { } function Spinner { - if [ $_SILENT == true ]; then + if [ $_LOGGER_SILENT == true ] || [ "$_LOGGER_ERR_ONLY" == true ]; then return 0 fi @@ -905,10 +817,10 @@ function IsNumericExpand { local re="^-?[0-9]+([.][0-9]+)?$" - if [[ $value =~ $re ]]; then - echo 1 && return 1 + if [[ $value =~ ^-?[0-9]+([.][0-9]+)?$ ]]; then + echo 1 else - echo 0 && return 0 + echo 0 fi } @@ -917,9 +829,9 @@ function IsNumeric { local value="${1}" if [[ $value =~ ^[0-9]+([.][0-9]+)?$ ]]; then - echo 1 && return 1 + echo 1 else - echo 0 && return 0 + echo 0 fi } @@ -927,9 +839,9 @@ function IsInteger { local value="${1}" if [[ $value =~ ^[0-9]+$ ]]; then - echo 1 && return 1 + echo 1 else - echo 0 && return 0 + echo 0 fi } @@ -1008,8 +920,7 @@ function GetLocalOS { local localOsVar # There's no good way to tell if currently running in BusyBox shell. Using sluggish way. - ls --help 2>&1 | grep -i BusyBox > /dev/null - if [ $? == 0 ]; then + if ls --help 2>&1 | grep -i "BusyBox" > /dev/null; then localOsVar="BusyBox" else localOsVar="$(uname -spio 2>&1)" @@ -1053,47 +964,35 @@ function GetLocalOS { function GetRemoteOS { - local retval - local cmd local remoteOsVar - if [ "$REMOTE_OPERATION" == "yes" ]; then - CheckConnectivity3rdPartyHosts - CheckConnectivityRemoteHost +$SSH_CMD bash -s << 'ENDSSH' >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" 2>&1 - cmd=$SSH_CMD' "ls --help 2>&1 | grep -i BusyBox > /dev/null"' - Logger "cmd: $cmd" "DEBUG" - eval "$cmd" & - WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-0" true $KEEP_LOGGING - retval=$? - if [ $retval == 0 ]; then - remoteOsVar="BusyBox" - else - cmd=$SSH_CMD' "uname -spio" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1' - Logger "cmd: $cmd" "DEBUG" - eval "$cmd" & - WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-1" true $KEEP_LOGGING - retval=$? - if [ $retval != 0 ]; then - cmd=$SSH_CMD' "uname -v" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1' - Logger "cmd: $cmd" "DEBUG" - eval "$cmd" & - WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-2" true $KEEP_LOGGING - retval=$? - if [ $retval != 0 ]; then - cmd=$SSH_CMD' "uname" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1' - Logger "cmd: $cmd" "DEBUG" - eval "$cmd" & - WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-3" true $KEEP_LOGGING - retval=$? - if [ $retval != 0 ]; then - Logger "Cannot Get remote OS type." "ERROR" - fi - fi +function GetOs { + local localOsVar + + # There's no good way to tell if currently running in BusyBox shell. Using sluggish way. + if ls --help 2>&1 | grep -i "BusyBox" > /dev/null; then + localOsVar="BusyBox" + else + localOsVar="$(uname -spio 2>&1)" + if [ $? != 0 ]; then + localOsVar="$(uname -v 2>&1)" + if [ $? != 0 ]; then + localOsVar="$(uname)" fi - remoteOsVar=$(cat "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID") fi + fi + echo "$localOsVar" +} + +GetOs + +ENDSSH + + if [ -f "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" ]; then + remoteOsVar=$(cat "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID") case $remoteOsVar in *"Linux"*) REMOTE_OS="Linux" @@ -1115,7 +1014,7 @@ function GetRemoteOS { exit 1 ;; *) - if [ "$IGNORE_OS_TYPE" == "yes" ]; then #DOC: Undocumented option + if [ "$IGNORE_OS_TYPE" == "yes" ]; then #DOC: Undocumented debug only setting Logger "Running on unknown remote OS [$remoteOsVar]." "WARN" return fi @@ -1123,8 +1022,9 @@ function GetRemoteOS { Logger "Remote OS said:\n$remoteOsVar" "CRITICAL" exit 1 esac - Logger "Remote OS: [$remoteOsVar]." "DEBUG" + else + Logger "Cannot get Remote OS" "CRITICAL" fi } @@ -1147,7 +1047,7 @@ function RunLocalCommand { Logger "Command failed." "ERROR" fi - if [ $_VERBOSE == true ] || [ $retval -ne 0 ]; then + if [ $_LOGGER_VERBOSE == true ] || [ $retval -ne 0 ]; then Logger "Command output:\n$(cat $RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID)" "NOTICE" fi @@ -1181,7 +1081,7 @@ function RunRemoteCommand { Logger "Command failed." "ERROR" fi - if [ -f "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" ] && ([ $_VERBOSE == true ] || [ $retval -ne 0 ]) + if [ -f "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" ] && ([ $_LOGGER_VERBOSE == true ] || [ $retval -ne 0 ]) then Logger "Command output:\n$(cat $RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID)" "NOTICE" fi @@ -1399,7 +1299,7 @@ function PreInit { RSYNC_ARGS="-rltD" if [ "$_DRYRUN" == true ]; then RSYNC_DRY_ARG="-n" - DRY_WARNING="/!\ DRY RUN" + DRY_WARNING="/!\ DRY RUN " else RSYNC_DRY_ARG="" fi @@ -1629,22 +1529,22 @@ function TrapQuit { if [ "$RUN_AFTER_CMD_ON_ERROR" == "yes" ]; then RunAfterHook fi - CleanUp Logger "$PROGRAM finished with errors." "ERROR" SendAlert + CleanUp exitcode=1 elif [ $WARN_ALERT == true ]; then if [ "$RUN_AFTER_CMD_ON_ERROR" == "yes" ]; then RunAfterHook fi - CleanUp Logger "$PROGRAM finished with warnings." "WARN" SendAlert + CleanUp exitcode=2 else RunAfterHook - CleanUp Logger "$PROGRAM finshed without errors." "NOTICE" + CleanUp exitcode=0 fi @@ -3042,7 +2942,7 @@ function Init { ## Add update to default RSYNC_ARGS RSYNC_ARGS=$RSYNC_ARGS" -u" - if [ $_VERBOSE == true ]; then + if [ $_LOGGER_VERBOSE == true ]; then RSYNC_ARGS=$RSYNC_ARGS" -i" fi @@ -3118,6 +3018,7 @@ function Usage { echo "OPTIONS:" echo "--dry will run obackup without actually doing anything, just testing" echo "--silent will run obackup without any output to stdout, usefull for cron backups" + echo "--errors-only Output only errors (can be combined with silent or verbose)" echo "--verbose adds command outputs" echo "--stats Adds rsync transfer statistics to verbose output" echo "--partial Allows rsync to keep partial downloads that can be resumed later (experimental)" @@ -3137,7 +3038,7 @@ function Usage { # Command line argument flags _DRYRUN=false -_SILENT=false +_LOGGER_SILENT=false no_maxtime=false stats=false PARTIAL=no @@ -3156,10 +3057,10 @@ function GetCommandlineArguments { _DRYRUN=true ;; --silent) - _SILENT=true + _LOGGER_SILENT=true ;; --verbose) - _VERBOSE=true + _LOGGER_VERBOSE=true ;; --stats) stats=false @@ -3199,6 +3100,9 @@ function GetCommandlineArguments { --recipient=*) GPG_RECIPIENT="${i##*=}" ;; + --errors-only) + _LOGGER_ERR_ONLY=true + ;; esac done } @@ -3242,7 +3146,7 @@ fi DATE=$(date) Logger "--------------------------------------------------------------------" "NOTICE" -Logger "$DRY_WARNING $DATE - $PROGRAM v$PROGRAM_VERSION $BACKUP_TYPE script begin." "NOTICE" +Logger "$DRY_WARNING$DATE - $PROGRAM v$PROGRAM_VERSION $BACKUP_TYPE script begin." "NOTICE" Logger "--------------------------------------------------------------------" "NOTICE" Logger "Backup instance [$INSTANCE_ID] launched as $LOCAL_USER@$LOCAL_HOST (PID $SCRIPT_PID)" "NOTICE"