From b9bfbb39746c760014f92f1ea537e0bba6786628 Mon Sep 17 00:00:00 2001 From: deajan Date: Tue, 8 Sep 2015 16:16:22 +0200 Subject: [PATCH] Coding style compliance continues --- CODING_STYLE.TXT | 8 +++ osync-batch.sh | 138 ++++++++++++++++++++++++++++------------------- ssh_filter.sh | 32 +++++------ 3 files changed, 108 insertions(+), 70 deletions(-) diff --git a/CODING_STYLE.TXT b/CODING_STYLE.TXT index 824708e..c675a44 100644 --- a/CODING_STYLE.TXT +++ b/CODING_STYLE.TXT @@ -64,3 +64,11 @@ The eval command should always contain 2>1&. There's a special case where this is needed: eval "some;commands" 2>> "$LOG_FILE" in order to get error messages into the log. +7. Version numbering + +Using bind style versionning: + +YYYYMMDDVV (Year, Month, Day, Revision) +Ex: + +2015012402 = 2nd revision of 24 Jan 2015 diff --git a/osync-batch.sh b/osync-batch.sh index e8a23ea..3419ca9 100755 --- a/osync-batch.sh +++ b/osync-batch.sh @@ -3,7 +3,7 @@ PROGRAM="Osync-batch" # Batch program to run osync instances sequentially and rerun failed ones AUTHOR="(L) 2013-2014 by Orsiris \"Ozy\" de Jong" CONTACT="http://www.netpower.fr/osync - ozy@netpower.fr" -PROGRAM_BUILD=2015082501 +PROGRAM_BUILD=2015090801 ## Runs an osync instance for every conf file found ## If an instance fails, run it again if time permits @@ -19,44 +19,74 @@ MAX_RERUNS=3 ## Log file path if [ -w /var/log ]; then - LOG_FILE=/var/log/osync-batch.log + LOG_FILE=/var/log/osync-batch.log else - LOG_FILE=./osync-batch.log + LOG_FILE=./osync-batch.log fi # No need to edit under this line ############################################################## -function Log { - prefix="TIME: $SECONDS - " - echo -e "$prefix$1" >> "$LOG_FILE" +function _logger { + local value="${1}" # What to log + echo -e "$value" >> "$LOG_FILE" - if [ $silent -eq 0 ] - then - echo -e "$prefix$1" - fi + if [ $silent -eq 0 ]; then + echo -e "$value" + fi +} + +function Logger { + local value="${1}" # What to log + local level="${2}" # Log level: DEBUG, NOTICE, WARN, ERROR, CRITIAL + + # Special case in daemon mode we should timestamp instead of counting seconds + if [ $sync_on_changes -eq 1 ]; then + prefix="$(date) - " + else + prefix="TIME: $SECONDS - " + fi + + if [ "$level" == "CRITICAL" ]; then + _logger "$prefix\e[41m$value\e[0m" + ERROR_ALERT=1 + elif [ "$level" == "ERROR" ]; then + _logger "$prefix\e[91m$value\e[0m" + ERROR_ALERT=1 + elif [ "$level" == "WARN" ]; then + _logger "$prefix\e[93m$value\e[0m" + elif [ "$level" == "NOTICE" ]; then + _logger "$prefix$value" + elif [ "$level" == "DEBUG" ]; then + if [ "$DEBUG" == "yes" ]; then + _logger "$prefix$value" + fi + else + _logger "\e[41mLogger function called without proper loglevel.\e[0m" + _logger "$prefix$value" + fi } function CheckEnvironment { - ## Osync executable full path can be set here if it cannot be found on the system - if ! type -p osync.sh > /dev/null 2>&1 - then - if [ -f /usr/local/bin/osync.sh ] - then - OSYNC_EXECUTABLE=/usr/local/bin/osync.sh - elif [ -f ./osync.sh ] - then - OSYNC_EXECUTABLE=./osync.sh - else - Log "Could not find osync.sh" - exit 1 - fi - else - OSYNC_EXECUTABLE=$(type -p osync.sh) - fi + ## Osync executable full path can be set here if it cannot be found on the system + if ! type -p osync.sh > /dev/null 2>&1 + then + if [ -f /usr/local/bin/osync.sh ] + then + OSYNC_EXECUTABLE=/usr/local/bin/osync.sh + elif [ -f ./osync.sh ] + then + OSYNC_EXECUTABLE=./osync.sh + else + Logger "Could not find osync.sh" "CRITICAL" + exit 1 + fi + else + OSYNC_EXECUTABLE=$(type -p osync.sh) + fi ## Check for CONF_FILE_PATH if [ ! -d "$CONF_FILE_PATH" ]; then - Log "Cannot find conf file path $CONF_FILE_PATH" + Logger "Cannot find conf file path $CONF_FILE_PATH" "CRITICAL" Usage fi } @@ -75,19 +105,19 @@ function Batch { RERUNS=0 while ([ $MAX_EXECUTION_TIME -gt $SECONDS ] || [ $MAX_EXECUTION_TIME -eq 0 ]) && [ "$RUN" != "" ] && [ $MAX_RERUNS -gt $RERUNS ] do - Log "Osync instances will be run for: $RUN" + Logger "Osync instances will be run for: $RUN" "NOTICE" for i in $RUN do $OSYNC_EXECUTABLE "$i" $opts if [ $? != 0 ]; then - Log "Run instance $(basename $i) failed" + Logger "Run instance $(basename $i) failed" "ERROR" if [ "RUN_AGAIN" == "" ]; then RUN_AGAIN="$i" else RUN_AGAIN=$RUN_AGAIN" $i" fi else - Log "Run instance $(basename $i) succeed." + Logger "Run instance $(basename $i) succeed." "NOTICE" fi done RUN="$RUN_AGAIN" @@ -97,22 +127,22 @@ function Batch { } function Usage { - echo "$PROGRAM $PROGRAM_BUILD" - echo $AUTHOR - echo $CONTACT - echo "" - echo "Batch script to sequentially run osync instances and rerun failed ones." - echo "Usage: osync-batch.sh [OPTIONS]" - echo "" - echo "[OPTIONS]" + echo "$PROGRAM $PROGRAM_BUILD" + echo $AUTHOR + echo $CONTACT + echo "" + echo "Batch script to sequentially run osync instances and rerun failed ones." + echo "Usage: osync-batch.sh [OPTIONS]" + echo "" + echo "[OPTIONS]" echo "--path=/path/to/conf Path to osync conf files, defaults to /etc/osync" echo "--max-reruns=X Number of runs max for failed instances, (defaults to 3)" echo "--max-exec-time=X Retry failed instances only if max execution time not reached (defaults to 36000 seconds). Set to 0 to bypass execution time check." echo "--no-maxtime Run osync without honoring conf file defined timeouts" - echo "--dry Will run osync without actually doing anything; just testing" - echo "--silent Will run osync without any output to stdout, used for cron jobs" - echo "--verbose Increases output" - exit 128 + echo "--dry Will run osync without actually doing anything; just testing" + echo "--silent Will run osync without any output to stdout, used for cron jobs" + echo "--verbose Increases output" + exit 128 } silent=0 @@ -121,18 +151,18 @@ verbose=0 opts="" for i in "$@" do - case $i in - --silent) - silent=1 + case $i in + --silent) + silent=1 opts=$opts" --silent" - ;; - --dry) - dry=1 + ;; + --dry) + dry=1 opts=$opts" --dry" - ;; - --verbose) - verbose=1 - opts=$opts" --verbose" + ;; + --verbose) + verbose=1 + opts=$opts" --verbose" ;; --no-maxtime) opts=$opts" --no-maxtime" @@ -150,12 +180,12 @@ do Usage ;; *) - Log "Unknown param '$i'" + Logger "Unknown param '$i'" "CRITICAL" Usage ;; esac done CheckEnvironment -Log "$(date) Osync batch run" +Logger "$(date) Osync batch run" "NOTICE" Batch diff --git a/ssh_filter.sh b/ssh_filter.sh index 3207153..dbae96b 100755 --- a/ssh_filter.sh +++ b/ssh_filter.sh @@ -1,6 +1,6 @@ #!/bin/bash -##### Osync ssh command filter build 2015070203 +##### Osync ssh command filter build 2015090801 ##### This script should be located in /usr/local/bin in the remote system to sync / backup ##### It will filter the commands that can be run remotely via ssh. ##### Please chmod 755 and chown root:root this file @@ -65,21 +65,21 @@ case ${SSH_ORIGINAL_COMMAND%% *} in Go elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo find"* ]]; then Go - elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo mkdir"* ]] - then - Go - elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo rm"* ]] - then - Go - elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo echo"* ]] - then - Go - elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo df"* ]] - then - Go - elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo mv"* ]] - then - Go + elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo mkdir"* ]] + then + Go + elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo rm"* ]] + then + Go + elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo echo"* ]] + then + Go + elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo df"* ]] + then + Go + elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo mv"* ]] + then + Go elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo $CMD1"* ]]; then if [ "$CMD1" != "" ]; then Go