From 4f4fcf831a7b14c851b60a75fb7ed5f2f9152197 Mon Sep 17 00:00:00 2001 From: deajan Date: Sun, 23 Oct 2016 13:26:41 +0200 Subject: [PATCH] Fixed HumanToNumeric function --- dev/ofunctions.sh | 280 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 197 insertions(+), 83 deletions(-) diff --git a/dev/ofunctions.sh b/dev/ofunctions.sh index b02f9f0..88323dc 100644 --- a/dev/ofunctions.sh +++ b/dev/ofunctions.sh @@ -1,6 +1,6 @@ #### MINIMAL-FUNCTION-SET BEGIN #### -## FUNC_BUILD=2016090901 +## FUNC_BUILD=2016102303 ## 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: @@ -105,7 +105,7 @@ function _Logger { echo -e "$lvalue" >> "$LOG_FILE" CURRENT_LOG="$CURRENT_LOG"$'\n'"$lvalue" - if [ $_LOGGER_STDERR == true ]; then + if [ $_LOGGER_STDERR == true ] && [ "$evalue" != "" ]; then cat <<< "$evalue" 1>&2 elif [ "$_SILENT" == false ]; then echo -e "$svalue" @@ -267,7 +267,7 @@ function SendAlert { fi # - if [ "$_QUICK_SYNC" == "2" ]; then + if [ "$_QUICK_SYNC" -eq 2 ]; then Logger "Current task is a quicksync task. Will not send any alert." "NOTICE" return 0 fi @@ -536,6 +536,7 @@ function TrapError { local job="$0" local line="$1" local code="${2:-1}" + if [ $_SILENT == false ]; then echo -e " /!\ ERROR in ${job}: Near line ${line}, exit code ${code}" fi @@ -543,6 +544,7 @@ function TrapError { function LoadConfigFile { local configFile="${1}" + __CheckArguments 1 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG @@ -606,13 +608,13 @@ function joinString { function WaitForTaskCompletion { local pids="${1}" # pids to wait for, separated by semi-colon - local soft_max_time="${2}" # If program with pid $pid takes longer than $soft_max_time seconds, will log a warning, unless $soft_max_time equals 0. - local hard_max_time="${3}" # If program with pid $pid takes longer than $hard_max_time seconds, will stop execution, unless $hard_max_time equals 0. - local caller_name="${4}" # Who called this function + local softMaxTime="${2}" # If program with pid $pid takes longer than $softMaxTime seconds, will log a warning, unless $softMaxTime equals 0. + local hardMaxTime="${3}" # If program with pid $pid takes longer than $hardMaxTime seconds, will stop execution, unless $hardMaxTime equals 0. + local callerName="${4}" # Who called this function local counting="${5:-true}" # Count time since function has been launched if true, since script has been launched if false - local keep_logging="${6:-0}" # Log a standby message every X seconds. Set to zero to disable logging + local keepLogging="${6:-0}" # Log a standby message every X seconds. Set to zero to disable logging - Logger "${FUNCNAME[0]} called by [$caller_name]." "PARANOIA_DEBUG" #__WITH_PARANOIA_DEBUG + Logger "${FUNCNAME[0]} called by [$callerName]." "PARANOIA_DEBUG" #__WITH_PARANOIA_DEBUG __CheckArguments 6 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG local soft_alert=false # Does a soft alert need to be triggered, if yes, send an alert once @@ -648,8 +650,8 @@ function WaitForTaskCompletion { exec_time=$SECONDS fi - if [ $keep_logging -ne 0 ]; then - if [ $((($exec_time + 1) % $keep_logging)) -eq 0 ]; then + if [ $keepLogging -ne 0 ]; then + if [ $((($exec_time + 1) % $keepLogging)) -eq 0 ]; then if [ $log_ttime -ne $exec_time ]; then # Fix when sleep time lower than 1s log_ttime=$exec_time Logger "Current tasks still running with pids [$(joinString , ${pidsArray[@]})]." "NOTICE" @@ -657,15 +659,15 @@ function WaitForTaskCompletion { fi fi - if [ $exec_time -gt $soft_max_time ]; then - if [ $soft_alert == true ] && [ $soft_max_time -ne 0 ]; then - Logger "Max soft execution time exceeded for task [$caller_name] with pids [$(joinString , ${pidsArray[@]})]." "WARN" + if [ $exec_time -gt $softMaxTime ]; then + if [ $soft_alert == true ] && [ $softMaxTime -ne 0 ]; then + Logger "Max soft execution time exceeded for task [$callerName] with pids [$(joinString , ${pidsArray[@]})]." "WARN" soft_alert=true SendAlert true fi - if [ $exec_time -gt $hard_max_time ] && [ $hard_max_time -ne 0 ]; then - Logger "Max hard execution time exceeded for task [$caller_name] with pids [$(joinString , ${pidsArray[@]})]. Stopping task execution." "ERROR" + if [ $exec_time -gt $hardMaxTime ] && [ $hardMaxTime -ne 0 ]; then + Logger "Max hard execution time exceeded for task [$callerName] with pids [$(joinString , ${pidsArray[@]})]. Stopping task execution." "ERROR" for pid in "${pidsArray[@]}"; do KillChilds $pid true if [ $? == 0 ]; then @@ -682,8 +684,10 @@ function WaitForTaskCompletion { if [ $(IsInteger $pid) -eq 1 ]; then if kill -0 $pid > /dev/null 2>&1; then # Handle uninterruptible sleep state or zombies by ommiting them from running process array (How to kill that is already dead ? :) - #TODO(high): have this tested on *BSD, Mac & Win - pidState=$(ps -p$pid -o state= 2 > /dev/null) + #TODO(high): have this tested on *BSD, Mac, Win & busybox. + #TODO(high): propagate changes to ParallelExec + #pidState=$(ps -p$pid -o state= 2 > /dev/null) + pidState="$(eval $PROCESS_STATE_CMD)" if [ "$pidState" != "D" ] && [ "$pidState" != "Z" ]; then newPidsArray+=($pid) fi @@ -693,7 +697,7 @@ function WaitForTaskCompletion { retval=$? if [ $retval -ne 0 ]; then errorcount=$((errorcount+1)) - Logger "${FUNCNAME[0]} called by [$caller_name] finished monitoring [$pid] with exitcode [$retval]." "DEBUG" + Logger "${FUNCNAME[0]} called by [$callerName] finished monitoring [$pid] with exitcode [$retval]." "DEBUG" if [ "$WAIT_FOR_TASK_COMPLETION" == "" ]; then WAIT_FOR_TASK_COMPLETION="$pid:$retval" else @@ -714,7 +718,7 @@ function WaitForTaskCompletion { sleep $SLEEP_TIME done - Logger "${FUNCNAME[0]} ended for [$caller_name] using [$pidCount] subprocesses with [$errorcount] errors." "PARANOIA_DEBUG" #__WITH_PARANOIA_DEBUG + Logger "${FUNCNAME[0]} ended for [$callerName] using [$pidCount] subprocesses with [$errorcount] errors." "PARANOIA_DEBUG" #__WITH_PARANOIA_DEBUG # Return exit code if only one process was monitored, else return number of errors if [ $pidCount -eq 1 ] && [ $errorcount -eq 0 ]; then @@ -731,8 +735,13 @@ function ParallelExec { local numberOfProcesses="${1}" # Number of simultaneous commands to run local commandsArg="${2}" # Semi-colon separated list of commands, or file containing one command per line local readFromFile="${3:-false}" # Is commandsArg a file or a string ? + local softMaxTime="${4:-0}" + local hardMaxTime="${5:-0}" + local callerName="${6}" # Who called this function + local counting="${7:-true}" # Count time since function has been launched if true, since script has been launched if false + local keepLogging="${8:-0}" # Log a standby message every X seconds. Set to zero to disable logging - __CheckArguments 3 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG + __CheckArguments 8 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG local commandCount local command @@ -771,7 +780,7 @@ function ParallelExec { command="${commandsArray[$counter]}" fi Logger "Running command [$command]." "DEBUG" - eval "$command" & + eval "$command" > "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" 2>&1 & pid=$! pidsArray+=($pid) commandsArrayPid[$pid]="$command" @@ -784,7 +793,8 @@ function ParallelExec { if [ $(IsInteger $pid) -eq 1 ]; then # Handle uninterruptible sleep state or zombies by ommiting them from running process array (How to kill that is already dead ? :) if kill -0 $pid > /dev/null 2>&1; then - pidState=$(ps -p$pid -o state= 2 > /dev/null) + #pidState=$(ps -p$pid -o state= 2 > /dev/null) + pidState="$(eval $PROCESS_STATE_CMD)" if [ "$pidState" != "D" ] && [ "$pidState" != "Z" ]; then newPidsArray+=($pid) fi @@ -823,8 +833,6 @@ function CleanUp { fi } -#### MINIMAL-FUNCTION-SET END #### - # obsolete, use StripQuotes function SedStripQuotes { echo $(echo $1 | sed "s/^\([\"']\)\(.*\)\1\$/\2/g") @@ -833,6 +841,7 @@ function SedStripQuotes { # Usage: var=$(StripSingleQuotes "$var") function StripSingleQuotes { local string="${1}" + string="${string/#\'/}" # Remove singlequote if it begins string string="${string/%\'/}" # Remove singlequote if it ends string echo "$string" @@ -841,6 +850,7 @@ function StripSingleQuotes { # Usage: var=$(StripDoubleQuotes "$var") function StripDoubleQuotes { local string="${1}" + string="${string/#\"/}" string="${string/%\"/}" echo "$string" @@ -848,12 +858,14 @@ function StripDoubleQuotes { function StripQuotes { local string="${1}" + echo "$(StripSingleQuotes $(StripDoubleQuotes $string))" } # Usage var=$(EscapeSpaces "$var") or var="$(EscapeSpaces "$var")" function EscapeSpaces { local string="${1}" # String on which spaces will be escaped + echo "${string// /\\ }" } @@ -861,20 +873,22 @@ function IsNumericExpand { eval "local value=\"${1}\"" # Needed eval so variable variables can be processed local re="^-?[0-9]+([.][0-9]+)?$" + if [[ $value =~ $re ]]; then - echo 1 + echo 1 && return 1 else - echo 0 + echo 0 && return 0 fi } +# Usage [ $(IsNumeric $var) -eq 1 ] function IsNumeric { local value="${1}" if [[ $value =~ ^[0-9]+([.][0-9]+)?$ ]]; then - echo 1 + echo 1 && return 1 else - echo 0 + echo 0 && return 0 fi } @@ -882,12 +896,43 @@ function IsInteger { local value="${1}" if [[ $value =~ ^[0-9]+$ ]]; then - echo 1 + echo 1 && return 1 else - echo 0 + echo 0 && return 0 fi } +# Converts human readable sizes into integer kilobyte sizes +# Usage numericSize="$(HumanToNumeric $humanSize)" +function HumanToNumeric { + local value="${1}" + + local notation + local suffix + local suffixPresent + local multiplier + + notation=(K M G T P E) + for suffix in "${notation[@]}"; do + multiplier=$((multiplier+1)) + if [[ "$value" == *"$suffix"* ]]; then + suffixPresent=$suffix + break; + fi + done + + if [ "$suffixPresent" != "" ]; then + value=${value%$suffix*} + value=${value%.*} + # /1024 since we convert to kilobytes instead of bytes + value=$((value*(1024**multiplier/1024))) + else + value=${value%.*} + fi + + echo $value +} + ## from https://gist.github.com/cdown/1163649 function urlEncode { local length="${#1}" @@ -907,25 +952,44 @@ function urlEncode { } function urlDecode { - local url_encoded="${1//+/ }" + local urlEncoded="${1//+/ }" - printf '%b' "${url_encoded//%/\\x}" + printf '%b' "${urlEncoded//%/\\x}" +} + +## Modified version of http://stackoverflow.com/a/8574392 +## Usage: arrayContains "needle" "${haystack[@]}" +arrayContains () { + local e + + if [ "$2" == "" ]; then + echo 0 && return 0 + fi + + for e in "${@:2}"; do + [[ "$e" == "$1" ]] && echo 1 && return 1 + done + echo 0 && return 0 } function GetLocalOS { __CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG - local local_os_var= + local localOsVar - local_os_var="$(uname -spio 2>&1)" - if [ $? != 0 ]; then - local_os_var="$(uname -v 2>&1)" + if type busybox > /dev/null 2>&1; then + localOsVar="BusyBox" + else + localOsVar="$(uname -spio 2>&1)" if [ $? != 0 ]; then - local_os_var="$(uname)" + localOsVar="$(uname -v 2>&1)" + if [ $? != 0 ]; then + localOsVar="$(uname)" + fi fi fi - case $local_os_var in + case $localOsVar in *"Linux"*) LOCAL_OS="Linux" ;; @@ -938,54 +1002,68 @@ function GetLocalOS { *"Darwin"*) LOCAL_OS="MacOSX" ;; + *"BusyBox"*) + LOCAL_OS="BUSYBOX" + ;; *) - if [ "$IGNORE_OS_TYPE" == "yes" ]; then #DOC: Undocumented option - Logger "Running on unknown local OS [$local_os_var]." "WARN" + if [ "$IGNORE_OS_TYPE" == "yes" ]; then #TODO(doc): Undocumented option + Logger "Running on unknown local OS [$localOsVar]." "WARN" return fi - Logger "Running on >> $local_os_var << not supported. Please report to the author." "ERROR" + Logger "Running on >> $localOsVar << not supported. Please report to the author." "ERROR" exit 1 ;; esac - Logger "Local OS: [$local_os_var]." "DEBUG" + Logger "Local OS: [$localOsVar]." "DEBUG" } +#### MINIMAL-FUNCTION-SET END #### + function GetRemoteOS { __CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG - local cmd= - local remote_os_var= - + local retval + local cmd + local remoteOsVar if [ "$REMOTE_OPERATION" == "yes" ]; then CheckConnectivity3rdPartyHosts CheckConnectivityRemoteHost - cmd=$SSH_CMD' "uname -spio" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1' + + cmd=$SSH_CMD' "type busybox" > /dev/null 2>&1' Logger "cmd: $cmd" "DEBUG" eval "$cmd" & - WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-1" true $KEEP_LOGGING + WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-0" true $KEEP_LOGGING retval=$? - if [ $retval != 0 ]; then - cmd=$SSH_CMD' "uname -v" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1' + 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]}"-2" true $KEEP_LOGGING + WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-1" true $KEEP_LOGGING retval=$? if [ $retval != 0 ]; then - cmd=$SSH_CMD' "uname" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1' + 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]}"-3" true $KEEP_LOGGING + WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-2" true $KEEP_LOGGING retval=$? if [ $retval != 0 ]; then - Logger "Cannot Get remote OS type." "ERROR" + 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 fi + remoteOsVar=$(cat "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID") fi - remote_os_var=$(cat "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID") - - case $remote_os_var in + case $remoteOsVar in *"Linux"*) REMOTE_OS="Linux" ;; @@ -998,27 +1076,30 @@ function GetRemoteOS { *"Darwin"*) REMOTE_OS="MacOSX" ;; + *"BusyBox"*) + REMOTE_OS="BUSYBOX" + ;; *"ssh"*|*"SSH"*) Logger "Cannot connect to remote system." "CRITICAL" exit 1 ;; *) if [ "$IGNORE_OS_TYPE" == "yes" ]; then #DOC: Undocumented option - Logger "Running on unknown remote OS [$remote_os_var]." "WARN" + Logger "Running on unknown remote OS [$remoteOsVar]." "WARN" return fi Logger "Running on remote OS failed. Please report to the author if the OS is not supported." "CRITICAL" - Logger "Remote OS said:\n$remote_os_var" "CRITICAL" + Logger "Remote OS said:\n$remoteOsVar" "CRITICAL" exit 1 esac - Logger "Remote OS: [$remote_os_var]." "DEBUG" + Logger "Remote OS: [$remoteOsVar]." "DEBUG" fi } function RunLocalCommand { local command="${1}" # Command to run - local hard_max_time="${2}" # Max time to wait for command to compleet + local hardMaxTime="${2}" # Max time to wait for command to compleet __CheckArguments 2 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG if [ $_DRYRUN == true ]; then @@ -1028,7 +1109,7 @@ function RunLocalCommand { Logger "Running command [$command] on local host." "NOTICE" eval "$command" > "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" 2>&1 & - WaitForTaskCompletion $! 0 $hard_max_time ${FUNCNAME[0]} true $KEEP_LOGGING + WaitForTaskCompletion $! 0 $hardMaxTime ${FUNCNAME[0]} true $KEEP_LOGGING retval=$? if [ $retval -eq 0 ]; then Logger "Command succeded." "NOTICE" @@ -1049,7 +1130,7 @@ function RunLocalCommand { ## Runs remote command $1 and waits for completition in $2 seconds function RunRemoteCommand { local command="${1}" # Command to run - local hard_max_time="${2}" # Max time to wait for command to compleet + local hardMaxTime="${2}" # Max time to wait for command to compleet __CheckArguments 2 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG CheckConnectivity3rdPartyHosts @@ -1063,7 +1144,7 @@ function RunRemoteCommand { cmd=$SSH_CMD' "$command" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1' Logger "cmd: $cmd" "DEBUG" eval "$cmd" & - WaitForTaskCompletion $! 0 $hard_max_time ${FUNCNAME[0]} true $KEEP_LOGGING + WaitForTaskCompletion $! 0 $hardMaxTime ${FUNCNAME[0]} true $KEEP_LOGGING retval=$? if [ $retval -eq 0 ]; then Logger "Command succeded." "NOTICE" @@ -1085,7 +1166,7 @@ function RunRemoteCommand { function RunBeforeHook { __CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG - local pids= + local pids if [ "$LOCAL_RUN_BEFORE_CMD" != "" ]; then RunLocalCommand "$LOCAL_RUN_BEFORE_CMD" $MAX_EXEC_TIME_PER_CMD_BEFORE & @@ -1142,13 +1223,13 @@ function CheckConnectivityRemoteHost { function CheckConnectivity3rdPartyHosts { __CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG - local remote_3rd_party_success + local remote3rdPartySuccess local retval if [ "$_PARANOIA_DEBUG" != "yes" ]; then # Do not loose time in paranoia debug if [ "$REMOTE_3RD_PARTY_HOSTS" != "" ]; then - remote_3rd_party_success=false + remote3rdPartySuccess=false for i in $REMOTE_3RD_PARTY_HOSTS do eval "$PING_CMD $i > /dev/null 2>&1" & @@ -1157,11 +1238,11 @@ function CheckConnectivity3rdPartyHosts { if [ $retval != 0 ]; then Logger "Cannot ping 3rd party host [$i]. Return code [$retval]." "NOTICE" else - remote_3rd_party_success=true + remote3rdPartySuccess=true fi done - if [ $remote_3rd_party_success == false ]; then + if [ $remote3rdPartySuccess == false ]; then Logger "No remote 3rd party host responded to ping. No internet ?" "WARN" return 1 else @@ -1224,7 +1305,7 @@ function __CheckArguments { #__END_WITH_PARANOIA_DEBUG function RsyncPatternsAdd { - local pattern_type="${1}" # exclude or include + local patternType="${1}" # exclude or include local pattern="${2}" __CheckArguments 2 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG @@ -1236,7 +1317,7 @@ function RsyncPatternsAdd { while [ -n "$rest" ] do # Take the string until first occurence until $PATH_SEPARATOR_CHAR - str=${rest%%;*} + str=${rest%%;*} #TODO: replace ; with $PATH_SEPARATOR_CHAR # Handle the last case if [ "$rest" = "${rest/$PATH_SEPARATOR_CHAR/}" ]; then rest= @@ -1245,26 +1326,26 @@ function RsyncPatternsAdd { rest=${rest#*$PATH_SEPARATOR_CHAR} fi if [ "$RSYNC_PATTERNS" == "" ]; then - RSYNC_PATTERNS="--"$pattern_type"=\"$str\"" + RSYNC_PATTERNS="--"$patternType"=\"$str\"" else - RSYNC_PATTERNS="$RSYNC_PATTERNS --"$pattern_type"=\"$str\"" + RSYNC_PATTERNS="$RSYNC_PATTERNS --"$patternType"=\"$str\"" fi done set +f } function RsyncPatternsFromAdd { - local pattern_type="${1}" - local pattern_from="${2}" + local patternType="${1}" + local patternFrom="${2}" __CheckArguments 2 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG ## Check if the exclude list has a full path, and if not, add the config file path if there is one - if [ "$(basename $pattern_from)" == "$pattern_from" ]; then - pattern_from="$(dirname $CONFIG_FILE)/$pattern_from" + if [ "$(basename $patternFrom)" == "$patternFrom" ]; then + patternFrom="$(dirname $CONFIG_FILE)/$patternFrom" fi - if [ -e "$pattern_from" ]; then - RSYNC_PATTERNS="$RSYNC_PATTERNS --"$pattern_type"-from=\"$pattern_from\"" + if [ -e "$patternFrom" ]; then + RSYNC_PATTERNS="$RSYNC_PATTERNS --"$patternType"-from=\"$patternFrom\"" fi } @@ -1284,7 +1365,8 @@ function RsyncPatterns { if [ "$RSYNC_INCLUDE_FROM" != "" ]; then RsyncPatternsFromAdd "include" "$RSYNC_INCLUDE_FROM" fi - elif [ "$RSYNC_PATTERN_FIRST" == "include" ]; then + # Use default include first for quicksync runs + elif [ "$RSYNC_PATTERN_FIRST" == "include" ] || [ $_QUICK_SYNC -eq 2 ]; then if [ "$RSYNC_INCLUDE_PATTERN" != "" ]; then RsyncPatternsAdd "include" "$RSYNC_INCLUDE_PATTERN" fi @@ -1427,9 +1509,20 @@ function PostInit { __CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG # Define remote commands - SSH_CMD="$(type -p ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $SSH_OPTS $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT" - SCP_CMD="$(type -p scp) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY -P $REMOTE_PORT" - RSYNC_SSH_CMD="$(type -p ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $SSH_OPTS -p $REMOTE_PORT" + if [ -f "$SSH_RSA_PRIVATE_KEY" ]; then + SSH_CMD="$(type -p ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $SSH_OPTS $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT" + SCP_CMD="$(type -p scp) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY -P $REMOTE_PORT" + RSYNC_SSH_CMD="$(type -p ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $SSH_OPTS -p $REMOTE_PORT" + elif [ -f "$SSH_PASSWORD_FILE" ]; then + SSH_CMD="$(type -p sshpass) -f $SSH_PASSWORD_FILE $(type -p ssh) $SSH_COMP $SSH_OPTS $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT" + SCP_CMD="$(type -p sshpass) -f $SSH_PASSWORD_FILE $(type -p scp) $SSH_COMP -P $REMOTE_PORT" + RSYNC_SSH_CMD="$(type -p sshpass) -f $SSH_PASSWORD_FILE $(type -p ssh) $SSH_COMP $SSH_OPTS -p $REMOTE_PORT" + else + SSH_PASSWORD="" + SSH_CMD="" + SCP_CMD="" + RSYNC_SSH_CMD="" + fi } function InitLocalOSSettings { @@ -1447,12 +1540,20 @@ function InitLocalOSSettings { PING_CMD="ping -c 2 -i .2" fi + if [ "$LOCAL_OS" == "BUSYBOX" ]; then + PROCESS_STATE_CMD="echo none" + else + PROCESS_STATE_CMD='ps -p$pid -o state= 2 > /dev/null' + fi + ## Stat command has different syntax on Linux and FreeBSD/MacOSX if [ "$LOCAL_OS" == "MacOSX" ] || [ "$LOCAL_OS" == "BSD" ]; then + # Tested on BSD and Mac STAT_CMD="stat -f \"%Sm\"" STAT_CTIME_MTIME_CMD="stat -f %N;%c;%m" else - STAT_CMD="stat --format %y" + # Tested on GNU stat and busybox + STAT_CMD="stat -c %y" STAT_CTIME_MTIME_CMD="stat -c %n;%Z;%Y" fi } @@ -1489,4 +1590,17 @@ function PrintIFS { printf "IFS is: %q" "$IFS" } +# Process debugging +# Recursive function to get all parents from a pid +function ParentPid { + local pid="${1}" # Pid to analyse + local parent + + parent=$(ps -p $pid -o ppid=) + echo "$pid is a child of $parent" + if [ $parent -gt 0 ]; then + ParentPid $parent + fi +} + ## END Generic functions