1
0
mirror of https://github.com/deajan/obackup.git synced 2024-12-26 07:23:41 +01:00

Fixed HumanToNumeric function

This commit is contained in:
deajan 2016-10-23 13:26:41 +02:00
parent 2118a27bcc
commit 4f4fcf831a

View File

@ -1,6 +1,6 @@
#### MINIMAL-FUNCTION-SET BEGIN #### #### 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 ## 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: ## To use in a program, define the following variables:
@ -105,7 +105,7 @@ function _Logger {
echo -e "$lvalue" >> "$LOG_FILE" echo -e "$lvalue" >> "$LOG_FILE"
CURRENT_LOG="$CURRENT_LOG"$'\n'"$lvalue" CURRENT_LOG="$CURRENT_LOG"$'\n'"$lvalue"
if [ $_LOGGER_STDERR == true ]; then if [ $_LOGGER_STDERR == true ] && [ "$evalue" != "" ]; then
cat <<< "$evalue" 1>&2 cat <<< "$evalue" 1>&2
elif [ "$_SILENT" == false ]; then elif [ "$_SILENT" == false ]; then
echo -e "$svalue" echo -e "$svalue"
@ -267,7 +267,7 @@ function SendAlert {
fi fi
# <OSYNC SPECIFIC> # <OSYNC SPECIFIC>
if [ "$_QUICK_SYNC" == "2" ]; then if [ "$_QUICK_SYNC" -eq 2 ]; then
Logger "Current task is a quicksync task. Will not send any alert." "NOTICE" Logger "Current task is a quicksync task. Will not send any alert." "NOTICE"
return 0 return 0
fi fi
@ -536,6 +536,7 @@ function TrapError {
local job="$0" local job="$0"
local line="$1" local line="$1"
local code="${2:-1}" local code="${2:-1}"
if [ $_SILENT == false ]; then if [ $_SILENT == false ]; then
echo -e " /!\ ERROR in ${job}: Near line ${line}, exit code ${code}" echo -e " /!\ ERROR in ${job}: Near line ${line}, exit code ${code}"
fi fi
@ -543,6 +544,7 @@ function TrapError {
function LoadConfigFile { function LoadConfigFile {
local configFile="${1}" local configFile="${1}"
__CheckArguments 1 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG __CheckArguments 1 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
@ -606,13 +608,13 @@ function joinString {
function WaitForTaskCompletion { function WaitForTaskCompletion {
local pids="${1}" # pids to wait for, separated by semi-colon 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 softMaxTime="${2}" # If program with pid $pid takes longer than $softMaxTime seconds, will log a warning, unless $softMaxTime 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 hardMaxTime="${3}" # If program with pid $pid takes longer than $hardMaxTime seconds, will stop execution, unless $hardMaxTime equals 0.
local caller_name="${4}" # Who called this function 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 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 __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 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 exec_time=$SECONDS
fi fi
if [ $keep_logging -ne 0 ]; then if [ $keepLogging -ne 0 ]; then
if [ $((($exec_time + 1) % $keep_logging)) -eq 0 ]; then if [ $((($exec_time + 1) % $keepLogging)) -eq 0 ]; then
if [ $log_ttime -ne $exec_time ]; then # Fix when sleep time lower than 1s if [ $log_ttime -ne $exec_time ]; then # Fix when sleep time lower than 1s
log_ttime=$exec_time log_ttime=$exec_time
Logger "Current tasks still running with pids [$(joinString , ${pidsArray[@]})]." "NOTICE" Logger "Current tasks still running with pids [$(joinString , ${pidsArray[@]})]." "NOTICE"
@ -657,15 +659,15 @@ function WaitForTaskCompletion {
fi fi
fi fi
if [ $exec_time -gt $soft_max_time ]; then if [ $exec_time -gt $softMaxTime ]; then
if [ $soft_alert == true ] && [ $soft_max_time -ne 0 ]; then if [ $soft_alert == true ] && [ $softMaxTime -ne 0 ]; then
Logger "Max soft execution time exceeded for task [$caller_name] with pids [$(joinString , ${pidsArray[@]})]." "WARN" Logger "Max soft execution time exceeded for task [$callerName] with pids [$(joinString , ${pidsArray[@]})]." "WARN"
soft_alert=true soft_alert=true
SendAlert true SendAlert true
fi fi
if [ $exec_time -gt $hard_max_time ] && [ $hard_max_time -ne 0 ]; then if [ $exec_time -gt $hardMaxTime ] && [ $hardMaxTime -ne 0 ]; then
Logger "Max hard execution time exceeded for task [$caller_name] with pids [$(joinString , ${pidsArray[@]})]. Stopping task execution." "ERROR" Logger "Max hard execution time exceeded for task [$callerName] with pids [$(joinString , ${pidsArray[@]})]. Stopping task execution." "ERROR"
for pid in "${pidsArray[@]}"; do for pid in "${pidsArray[@]}"; do
KillChilds $pid true KillChilds $pid true
if [ $? == 0 ]; then if [ $? == 0 ]; then
@ -682,8 +684,10 @@ function WaitForTaskCompletion {
if [ $(IsInteger $pid) -eq 1 ]; then if [ $(IsInteger $pid) -eq 1 ]; then
if kill -0 $pid > /dev/null 2>&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 ? :) # 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 #TODO(high): have this tested on *BSD, Mac, Win & busybox.
pidState=$(ps -p$pid -o state= 2 > /dev/null) #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 if [ "$pidState" != "D" ] && [ "$pidState" != "Z" ]; then
newPidsArray+=($pid) newPidsArray+=($pid)
fi fi
@ -693,7 +697,7 @@ function WaitForTaskCompletion {
retval=$? retval=$?
if [ $retval -ne 0 ]; then if [ $retval -ne 0 ]; then
errorcount=$((errorcount+1)) 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 if [ "$WAIT_FOR_TASK_COMPLETION" == "" ]; then
WAIT_FOR_TASK_COMPLETION="$pid:$retval" WAIT_FOR_TASK_COMPLETION="$pid:$retval"
else else
@ -714,7 +718,7 @@ function WaitForTaskCompletion {
sleep $SLEEP_TIME sleep $SLEEP_TIME
done 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 # Return exit code if only one process was monitored, else return number of errors
if [ $pidCount -eq 1 ] && [ $errorcount -eq 0 ]; then if [ $pidCount -eq 1 ] && [ $errorcount -eq 0 ]; then
@ -731,8 +735,13 @@ function ParallelExec {
local numberOfProcesses="${1}" # Number of simultaneous commands to run 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 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 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 commandCount
local command local command
@ -771,7 +780,7 @@ function ParallelExec {
command="${commandsArray[$counter]}" command="${commandsArray[$counter]}"
fi fi
Logger "Running command [$command]." "DEBUG" Logger "Running command [$command]." "DEBUG"
eval "$command" & eval "$command" > "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" 2>&1 &
pid=$! pid=$!
pidsArray+=($pid) pidsArray+=($pid)
commandsArrayPid[$pid]="$command" commandsArrayPid[$pid]="$command"
@ -784,7 +793,8 @@ function ParallelExec {
if [ $(IsInteger $pid) -eq 1 ]; then 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 ? :) # 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 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 if [ "$pidState" != "D" ] && [ "$pidState" != "Z" ]; then
newPidsArray+=($pid) newPidsArray+=($pid)
fi fi
@ -823,8 +833,6 @@ function CleanUp {
fi fi
} }
#### MINIMAL-FUNCTION-SET END ####
# obsolete, use StripQuotes # obsolete, use StripQuotes
function SedStripQuotes { function SedStripQuotes {
echo $(echo $1 | sed "s/^\([\"']\)\(.*\)\1\$/\2/g") echo $(echo $1 | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")
@ -833,6 +841,7 @@ function SedStripQuotes {
# Usage: var=$(StripSingleQuotes "$var") # Usage: var=$(StripSingleQuotes "$var")
function StripSingleQuotes { function StripSingleQuotes {
local string="${1}" local string="${1}"
string="${string/#\'/}" # Remove singlequote if it begins string string="${string/#\'/}" # Remove singlequote if it begins string
string="${string/%\'/}" # Remove singlequote if it ends string string="${string/%\'/}" # Remove singlequote if it ends string
echo "$string" echo "$string"
@ -841,6 +850,7 @@ function StripSingleQuotes {
# Usage: var=$(StripDoubleQuotes "$var") # Usage: var=$(StripDoubleQuotes "$var")
function StripDoubleQuotes { function StripDoubleQuotes {
local string="${1}" local string="${1}"
string="${string/#\"/}" string="${string/#\"/}"
string="${string/%\"/}" string="${string/%\"/}"
echo "$string" echo "$string"
@ -848,12 +858,14 @@ function StripDoubleQuotes {
function StripQuotes { function StripQuotes {
local string="${1}" local string="${1}"
echo "$(StripSingleQuotes $(StripDoubleQuotes $string))" echo "$(StripSingleQuotes $(StripDoubleQuotes $string))"
} }
# Usage var=$(EscapeSpaces "$var") or var="$(EscapeSpaces "$var")" # Usage var=$(EscapeSpaces "$var") or var="$(EscapeSpaces "$var")"
function EscapeSpaces { function EscapeSpaces {
local string="${1}" # String on which spaces will be escaped local string="${1}" # String on which spaces will be escaped
echo "${string// /\\ }" echo "${string// /\\ }"
} }
@ -861,20 +873,22 @@ function IsNumericExpand {
eval "local value=\"${1}\"" # Needed eval so variable variables can be processed eval "local value=\"${1}\"" # Needed eval so variable variables can be processed
local re="^-?[0-9]+([.][0-9]+)?$" local re="^-?[0-9]+([.][0-9]+)?$"
if [[ $value =~ $re ]]; then if [[ $value =~ $re ]]; then
echo 1 echo 1 && return 1
else else
echo 0 echo 0 && return 0
fi fi
} }
# Usage [ $(IsNumeric $var) -eq 1 ]
function IsNumeric { function IsNumeric {
local value="${1}" local value="${1}"
if [[ $value =~ ^[0-9]+([.][0-9]+)?$ ]]; then if [[ $value =~ ^[0-9]+([.][0-9]+)?$ ]]; then
echo 1 echo 1 && return 1
else else
echo 0 echo 0 && return 0
fi fi
} }
@ -882,12 +896,43 @@ function IsInteger {
local value="${1}" local value="${1}"
if [[ $value =~ ^[0-9]+$ ]]; then if [[ $value =~ ^[0-9]+$ ]]; then
echo 1 echo 1 && return 1
else else
echo 0 echo 0 && return 0
fi 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 ## from https://gist.github.com/cdown/1163649
function urlEncode { function urlEncode {
local length="${#1}" local length="${#1}"
@ -907,25 +952,44 @@ function urlEncode {
} }
function urlDecode { 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 { function GetLocalOS {
__CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG __CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
local local_os_var= local localOsVar
local_os_var="$(uname -spio 2>&1)" if type busybox > /dev/null 2>&1; then
if [ $? != 0 ]; then localOsVar="BusyBox"
local_os_var="$(uname -v 2>&1)" else
localOsVar="$(uname -spio 2>&1)"
if [ $? != 0 ]; then if [ $? != 0 ]; then
local_os_var="$(uname)" localOsVar="$(uname -v 2>&1)"
if [ $? != 0 ]; then
localOsVar="$(uname)"
fi
fi fi
fi fi
case $local_os_var in case $localOsVar in
*"Linux"*) *"Linux"*)
LOCAL_OS="Linux" LOCAL_OS="Linux"
;; ;;
@ -938,54 +1002,68 @@ function GetLocalOS {
*"Darwin"*) *"Darwin"*)
LOCAL_OS="MacOSX" LOCAL_OS="MacOSX"
;; ;;
*"BusyBox"*)
LOCAL_OS="BUSYBOX"
;;
*) *)
if [ "$IGNORE_OS_TYPE" == "yes" ]; then #DOC: Undocumented option if [ "$IGNORE_OS_TYPE" == "yes" ]; then #TODO(doc): Undocumented option
Logger "Running on unknown local OS [$local_os_var]." "WARN" Logger "Running on unknown local OS [$localOsVar]." "WARN"
return return
fi 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 exit 1
;; ;;
esac esac
Logger "Local OS: [$local_os_var]." "DEBUG" Logger "Local OS: [$localOsVar]." "DEBUG"
} }
#### MINIMAL-FUNCTION-SET END ####
function GetRemoteOS { function GetRemoteOS {
__CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG __CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
local cmd= local retval
local remote_os_var= local cmd
local remoteOsVar
if [ "$REMOTE_OPERATION" == "yes" ]; then if [ "$REMOTE_OPERATION" == "yes" ]; then
CheckConnectivity3rdPartyHosts CheckConnectivity3rdPartyHosts
CheckConnectivityRemoteHost 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" Logger "cmd: $cmd" "DEBUG"
eval "$cmd" & eval "$cmd" &
WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-1" true $KEEP_LOGGING WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-0" true $KEEP_LOGGING
retval=$? retval=$?
if [ $retval != 0 ]; then if [ $retval == 0 ]; then
cmd=$SSH_CMD' "uname -v" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1' remoteOsVar="BusyBox"
else
cmd=$SSH_CMD' "uname -spio" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1'
Logger "cmd: $cmd" "DEBUG" Logger "cmd: $cmd" "DEBUG"
eval "$cmd" & eval "$cmd" &
WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-2" true $KEEP_LOGGING WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-1" true $KEEP_LOGGING
retval=$? retval=$?
if [ $retval != 0 ]; then 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" Logger "cmd: $cmd" "DEBUG"
eval "$cmd" & eval "$cmd" &
WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-3" true $KEEP_LOGGING WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-2" true $KEEP_LOGGING
retval=$? retval=$?
if [ $retval != 0 ]; then 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
fi fi
remoteOsVar=$(cat "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID")
fi fi
remote_os_var=$(cat "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID") case $remoteOsVar in
case $remote_os_var in
*"Linux"*) *"Linux"*)
REMOTE_OS="Linux" REMOTE_OS="Linux"
;; ;;
@ -998,27 +1076,30 @@ function GetRemoteOS {
*"Darwin"*) *"Darwin"*)
REMOTE_OS="MacOSX" REMOTE_OS="MacOSX"
;; ;;
*"BusyBox"*)
REMOTE_OS="BUSYBOX"
;;
*"ssh"*|*"SSH"*) *"ssh"*|*"SSH"*)
Logger "Cannot connect to remote system." "CRITICAL" Logger "Cannot connect to remote system." "CRITICAL"
exit 1 exit 1
;; ;;
*) *)
if [ "$IGNORE_OS_TYPE" == "yes" ]; then #DOC: Undocumented option 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 return
fi fi
Logger "Running on remote OS failed. Please report to the author if the OS is not supported." "CRITICAL" 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 exit 1
esac esac
Logger "Remote OS: [$remote_os_var]." "DEBUG" Logger "Remote OS: [$remoteOsVar]." "DEBUG"
fi fi
} }
function RunLocalCommand { function RunLocalCommand {
local command="${1}" # Command to run 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 __CheckArguments 2 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
if [ $_DRYRUN == true ]; then if [ $_DRYRUN == true ]; then
@ -1028,7 +1109,7 @@ function RunLocalCommand {
Logger "Running command [$command] on local host." "NOTICE" Logger "Running command [$command] on local host." "NOTICE"
eval "$command" > "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" 2>&1 & 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=$? retval=$?
if [ $retval -eq 0 ]; then if [ $retval -eq 0 ]; then
Logger "Command succeded." "NOTICE" Logger "Command succeded." "NOTICE"
@ -1049,7 +1130,7 @@ function RunLocalCommand {
## Runs remote command $1 and waits for completition in $2 seconds ## Runs remote command $1 and waits for completition in $2 seconds
function RunRemoteCommand { function RunRemoteCommand {
local command="${1}" # Command to run 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 __CheckArguments 2 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
CheckConnectivity3rdPartyHosts CheckConnectivity3rdPartyHosts
@ -1063,7 +1144,7 @@ function RunRemoteCommand {
cmd=$SSH_CMD' "$command" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1' cmd=$SSH_CMD' "$command" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1'
Logger "cmd: $cmd" "DEBUG" Logger "cmd: $cmd" "DEBUG"
eval "$cmd" & eval "$cmd" &
WaitForTaskCompletion $! 0 $hard_max_time ${FUNCNAME[0]} true $KEEP_LOGGING WaitForTaskCompletion $! 0 $hardMaxTime ${FUNCNAME[0]} true $KEEP_LOGGING
retval=$? retval=$?
if [ $retval -eq 0 ]; then if [ $retval -eq 0 ]; then
Logger "Command succeded." "NOTICE" Logger "Command succeded." "NOTICE"
@ -1085,7 +1166,7 @@ function RunRemoteCommand {
function RunBeforeHook { function RunBeforeHook {
__CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG __CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
local pids= local pids
if [ "$LOCAL_RUN_BEFORE_CMD" != "" ]; then if [ "$LOCAL_RUN_BEFORE_CMD" != "" ]; then
RunLocalCommand "$LOCAL_RUN_BEFORE_CMD" $MAX_EXEC_TIME_PER_CMD_BEFORE & RunLocalCommand "$LOCAL_RUN_BEFORE_CMD" $MAX_EXEC_TIME_PER_CMD_BEFORE &
@ -1142,13 +1223,13 @@ function CheckConnectivityRemoteHost {
function CheckConnectivity3rdPartyHosts { function CheckConnectivity3rdPartyHosts {
__CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG __CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
local remote_3rd_party_success local remote3rdPartySuccess
local retval local retval
if [ "$_PARANOIA_DEBUG" != "yes" ]; then # Do not loose time in paranoia debug if [ "$_PARANOIA_DEBUG" != "yes" ]; then # Do not loose time in paranoia debug
if [ "$REMOTE_3RD_PARTY_HOSTS" != "" ]; then if [ "$REMOTE_3RD_PARTY_HOSTS" != "" ]; then
remote_3rd_party_success=false remote3rdPartySuccess=false
for i in $REMOTE_3RD_PARTY_HOSTS for i in $REMOTE_3RD_PARTY_HOSTS
do do
eval "$PING_CMD $i > /dev/null 2>&1" & eval "$PING_CMD $i > /dev/null 2>&1" &
@ -1157,11 +1238,11 @@ function CheckConnectivity3rdPartyHosts {
if [ $retval != 0 ]; then if [ $retval != 0 ]; then
Logger "Cannot ping 3rd party host [$i]. Return code [$retval]." "NOTICE" Logger "Cannot ping 3rd party host [$i]. Return code [$retval]." "NOTICE"
else else
remote_3rd_party_success=true remote3rdPartySuccess=true
fi fi
done done
if [ $remote_3rd_party_success == false ]; then if [ $remote3rdPartySuccess == false ]; then
Logger "No remote 3rd party host responded to ping. No internet ?" "WARN" Logger "No remote 3rd party host responded to ping. No internet ?" "WARN"
return 1 return 1
else else
@ -1224,7 +1305,7 @@ function __CheckArguments {
#__END_WITH_PARANOIA_DEBUG #__END_WITH_PARANOIA_DEBUG
function RsyncPatternsAdd { function RsyncPatternsAdd {
local pattern_type="${1}" # exclude or include local patternType="${1}" # exclude or include
local pattern="${2}" local pattern="${2}"
__CheckArguments 2 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG __CheckArguments 2 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
@ -1236,7 +1317,7 @@ function RsyncPatternsAdd {
while [ -n "$rest" ] while [ -n "$rest" ]
do do
# Take the string until first occurence until $PATH_SEPARATOR_CHAR # Take the string until first occurence until $PATH_SEPARATOR_CHAR
str=${rest%%;*} str=${rest%%;*} #TODO: replace ; with $PATH_SEPARATOR_CHAR
# Handle the last case # Handle the last case
if [ "$rest" = "${rest/$PATH_SEPARATOR_CHAR/}" ]; then if [ "$rest" = "${rest/$PATH_SEPARATOR_CHAR/}" ]; then
rest= rest=
@ -1245,26 +1326,26 @@ function RsyncPatternsAdd {
rest=${rest#*$PATH_SEPARATOR_CHAR} rest=${rest#*$PATH_SEPARATOR_CHAR}
fi fi
if [ "$RSYNC_PATTERNS" == "" ]; then if [ "$RSYNC_PATTERNS" == "" ]; then
RSYNC_PATTERNS="--"$pattern_type"=\"$str\"" RSYNC_PATTERNS="--"$patternType"=\"$str\""
else else
RSYNC_PATTERNS="$RSYNC_PATTERNS --"$pattern_type"=\"$str\"" RSYNC_PATTERNS="$RSYNC_PATTERNS --"$patternType"=\"$str\""
fi fi
done done
set +f set +f
} }
function RsyncPatternsFromAdd { function RsyncPatternsFromAdd {
local pattern_type="${1}" local patternType="${1}"
local pattern_from="${2}" local patternFrom="${2}"
__CheckArguments 2 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG __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 ## 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 if [ "$(basename $patternFrom)" == "$patternFrom" ]; then
pattern_from="$(dirname $CONFIG_FILE)/$pattern_from" patternFrom="$(dirname $CONFIG_FILE)/$patternFrom"
fi fi
if [ -e "$pattern_from" ]; then if [ -e "$patternFrom" ]; then
RSYNC_PATTERNS="$RSYNC_PATTERNS --"$pattern_type"-from=\"$pattern_from\"" RSYNC_PATTERNS="$RSYNC_PATTERNS --"$patternType"-from=\"$patternFrom\""
fi fi
} }
@ -1284,7 +1365,8 @@ function RsyncPatterns {
if [ "$RSYNC_INCLUDE_FROM" != "" ]; then if [ "$RSYNC_INCLUDE_FROM" != "" ]; then
RsyncPatternsFromAdd "include" "$RSYNC_INCLUDE_FROM" RsyncPatternsFromAdd "include" "$RSYNC_INCLUDE_FROM"
fi 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 if [ "$RSYNC_INCLUDE_PATTERN" != "" ]; then
RsyncPatternsAdd "include" "$RSYNC_INCLUDE_PATTERN" RsyncPatternsAdd "include" "$RSYNC_INCLUDE_PATTERN"
fi fi
@ -1427,9 +1509,20 @@ function PostInit {
__CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG __CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
# Define remote commands # Define remote commands
SSH_CMD="$(type -p ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $SSH_OPTS $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT" if [ -f "$SSH_RSA_PRIVATE_KEY" ]; then
SCP_CMD="$(type -p scp) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY -P $REMOTE_PORT" SSH_CMD="$(type -p ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $SSH_OPTS $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT"
RSYNC_SSH_CMD="$(type -p ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $SSH_OPTS -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 { function InitLocalOSSettings {
@ -1447,12 +1540,20 @@ function InitLocalOSSettings {
PING_CMD="ping -c 2 -i .2" PING_CMD="ping -c 2 -i .2"
fi 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 ## Stat command has different syntax on Linux and FreeBSD/MacOSX
if [ "$LOCAL_OS" == "MacOSX" ] || [ "$LOCAL_OS" == "BSD" ]; then if [ "$LOCAL_OS" == "MacOSX" ] || [ "$LOCAL_OS" == "BSD" ]; then
# Tested on BSD and Mac
STAT_CMD="stat -f \"%Sm\"" STAT_CMD="stat -f \"%Sm\""
STAT_CTIME_MTIME_CMD="stat -f %N;%c;%m" STAT_CTIME_MTIME_CMD="stat -f %N;%c;%m"
else 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" STAT_CTIME_MTIME_CMD="stat -c %n;%Z;%Y"
fi fi
} }
@ -1489,4 +1590,17 @@ function PrintIFS {
printf "IFS is: %q" "$IFS" 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 ## END Generic functions