mirror of
https://github.com/deajan/obackup.git
synced 2024-11-15 04:03:41 +01:00
Updated ofunctions
This commit is contained in:
parent
5a51855691
commit
ff9b9b632b
@ -31,7 +31,7 @@
|
|||||||
#### OFUNCTIONS MINI SUBSET ####
|
#### OFUNCTIONS MINI SUBSET ####
|
||||||
#### OFUNCTIONS MICRO SUBSET ####
|
#### OFUNCTIONS MICRO SUBSET ####
|
||||||
_OFUNCTIONS_VERSION=2.3.0-RC2
|
_OFUNCTIONS_VERSION=2.3.0-RC2
|
||||||
_OFUNCTIONS_BUILD=2018100205
|
_OFUNCTIONS_BUILD=2018110502
|
||||||
#### _OFUNCTIONS_BOOTSTRAP SUBSET ####
|
#### _OFUNCTIONS_BOOTSTRAP SUBSET ####
|
||||||
_OFUNCTIONS_BOOTSTRAP=true
|
_OFUNCTIONS_BOOTSTRAP=true
|
||||||
#### _OFUNCTIONS_BOOTSTRAP SUBSET END ####
|
#### _OFUNCTIONS_BOOTSTRAP SUBSET END ####
|
||||||
@ -104,7 +104,9 @@ else
|
|||||||
LOG_FILE="/tmp/$PROGRAM.log"
|
LOG_FILE="/tmp/$PROGRAM.log"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
#### RUN_DIR SUBSET ####
|
||||||
## Default directory where to store temporary run files
|
## Default directory where to store temporary run files
|
||||||
|
|
||||||
if [ -w /tmp ]; then
|
if [ -w /tmp ]; then
|
||||||
RUN_DIR=/tmp
|
RUN_DIR=/tmp
|
||||||
elif [ -w /var/tmp ]; then
|
elif [ -w /var/tmp ]; then
|
||||||
@ -113,6 +115,13 @@ else
|
|||||||
RUN_DIR=.
|
RUN_DIR=.
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
## Special note when remote target is on the same host as initiator (happens for unit tests): we'll have to differentiate RUN_DIR so remote CleanUp won't affect initiator.
|
||||||
|
if [ "$_REMOTE_EXECUTION" == true ]; then
|
||||||
|
mkdir -p "$RUN_DIR/$PROGRAM.remote"
|
||||||
|
RUN_DIR="$RUN_DIR/$PROGRAM.remote"
|
||||||
|
fi
|
||||||
|
#### RUN_DIR SUBSET END ####
|
||||||
|
|
||||||
#### PoorMansRandomGenerator SUBSET ####
|
#### PoorMansRandomGenerator SUBSET ####
|
||||||
# Get a random number on Windows BusyBox alike, also works on most Unixes that have dd, if dd is not found, then return $RANDOM
|
# Get a random number on Windows BusyBox alike, also works on most Unixes that have dd, if dd is not found, then return $RANDOM
|
||||||
function PoorMansRandomGenerator {
|
function PoorMansRandomGenerator {
|
||||||
@ -155,7 +164,7 @@ function PoorMansRandomGenerator {
|
|||||||
#### PoorMansRandomGenerator SUBSET END ####
|
#### PoorMansRandomGenerator SUBSET END ####
|
||||||
|
|
||||||
# Initial TSTMAP value before function declaration
|
# Initial TSTMAP value before function declaration
|
||||||
TSTAMP=$(date '+%Y%m%dT%H%M%S').$(PoorMansRandomGenerator 4)
|
TSTAMP=$(date '+%Y%m%dT%H%M%S').$(PoorMansRandomGenerator 5)
|
||||||
|
|
||||||
# Default alert attachment filename
|
# Default alert attachment filename
|
||||||
ALERT_LOG_FILE="$RUN_DIR/$PROGRAM.$SCRIPT_PID.$TSTAMP.last.log"
|
ALERT_LOG_FILE="$RUN_DIR/$PROGRAM.$SCRIPT_PID.$TSTAMP.last.log"
|
||||||
@ -205,6 +214,8 @@ function RemoteLogger {
|
|||||||
local level="${2}" # Log level
|
local level="${2}" # Log level
|
||||||
local retval="${3:-undef}" # optional return value of command
|
local retval="${3:-undef}" # optional return value of command
|
||||||
|
|
||||||
|
local prefix
|
||||||
|
|
||||||
if [ "$_LOGGER_PREFIX" == "time" ]; then
|
if [ "$_LOGGER_PREFIX" == "time" ]; then
|
||||||
prefix="TIME: $SECONDS - "
|
prefix="TIME: $SECONDS - "
|
||||||
elif [ "$_LOGGER_PREFIX" == "date" ]; then
|
elif [ "$_LOGGER_PREFIX" == "date" ]; then
|
||||||
@ -282,6 +293,8 @@ function Logger {
|
|||||||
local level="${2}" # Log level
|
local level="${2}" # Log level
|
||||||
local retval="${3:-undef}" # optional return value of command
|
local retval="${3:-undef}" # optional return value of command
|
||||||
|
|
||||||
|
local prefix
|
||||||
|
|
||||||
if [ "$_LOGGER_PREFIX" == "time" ]; then
|
if [ "$_LOGGER_PREFIX" == "time" ]; then
|
||||||
prefix="TIME: $SECONDS - "
|
prefix="TIME: $SECONDS - "
|
||||||
elif [ "$_LOGGER_PREFIX" == "date" ]; then
|
elif [ "$_LOGGER_PREFIX" == "date" ]; then
|
||||||
@ -347,6 +360,28 @@ function Logger {
|
|||||||
}
|
}
|
||||||
#### Logger SUBSET END ####
|
#### Logger SUBSET END ####
|
||||||
|
|
||||||
|
#### IsInteger SUBSET ####
|
||||||
|
# Function is busybox compatible since busybox ash does not understand direct regex, we use expr
|
||||||
|
function IsInteger {
|
||||||
|
local value="${1}"
|
||||||
|
|
||||||
|
if type expr > /dev/null 2>&1; then
|
||||||
|
expr "$value" : '^[0-9]\{1,\}$' > /dev/null 2>&1
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
echo 1
|
||||||
|
else
|
||||||
|
echo 0
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [[ $value =~ ^[0-9]+$ ]]; then
|
||||||
|
echo 1
|
||||||
|
else
|
||||||
|
echo 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
#### IsInteger SUBSET END ####
|
||||||
|
|
||||||
# Portable child (and grandchild) kill function tester under Linux, BSD and MacOS X
|
# Portable child (and grandchild) kill function tester under Linux, BSD and MacOS X
|
||||||
function KillChilds {
|
function KillChilds {
|
||||||
local pid="${1}" # Parent pid to kill childs
|
local pid="${1}" # Parent pid to kill childs
|
||||||
@ -416,8 +451,6 @@ function KillAllChilds {
|
|||||||
|
|
||||||
#### CleanUp SUBSET ####
|
#### CleanUp SUBSET ####
|
||||||
function CleanUp {
|
function CleanUp {
|
||||||
__CheckArguments 0 $# "$@" #__WITH_PARANOIA_DEBUG
|
|
||||||
|
|
||||||
if [ "$_DEBUG" != "yes" ]; then
|
if [ "$_DEBUG" != "yes" ]; then
|
||||||
rm -f "$RUN_DIR/$PROGRAM."*".$SCRIPT_PID.$TSTAMP"
|
rm -f "$RUN_DIR/$PROGRAM."*".$SCRIPT_PID.$TSTAMP"
|
||||||
# Fix for sed -i requiring backup extension for BSD & Mac (see all sed -i statements)
|
# Fix for sed -i requiring backup extension for BSD & Mac (see all sed -i statements)
|
||||||
@ -846,10 +879,11 @@ function ExecTasks {
|
|||||||
local minTimeBetweenRetries="${17:-300}" # Time (in seconds) between postponed command retries
|
local minTimeBetweenRetries="${17:-300}" # Time (in seconds) between postponed command retries
|
||||||
local validExitCodes="${18:-0}" # Semi colon separated list of valid main command exit codes which will not trigger errors
|
local validExitCodes="${18:-0}" # Semi colon separated list of valid main command exit codes which will not trigger errors
|
||||||
|
|
||||||
|
__CheckArguments 1-18 $# "$@" #__WITH_PARANOIA_DEBUG
|
||||||
|
|
||||||
local i
|
local i
|
||||||
|
|
||||||
Logger "${FUNCNAME[0]} called by [${FUNCNAME[0]} < ${FUNCNAME[1]} < ${FUNCNAME[2]} < ${FUNCNAME[3]} < ${FUNCNAME[4]} ...]." "PARANOIA_DEBUG" #__WITH_PARANOIA_DEBUG
|
Logger "${FUNCNAME[0]} id [$id] called by [${FUNCNAME[1]} < ${FUNCNAME[2]} < ${FUNCNAME[3]} < ${FUNCNAME[4]} < ${FUNCNAME[5]} < ${FUNCNAME[6]} ...]." "PARANOIA_DEBUG" #__WITH_PARANOIA_DEBUG
|
||||||
__CheckArguments 1-18 $# "$@" #__WITH_PARANOIA_DEBUG
|
|
||||||
|
|
||||||
# Since ExecTasks takes up to 17 arguments, do a quick preflight check in DEBUG mode
|
# Since ExecTasks takes up to 17 arguments, do a quick preflight check in DEBUG mode
|
||||||
if [ "$_DEBUG" == "yes" ]; then
|
if [ "$_DEBUG" == "yes" ]; then
|
||||||
@ -865,9 +899,6 @@ function ExecTasks {
|
|||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Change '-' to '_' in task id
|
|
||||||
id="${id/-/_}"
|
|
||||||
|
|
||||||
# Expand validExitCodes into array
|
# Expand validExitCodes into array
|
||||||
IFS=';' read -r -a validExitCodes <<< "$validExitCodes"
|
IFS=';' read -r -a validExitCodes <<< "$validExitCodes"
|
||||||
|
|
||||||
@ -904,16 +935,10 @@ function ExecTasks {
|
|||||||
local newPidsArray # New array of currently running pids for next iteration
|
local newPidsArray # New array of currently running pids for next iteration
|
||||||
local pidsTimeArray # Array containing execution begin time of pids
|
local pidsTimeArray # Array containing execution begin time of pids
|
||||||
local executeCommand # Boolean to check if currentCommand can be executed given a condition
|
local executeCommand # Boolean to check if currentCommand can be executed given a condition
|
||||||
|
|
||||||
local hasPids=false # Are any valable pids given to function ? #__WITH_PARANOIA_DEBUG
|
local hasPids=false # Are any valable pids given to function ? #__WITH_PARANOIA_DEBUG
|
||||||
|
|
||||||
local functionMode
|
local functionMode
|
||||||
|
local softAlert=false
|
||||||
if [ $counting == true ]; then
|
local failedPidsList # List containing failed pids with exit code separated by semicolons (eg : 2355:1;4534:2;2354:3)
|
||||||
local softAlert=false # Does a soft alert need to be triggered, if yes, send an alert once
|
|
||||||
else
|
|
||||||
local softAlert=false
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Initialise global variable
|
# Initialise global variable
|
||||||
eval "WAIT_FOR_TASK_COMPLETION_$id=\"\""
|
eval "WAIT_FOR_TASK_COMPLETION_$id=\"\""
|
||||||
@ -1074,17 +1099,17 @@ function ExecTasks {
|
|||||||
# Check for valid exit codes
|
# Check for valid exit codes
|
||||||
if [ $(ArrayContains $retval "${validExitCodes[@]}") -eq 0 ]; then
|
if [ $(ArrayContains $retval "${validExitCodes[@]}") -eq 0 ]; then
|
||||||
if [ $noErrorLogsAtAll != true ]; then
|
if [ $noErrorLogsAtAll != true ]; then
|
||||||
Logger "${FUNCNAME[0]} called by [$id] finished monitoring pid [$pid] with exitcode [$retval]." "DEBUG"
|
Logger "${FUNCNAME[0]} called by [$id] finished monitoring pid [$pid] with exitcode [$retval]." "ERROR"
|
||||||
if [ "$functionMode" == "ParallelExec" ]; then
|
if [ "$functionMode" == "ParallelExec" ]; then
|
||||||
Logger "Command was [${commandsArrayPid[$pid]}]." "ERROR"
|
Logger "Command was [${commandsArrayPid[$pid]}]." "ERROR"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
errorcount=$((errorcount+1))
|
errorcount=$((errorcount+1))
|
||||||
# Welcome to variable variable bash hell
|
# Welcome to variable variable bash hell
|
||||||
if [ "$(eval echo \"\$WAIT_FOR_TASK_COMPLETION_$id\")" == "" ]; then
|
if [ "$failedPidsList" == "" ]; then
|
||||||
eval "WAIT_FOR_TASK_COMPLETION_$id=\"$pid:$retval\""
|
failedPidsList="$pid:$retval"
|
||||||
else
|
else
|
||||||
eval "WAIT_FOR_TASK_COMPLETION_$id=\";$pid:$retval\""
|
failedPidsList="$failedPidsList;$pid:$retval"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
Logger "${FUNCNAME[0]} called by [$id] finished monitoring pid [$pid] with exitcode [$retval]." "DEBUG"
|
Logger "${FUNCNAME[0]} called by [$id] finished monitoring pid [$pid] with exitcode [$retval]." "DEBUG"
|
||||||
@ -1246,6 +1271,8 @@ function ExecTasks {
|
|||||||
# 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
|
||||||
# As we cannot return multiple values, a global variable WAIT_FOR_TASK_COMPLETION contains all pids with their return value
|
# As we cannot return multiple values, a global variable WAIT_FOR_TASK_COMPLETION contains all pids with their return value
|
||||||
|
|
||||||
|
eval "WAIT_FOR_TASK_COMPLETION_$id=\"$failedPidsList\""
|
||||||
|
|
||||||
if [ $mainItemCount -eq 1 ]; then
|
if [ $mainItemCount -eq 1 ]; then
|
||||||
return $retval
|
return $retval
|
||||||
else
|
else
|
||||||
@ -1296,7 +1323,7 @@ function IsNumeric {
|
|||||||
local value="${1}"
|
local value="${1}"
|
||||||
|
|
||||||
if type expr > /dev/null 2>&1; then
|
if type expr > /dev/null 2>&1; then
|
||||||
expr "$value" : "^[-+]\?[0-9]*\.\?[0-9]\+$" > /dev/null 2>&1
|
expr "$value" : '^[-+]\{0,1\}[0-9]*\.\{0,1\}[0-9]\{1,\}$' > /dev/null 2>&1
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
echo 1
|
echo 1
|
||||||
else
|
else
|
||||||
@ -1317,28 +1344,6 @@ function IsNumericExpand {
|
|||||||
echo $(IsNumeric "$value")
|
echo $(IsNumeric "$value")
|
||||||
}
|
}
|
||||||
|
|
||||||
#### IsInteger SUBSET ####
|
|
||||||
# Function is busybox compatible since busybox ash does not understand direct regex, we use expr
|
|
||||||
function IsInteger {
|
|
||||||
local value="${1}"
|
|
||||||
|
|
||||||
if type expr > /dev/null 2>&1; then
|
|
||||||
expr "$value" : "^[0-9]\+$" > /dev/null 2>&1
|
|
||||||
if [ $? -eq 0 ]; then
|
|
||||||
echo 1
|
|
||||||
else
|
|
||||||
echo 0
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
if [[ $value =~ ^[0-9]+$ ]]; then
|
|
||||||
echo 1
|
|
||||||
else
|
|
||||||
echo 0
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
#### IsInteger SUBSET END ####
|
|
||||||
|
|
||||||
#### HumanToNumeric SUBSET ####
|
#### HumanToNumeric SUBSET ####
|
||||||
# Converts human readable sizes into integer kilobyte sizes
|
# Converts human readable sizes into integer kilobyte sizes
|
||||||
# Usage numericSize="$(HumanToNumeric $humanSize)"
|
# Usage numericSize="$(HumanToNumeric $humanSize)"
|
||||||
@ -1444,6 +1449,8 @@ function GetLocalOS {
|
|||||||
# There is no good way to tell if currently running in BusyBox shell. Using sluggish way.
|
# There is 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
|
if ls --help 2>&1 | grep -i "BusyBox" > /dev/null; then
|
||||||
localOsVar="BusyBox"
|
localOsVar="BusyBox"
|
||||||
|
elif set -o | grep "winxp" > /dev/null; then
|
||||||
|
localOsVar="BusyBox-w32"
|
||||||
else
|
else
|
||||||
# Detecting the special ubuntu userland in Windows 10 bash
|
# Detecting the special ubuntu userland in Windows 10 bash
|
||||||
if grep -i Microsoft /proc/sys/kernel/osrelease > /dev/null 2>&1; then
|
if grep -i Microsoft /proc/sys/kernel/osrelease > /dev/null 2>&1; then
|
||||||
@ -1476,7 +1483,7 @@ function GetLocalOS {
|
|||||||
*"CYGWIN"*)
|
*"CYGWIN"*)
|
||||||
LOCAL_OS="Cygwin"
|
LOCAL_OS="Cygwin"
|
||||||
;;
|
;;
|
||||||
*"Microsoft"*)
|
*"Microsoft"*|*"MS/Windows"*)
|
||||||
LOCAL_OS="WinNT10"
|
LOCAL_OS="WinNT10"
|
||||||
;;
|
;;
|
||||||
*"Darwin"*)
|
*"Darwin"*)
|
||||||
@ -1507,7 +1514,8 @@ function GetLocalOS {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Get Host info for Windows
|
# Get Host info for Windows
|
||||||
if [ "$LOCAL_OS" == "msys" ] || [ "$LOCAL_OS" == "BusyBox" ] || [ "$LOCAL_OS" == "Cygwin" ] || [ "$LOCAL_OS" == "WinNT10" ]; then localOsVar="$(uname -a)"
|
if [ "$LOCAL_OS" == "msys" ] || [ "$LOCAL_OS" == "BusyBox" ] || [ "$LOCAL_OS" == "Cygwin" ] || [ "$LOCAL_OS" == "WinNT10" ]; then
|
||||||
|
localOsVar="$localOsVar $(uname -a)"
|
||||||
if [ "$PROGRAMW6432" != "" ]; then
|
if [ "$PROGRAMW6432" != "" ]; then
|
||||||
LOCAL_OS_BITNESS=64
|
LOCAL_OS_BITNESS=64
|
||||||
LOCAL_OS_FAMILY="Windows"
|
LOCAL_OS_FAMILY="Windows"
|
||||||
@ -1521,6 +1529,9 @@ function GetLocalOS {
|
|||||||
# Get Host info for Unix
|
# Get Host info for Unix
|
||||||
else
|
else
|
||||||
LOCAL_OS_FAMILY="Unix"
|
LOCAL_OS_FAMILY="Unix"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$LOCAL_OS_FAMILY" == "Unix" ]; then
|
||||||
if uname -m | grep '64' > /dev/null 2>&1; then
|
if uname -m | grep '64' > /dev/null 2>&1; then
|
||||||
LOCAL_OS_BITNESS=64
|
LOCAL_OS_BITNESS=64
|
||||||
else
|
else
|
||||||
@ -2139,14 +2150,14 @@ function InitLocalOSDependingSettings {
|
|||||||
function InitRemoteOSDependingSettings {
|
function InitRemoteOSDependingSettings {
|
||||||
__CheckArguments 0 $# "$@" #__WITH_PARANOIA_DEBUG
|
__CheckArguments 0 $# "$@" #__WITH_PARANOIA_DEBUG
|
||||||
|
|
||||||
if [ "$REMOTE_OS" == "msys" ] || [ "$LOCAL_OS" == "Cygwin" ]; then
|
if [ "$REMOTE_OS" == "msys" ] || [ "$REMOTE_OS" == "Cygwin" ]; then
|
||||||
REMOTE_FIND_CMD=$(dirname $BASH)/find
|
REMOTE_FIND_CMD=$(dirname $BASH)/find
|
||||||
else
|
else
|
||||||
REMOTE_FIND_CMD=find
|
REMOTE_FIND_CMD=find
|
||||||
fi
|
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 [ "$REMOTE_OS" == "MacOSX" ] || [ "$REMOTE_OS" == "BSD" ]; then
|
||||||
REMOTE_STAT_CMD="stat -f \"%Sm\""
|
REMOTE_STAT_CMD="stat -f \"%Sm\""
|
||||||
REMOTE_STAT_CTIME_MTIME_CMD="stat -f \\\"%N;%c;%m\\\""
|
REMOTE_STAT_CTIME_MTIME_CMD="stat -f \\\"%N;%c;%m\\\""
|
||||||
else
|
else
|
||||||
@ -2330,13 +2341,26 @@ function SetConfFileValue () {
|
|||||||
local value="${3}"
|
local value="${3}"
|
||||||
local separator="${4:-#}"
|
local separator="${4:-#}"
|
||||||
|
|
||||||
if grep "^$name=" "$file" > /dev/null; then
|
if [ -f "$file" ]; then
|
||||||
# Using -i.tmp for BSD compat
|
if grep "^$name=" "$file" > /dev/null 2>&1; then
|
||||||
sed -i.tmp "s$separator^$name=.*$separator$name=$value$separator" "$file"
|
# Using -i.tmp for BSD compat
|
||||||
rm -f "$file.tmp"
|
sed -i.tmp "s$separator^$name=.*$separator$name=$value$separator" "$file"
|
||||||
Logger "Set [$name] to [$value] in config file [$file]." "DEBUG"
|
if [ $? -ne 0 ]; then
|
||||||
|
Logger "Cannot update value [$name] to [$value] in config file [$file]." "ERROR"
|
||||||
|
fi
|
||||||
|
rm -f "$file.tmp"
|
||||||
|
Logger "Set [$name] to [$value] in config file [$file]." "DEBUG"
|
||||||
|
else
|
||||||
|
echo "$name=$value" >> "$file"
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
Logger "Cannot create value [$name] to [$value] in config file [$file]." "ERROR"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
Logger "Cannot set value [$name] to [$value] in config file [$file]." "ERROR"
|
echo "$name=$value" > "$file"
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
Logger "Config file [$file] does not exist. Failed to create it witn value [$name]." "ERROR"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
#### SetConfFileValue SUBSET END ####
|
#### SetConfFileValue SUBSET END ####
|
||||||
|
Loading…
Reference in New Issue
Block a user