1
0
mirror of https://github.com/deajan/obackup.git synced 2024-11-15 04:03:41 +01:00

Rebuilt targets

This commit is contained in:
deajan 2019-05-21 11:46:07 +02:00
parent 7edaa48c6a
commit c70f0d6bed
4 changed files with 201 additions and 163 deletions

View File

@ -7,7 +7,7 @@ PROGRAM="obackup"
AUTHOR="(C) 2013-2019 by Orsiris de Jong" AUTHOR="(C) 2013-2019 by Orsiris de Jong"
CONTACT="http://www.netpower.fr/obackup - ozy@netpower.fr" CONTACT="http://www.netpower.fr/obackup - ozy@netpower.fr"
PROGRAM_VERSION=2.1-RC1 PROGRAM_VERSION=2.1-RC1
PROGRAM_BUILD=2018110602 PROGRAM_BUILD=2019052001
IS_STABLE=true IS_STABLE=true
#### Execution order #__WITH_PARANOIA_DEBUG #### Execution order #__WITH_PARANOIA_DEBUG
@ -35,7 +35,7 @@ IS_STABLE=true
# FilesBackup #__WITH_PARANOIA_DEBUG # FilesBackup #__WITH_PARANOIA_DEBUG
_OFUNCTIONS_VERSION=2.3.0-RC2 _OFUNCTIONS_VERSION=2.3.0-RC2
_OFUNCTIONS_BUILD=2019012801 _OFUNCTIONS_BUILD=2019031502
_OFUNCTIONS_BOOTSTRAP=true _OFUNCTIONS_BOOTSTRAP=true
if ! type "$BASH" > /dev/null; then if ! type "$BASH" > /dev/null; then
@ -186,8 +186,8 @@ function _Logger {
echo -e "$logValue" >> "$LOG_FILE" echo -e "$logValue" >> "$LOG_FILE"
# Build current log file for alerts if we have a sufficient environment # Build current log file for alerts if we have a sufficient environment
if [ "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" != "" ]; then if [ "$RUN_DIR/$PROGRAM" != "/" ]; then
echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" echo -e "$logValue" >> "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log"
fi fi
fi fi
@ -394,11 +394,11 @@ function KillChilds {
if kill -0 "$pid" > /dev/null 2>&1; then if kill -0 "$pid" > /dev/null 2>&1; then
kill -s TERM "$pid" kill -s TERM "$pid"
Logger "Sent SIGTERM to process [$pid]." "DEBUG" Logger "Sent SIGTERM to process [$pid]." "DEBUG"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
sleep 15 sleep 15
Logger "Sending SIGTERM to process [$pid] failed." "DEBUG" Logger "Sending SIGTERM to process [$pid] failed." "DEBUG"
kill -9 "$pid" kill -9 "$pid"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Sending SIGKILL to process [$pid] failed." "DEBUG" Logger "Sending SIGKILL to process [$pid] failed." "DEBUG"
return 1 return 1
fi # Simplify the return 0 logic here fi # Simplify the return 0 logic here
@ -424,7 +424,7 @@ function KillAllChilds {
IFS=';' read -a pidsArray <<< "$pids" IFS=';' read -a pidsArray <<< "$pids"
for pid in "${pidsArray[@]}"; do for pid in "${pidsArray[@]}"; do
KillChilds $pid $self KillChilds $pid $self
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
errorcount=$((errorcount+1)) errorcount=$((errorcount+1))
fi fi
done done
@ -461,10 +461,10 @@ function GenericTrapQuit {
# osync/obackup/pmocr script specific mail alert function, use SendEmail function for generic mail sending # osync/obackup/pmocr script specific mail alert function, use SendEmail function for generic mail sending
function SendAlert { function SendAlert {
local runAlert="${1:-false}" # Specifies if current message is sent while running or at the end of a run local runAlert="${1:-false}" # Specifies if current message is sent while running or at the end of a run
local attachment="${2:-true}" # Should we send the log file as attachment
__CheckArguments 0-1 $# "$@" #__WITH_PARANOIA_DEBUG __CheckArguments 0-2 $# "$@" #__WITH_PARANOIA_DEBUG
local attachment
local attachmentFile local attachmentFile
local subject local subject
local body local body
@ -478,14 +478,17 @@ function SendAlert {
return 0 return 0
fi fi
eval "cat \"$LOG_FILE\" $COMPRESSION_PROGRAM > $ALERT_LOG_FILE" if [ $attachment == true ]; then
if [ $? != 0 ]; then attachmentFile="$LOG_FILE"
attachment=false if type "$COMPRESSION_PROGRAM" > /dev/null 2>&1; then
else eval "cat \"$LOG_FILE\" \"$COMPRESSION_PROGRAM\" > \"$ALERT_LOG_FILE\""
attachment=true if [ $? -eq 0 ]; then
attachmentFile="$ALERT_LOG_FILE"
fi
fi
fi fi
body="$MAIL_ALERT_MSG"$'\n\n'"Last 1000 lines of log"$'\n\n'"$(tail -n 1000 $RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP)" body="$MAIL_ALERT_MSG"$'\n\n'"Last 1000 lines of current log"$'\n\n'"$(tail -n 1000 "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log")"
if [ $ERROR_ALERT == true ]; then if [ $ERROR_ALERT == true ]; then
subject="Error alert for $INSTANCE_ID" subject="Error alert for $INSTANCE_ID"
@ -501,10 +504,6 @@ function SendAlert {
subject="Finished run - $subject" subject="Finished run - $subject"
fi fi
if [ "$attachment" == true ]; then
attachmentFile="$ALERT_LOG_FILE"
fi
SendEmail "$subject" "$body" "$DESTINATION_MAILS" "$attachmentFile" "$SENDER_MAIL" "$SMTP_SERVER" "$SMTP_PORT" "$SMTP_ENCRYPTION" "$SMTP_USER" "$SMTP_PASSWORD" SendEmail "$subject" "$body" "$DESTINATION_MAILS" "$attachmentFile" "$SENDER_MAIL" "$SMTP_SERVER" "$SMTP_PORT" "$SMTP_ENCRYPTION" "$SMTP_USER" "$SMTP_PASSWORD"
# Delete tmp log file # Delete tmp log file
@ -553,8 +552,7 @@ function SendEmail {
local i local i
if [ "${destinationMails}" != "" ]; then if [ "${destinationMails}" != "" ]; then
# Not quoted since we split at space character, and emails cannot contain spaces for i in "${destinationMails[@]}"; do
for i in ${destinationMails}; do
if [ $(CheckRFC822 "$i") -ne 1 ]; then if [ $(CheckRFC822 "$i") -ne 1 ]; then
Logger "Given email [$i] does not seem to be valid." "WARN" Logger "Given email [$i] does not seem to be valid." "WARN"
fi fi
@ -595,7 +593,7 @@ function SendEmail {
echo -e "Subject:$subject\r\n$message" | $(type -p sendmail) -f "$senderMail" -S "$smtpServer:$smtpPort" -au"$smtpUser" -ap"$smtpPassword" "$destinationMails" echo -e "Subject:$subject\r\n$message" | $(type -p sendmail) -f "$senderMail" -S "$smtpServer:$smtpPort" -au"$smtpUser" -ap"$smtpPassword" "$destinationMails"
fi fi
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot send alert mail via $(type -p sendmail) !!!" "WARN" Logger "Cannot send alert mail via $(type -p sendmail) !!!" "WARN"
# Do not bother try other mail systems with busybox # Do not bother try other mail systems with busybox
return 1 return 1
@ -611,7 +609,7 @@ function SendEmail {
if type mutt > /dev/null 2>&1 ; then if type mutt > /dev/null 2>&1 ; then
# We need to replace spaces with comma in order for mutt to be able to process multiple destinations # We need to replace spaces with comma in order for mutt to be able to process multiple destinations
echo "$message" | $(type -p mutt) -x -s "$subject" "${destinationMails// /,}" $attachment_command echo "$message" | $(type -p mutt) -x -s "$subject" "${destinationMails// /,}" $attachment_command
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot send mail via $(type -p mutt) !!!" "WARN" Logger "Cannot send mail via $(type -p mutt) !!!" "WARN"
else else
Logger "Sent mail using mutt." "NOTICE" Logger "Sent mail using mutt." "NOTICE"
@ -633,10 +631,10 @@ function SendEmail {
fi fi
echo "$message" | $(type -p mail) $attachment_command -s "$subject" "$destinationMails" echo "$message" | $(type -p mail) $attachment_command -s "$subject" "$destinationMails"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot send mail via $(type -p mail) with attachments !!!" "WARN" Logger "Cannot send mail via $(type -p mail) with attachments !!!" "WARN"
echo "$message" | $(type -p mail) -s "$subject" "$destinationMails" echo "$message" | $(type -p mail) -s "$subject" "$destinationMails"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot send mail via $(type -p mail) without attachments !!!" "WARN" Logger "Cannot send mail via $(type -p mail) without attachments !!!" "WARN"
else else
Logger "Sent mail using mail command without attachment." "NOTICE" Logger "Sent mail using mail command without attachment." "NOTICE"
@ -650,7 +648,7 @@ function SendEmail {
if type sendmail > /dev/null 2>&1 ; then if type sendmail > /dev/null 2>&1 ; then
echo -e "Subject:$subject\r\n$message" | $(type -p sendmail) "$destinationMails" echo -e "Subject:$subject\r\n$message" | $(type -p sendmail) "$destinationMails"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot send mail via $(type -p sendmail) !!!" "WARN" Logger "Cannot send mail via $(type -p sendmail) !!!" "WARN"
else else
Logger "Sent mail using sendmail command without attachment." "NOTICE" Logger "Sent mail using sendmail command without attachment." "NOTICE"
@ -684,7 +682,7 @@ function SendEmail {
auth_string="-auth -user \"$smtpUser\" -pass \"$smtpPassword\"" auth_string="-auth -user \"$smtpUser\" -pass \"$smtpPassword\""
fi fi
$(type mailsend.exe) -f "$senderMail" -t "$destinationMails" -sub "$subject" -M "$message" -attach "$attachment" -smtp "$smtpServer" -port "$smtpPort" $encryption_string $auth_string $(type mailsend.exe) -f "$senderMail" -t "$destinationMails" -sub "$subject" -M "$message" -attach "$attachment" -smtp "$smtpServer" -port "$smtpPort" $encryption_string $auth_string
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot send mail via $(type mailsend.exe) !!!" "WARN" Logger "Cannot send mail via $(type mailsend.exe) !!!" "WARN"
else else
Logger "Sent mail using mailsend.exe command with attachment." "NOTICE" Logger "Sent mail using mailsend.exe command with attachment." "NOTICE"
@ -695,7 +693,7 @@ function SendEmail {
# pfSense specific # pfSense specific
if [ -f /usr/local/bin/mail.php ]; then if [ -f /usr/local/bin/mail.php ]; then
echo "$message" | /usr/local/bin/mail.php -s="$subject" echo "$message" | /usr/local/bin/mail.php -s="$subject"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot send mail via /usr/local/bin/mail.php (pfsense) !!!" "WARN" Logger "Cannot send mail via /usr/local/bin/mail.php (pfsense) !!!" "WARN"
else else
Logger "Sent mail using pfSense mail.php." "NOTICE" Logger "Sent mail using pfSense mail.php." "NOTICE"
@ -721,7 +719,7 @@ function LoadConfigFile {
local configFile="${1}" local configFile="${1}"
local revisionRequired="${2}" local revisionRequired="${2}"
__CheckArguments 1 $# "$@" #__WITH_PARANOIA_DEBUG __CheckArguments 2 $# "$@" #__WITH_PARANOIA_DEBUG
local revisionPresent local revisionPresent
@ -732,11 +730,11 @@ function LoadConfigFile {
Logger "Wrong configuration file supplied [$configFile]. Cannot start." "CRITICAL" Logger "Wrong configuration file supplied [$configFile]. Cannot start." "CRITICAL"
exit 1 exit 1
else else
revisionPresent=$(GetConfFileValue "$configFile" "CONFIG_FILE_REVISION" true) revisionPresent="$(GetConfFileValue "$configFile" "CONFIG_FILE_REVISION" true)"
if [ "$(IsNumeric $revisionPresent)" -eq 0 ]; then if [ "$(IsNumeric "${revisionPresent%%.*}")" -eq 0 ]; then
Logger "Missing CONFIG_FILE_REVISION. Please provide a valid config file, or run the config update script." "WARN"
Logger "CONFIG_FILE_REVISION does not seem numeric [$revisionPresent]." "DEBUG" Logger "CONFIG_FILE_REVISION does not seem numeric [$revisionPresent]." "DEBUG"
fi elif [ "$revisionRequired" != "" ]; then
if [ "$revisionRequired" != "" ]; then
if [ $(VerComp "$revisionPresent" "$revisionRequired") -eq 2 ]; then if [ $(VerComp "$revisionPresent" "$revisionRequired") -eq 2 ]; then
Logger "Configuration file seems out of date. Required version [$revisionRequired]. Actual version [$revisionPresent]." "CRITICAL" Logger "Configuration file seems out of date. Required version [$revisionRequired]. Actual version [$revisionPresent]." "CRITICAL"
exit 1 exit 1
@ -989,13 +987,18 @@ function ExecTasks {
fi fi
if [ $keepLogging -ne 0 ]; then if [ $keepLogging -ne 0 ]; then
# This log solely exists for readability purposes before having next set of logs
if [ ${#pidsArray[@]} -eq $numberOfProcesses ] && [ $log_ttime -eq 0 ]; then
log_ttime=$exec_time
Logger "There are $((mainItemCount-counter+postponedItemCount)) / $mainItemCount tasks in the queue of which $postponedItemCount are postponed. Currently, ${#pidsArray[@]} tasks running with pids [$(joinString , ${pidsArray[@]})]." "NOTICE"
fi
if [ $(((exec_time + 1) % keepLogging)) -eq 0 ]; then if [ $(((exec_time + 1) % keepLogging)) -eq 0 ]; then
if [ $log_ttime -ne $exec_time ]; then # Fix when sleep time lower than 1 second if [ $log_ttime -ne $exec_time ]; then # Fix when sleep time lower than 1 second
log_ttime=$exec_time log_ttime=$exec_time
if [ $functionMode == "Wait" ]; then if [ $functionMode == "WaitForTaskCompletion" ]; then
Logger "Current tasks still running with pids [$(joinString , ${pidsArray[@]})]." "NOTICE" Logger "Current tasks still running with pids [$(joinString , ${pidsArray[@]})]." "NOTICE"
elif [ $functionMode == "ParallelExec" ]; then elif [ $functionMode == "ParallelExec" ]; then
Logger "There are $((mainItemCount-counter+postponedItemCount)) / $mainItemCount tasks in the queue. Currently, ${#pidsArray[@]} tasks running with pids [$(joinString , ${pidsArray[@]})]." "NOTICE" Logger "There are $((mainItemCount-counter+postponedItemCount)) / $mainItemCount tasks in the queue of which $postponedItemCount are postponed. Currently, ${#pidsArray[@]} tasks running with pids [$(joinString , ${pidsArray[@]})]." "NOTICE"
fi fi
fi fi
fi fi
@ -1015,7 +1018,7 @@ function ExecTasks {
fi fi
for pid in "${pidsArray[@]}"; do for pid in "${pidsArray[@]}"; do
KillChilds $pid true KillChilds $pid true
if [ $? == 0 ]; then if [ $? -eq 0 ]; then
Logger "Task with pid [$pid] stopped successfully." "NOTICE" Logger "Task with pid [$pid] stopped successfully." "NOTICE"
else else
if [ $noErrorLogsAtAll != true ]; then if [ $noErrorLogsAtAll != true ]; then
@ -1068,7 +1071,7 @@ function ExecTasks {
fi fi
fi fi
KillChilds $pid true KillChilds $pid true
if [ $? == 0 ]; then if [ $? -eq 0 ]; then
Logger "Command with pid [$pid] stopped successfully." "NOTICE" Logger "Command with pid [$pid] stopped successfully." "NOTICE"
else else
if [ $noErrorLogsAtAll != true ]; then if [ $noErrorLogsAtAll != true ]; then
@ -1096,7 +1099,7 @@ function ExecTasks {
Logger "Command was [${commandsArrayPid[$pid]}]." "ERROR" Logger "Command was [${commandsArrayPid[$pid]}]." "ERROR"
fi fi
if [ -f "${commandsArrayOutput[$pid]}" ]; then if [ -f "${commandsArrayOutput[$pid]}" ]; then
Logger "Command output was [\$(cat ${commandsArrayOutput[$pid]})\n]." "ERROR" Logger "Command output was [$(cat ${commandsArrayOutput[$pid]})\n]." "ERROR"
fi fi
fi fi
errorcount=$((errorcount+1)) errorcount=$((errorcount+1))
@ -1242,11 +1245,11 @@ function ExecTasks {
if [ $executeCommand == true ]; then if [ $executeCommand == true ]; then
Logger "Running command [$currentCommand]." "DEBUG" Logger "Running command [$currentCommand]." "DEBUG"
randomOutputName=$(date '+%Y%m%dT%H%M%S').$(PoorMansRandomGenerator 5) randomOutputName=$(date '+%Y%m%dT%H%M%S').$(PoorMansRandomGenerator 5)
eval "$currentCommand" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$randomOutputName.$SCRIPT_PID.$TSTAMP" 2>&1 & eval "$currentCommand" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$id.$pid.$randomOutputName.$SCRIPT_PID.$TSTAMP" 2>&1 &
pid=$! pid=$!
pidsArray+=($pid) pidsArray+=($pid)
commandsArrayPid[$pid]="$currentCommand" commandsArrayPid[$pid]="$currentCommand"
commandsArrayOutput[$pid]="$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$randomOutputName.$SCRIPT_PID.$TSTAMP" commandsArrayOutput[$pid]="$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$id.$pid.$randomOutputName.$SCRIPT_PID.$TSTAMP"
# Initialize pid execution time array # Initialize pid execution time array
pidsTimeArray[$pid]=0 pidsTimeArray[$pid]=0
else else
@ -1445,9 +1448,9 @@ function GetLocalOS {
localOsVar="Microsoft" localOsVar="Microsoft"
else else
localOsVar="$(uname -spior 2>&1)" localOsVar="$(uname -spior 2>&1)"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
localOsVar="$(uname -v 2>&1)" localOsVar="$(uname -v 2>&1)"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
localOsVar="$(uname)" localOsVar="$(uname)"
fi fi
fi fi
@ -1497,7 +1500,7 @@ function GetLocalOS {
localOsName=$(GetConfFileValue "/etc/os-release" "NAME" true) localOsName=$(GetConfFileValue "/etc/os-release" "NAME" true)
localOsVer=$(GetConfFileValue "/etc/os-release" "VERSION" true) localOsVer=$(GetConfFileValue "/etc/os-release" "VERSION" true)
elif [ "$LOCAL_OS" == "BusyBox" ]; then elif [ "$LOCAL_OS" == "BusyBox" ]; then
localOsVer=`ls --help 2>&1 | head -1 | cut -f2 -d' '` localOsVer=$(ls --help 2>&1 | head -1 | cut -f2 -d' ')
localOsName="BusyBox" localOsName="BusyBox"
fi fi
@ -1621,9 +1624,9 @@ function GetOs {
localOsVar="Microsoft" localOsVar="Microsoft"
else else
localOsVar="$(uname -spior 2>&1)" localOsVar="$(uname -spior 2>&1)"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
localOsVar="$(uname -v 2>&1)" localOsVar="$(uname -v 2>&1)"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
localOsVar="$(uname)" localOsVar="$(uname)"
fi fi
fi fi
@ -1670,7 +1673,7 @@ function GetOs {
GetOs GetOs
ENDSSH ENDSSH
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot connect to remote system [$REMOTE_HOST] port [$REMOTE_PORT]." "CRITICAL" Logger "Cannot connect to remote system [$REMOTE_HOST] port [$REMOTE_PORT]." "CRITICAL"
if [ -f "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" ]; then if [ -f "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" ]; then
Logger "$(cat "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP")" "ERROR" Logger "$(cat "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP")" "ERROR"
@ -1852,7 +1855,7 @@ function CheckConnectivityRemoteHost {
ExecTasks $! "${FUNCNAME[0]}" false 0 0 60 180 true $SLEEP_TIME $KEEP_LOGGING ExecTasks $! "${FUNCNAME[0]}" false 0 0 60 180 true $SLEEP_TIME $KEEP_LOGGING
#ExecTasks "${FUNCNAME[0]}" 0 0 60 180 $SLEEP_TIME $KEEP_LOGGING true true false false 1 $! #ExecTasks "${FUNCNAME[0]}" 0 0 60 180 $SLEEP_TIME $KEEP_LOGGING true true false false 1 $!
retval=$? retval=$?
if [ $retval != 0 ]; then if [ $retval -ne 0 ]; then
Logger "Cannot ping [$REMOTE_HOST]. Return code [$retval]." "WARN" Logger "Cannot ping [$REMOTE_HOST]. Return code [$retval]." "WARN"
return $retval return $retval
fi fi
@ -1877,7 +1880,7 @@ function CheckConnectivity3rdPartyHosts {
ExecTasks $! "${FUNCNAME[0]}" false 0 0 60 180 true $SLEEP_TIME $KEEP_LOGGING ExecTasks $! "${FUNCNAME[0]}" false 0 0 60 180 true $SLEEP_TIME $KEEP_LOGGING
#ExecTasks "${FUNCNAME[0]}" 0 0 180 360 $SLEEP_TIME $KEEP_LOGGING true true false false 1 $! #ExecTasks "${FUNCNAME[0]}" 0 0 180 360 $SLEEP_TIME $KEEP_LOGGING true true false false 1 $!
retval=$? retval=$?
if [ $retval != 0 ]; then if [ $retval -ne 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
remote3rdPartySuccess=true remote3rdPartySuccess=true
@ -2303,7 +2306,7 @@ function GetConfFileValue () {
local value local value
value=$(grep "^$name=" "$file") value=$(grep "^$name=" "$file")
if [ $? == 0 ]; then if [ $? -eq 0 ]; then
value="${value##*=}" value="${value##*=}"
echo "$value" echo "$value"
else else
@ -2834,8 +2837,8 @@ function _Logger {
echo -e "$logValue" >> "$LOG_FILE" echo -e "$logValue" >> "$LOG_FILE"
# Build current log file for alerts if we have a sufficient environment # Build current log file for alerts if we have a sufficient environment
if [ "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" != "" ]; then if [ "$RUN_DIR/$PROGRAM" != "/" ]; then
echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" echo -e "$logValue" >> "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log"
fi fi
fi fi
@ -3129,8 +3132,8 @@ function _Logger {
echo -e "$logValue" >> "$LOG_FILE" echo -e "$logValue" >> "$LOG_FILE"
# Build current log file for alerts if we have a sufficient environment # Build current log file for alerts if we have a sufficient environment
if [ "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" != "" ]; then if [ "$RUN_DIR/$PROGRAM" != "/" ]; then
echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" echo -e "$logValue" >> "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log"
fi fi
fi fi
@ -3334,8 +3337,8 @@ function _Logger {
echo -e "$logValue" >> "$LOG_FILE" echo -e "$logValue" >> "$LOG_FILE"
# Build current log file for alerts if we have a sufficient environment # Build current log file for alerts if we have a sufficient environment
if [ "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" != "" ]; then if [ "$RUN_DIR/$PROGRAM" != "/" ]; then
echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" echo -e "$logValue" >> "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log"
fi fi
fi fi
@ -3565,8 +3568,8 @@ function _Logger {
echo -e "$logValue" >> "$LOG_FILE" echo -e "$logValue" >> "$LOG_FILE"
# Build current log file for alerts if we have a sufficient environment # Build current log file for alerts if we have a sufficient environment
if [ "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" != "" ]; then if [ "$RUN_DIR/$PROGRAM" != "/" ]; then
echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" echo -e "$logValue" >> "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log"
fi fi
fi fi
@ -4536,8 +4539,8 @@ function _Logger {
echo -e "$logValue" >> "$LOG_FILE" echo -e "$logValue" >> "$LOG_FILE"
# Build current log file for alerts if we have a sufficient environment # Build current log file for alerts if we have a sufficient environment
if [ "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" != "" ]; then if [ "$RUN_DIR/$PROGRAM" != "/" ]; then
echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" echo -e "$logValue" >> "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log"
fi fi
fi fi
@ -4876,7 +4879,7 @@ function GetCommandlineArguments {
fi fi
for i in "$@"; do for i in "$@"; do
case $i in case "$i" in
--dry) --dry)
_DRYRUN=true _DRYRUN=true
;; ;;
@ -4948,6 +4951,7 @@ function GetCommandlineArguments {
} }
GetCommandlineArguments "$@" GetCommandlineArguments "$@"
if [ "$_DECRYPT_MODE" == true ]; then if [ "$_DECRYPT_MODE" == true ]; then
CheckCryptEnvironnment CheckCryptEnvironnment
GetLocalOS GetLocalOS
@ -4967,6 +4971,10 @@ if [ "$_ENCRYPT_MODE" == true ]; then
fi fi
LoadConfigFile "$1" LoadConfigFile "$1"
# Reload GetCommandlineArguments to override config file with runtime arguments
GetCommandlineArguments "$@"
if [ "$LOGFILE" == "" ]; then if [ "$LOGFILE" == "" ]; then
if [ -w /var/log ]; then if [ -w /var/log ]; then
LOG_FILE="/var/log/$PROGRAM.$INSTANCE_ID.log" LOG_FILE="/var/log/$PROGRAM.$INSTANCE_ID.log"

View File

@ -10,7 +10,7 @@ PROGRAM_BINARY=$PROGRAM".sh"
PROGRAM_BATCH=$PROGRAM"-batch.sh" PROGRAM_BATCH=$PROGRAM"-batch.sh"
SSH_FILTER="ssh_filter.sh" SSH_FILTER="ssh_filter.sh"
SCRIPT_BUILD=2019022601 SCRIPT_BUILD=2019052001
INSTANCE_ID="installer-$SCRIPT_BUILD" INSTANCE_ID="installer-$SCRIPT_BUILD"
## osync / obackup / pmocr / zsnap install script ## osync / obackup / pmocr / zsnap install script
@ -18,7 +18,7 @@ INSTANCE_ID="installer-$SCRIPT_BUILD"
## Please adapt this to fit your distro needs ## Please adapt this to fit your distro needs
_OFUNCTIONS_VERSION=2.3.0-RC2 _OFUNCTIONS_VERSION=2.3.0-RC2
_OFUNCTIONS_BUILD=2019012801 _OFUNCTIONS_BUILD=2019031502
_OFUNCTIONS_BOOTSTRAP=true _OFUNCTIONS_BOOTSTRAP=true
if ! type "$BASH" > /dev/null; then if ! type "$BASH" > /dev/null; then
@ -165,8 +165,8 @@ function _Logger {
echo -e "$logValue" >> "$LOG_FILE" echo -e "$logValue" >> "$LOG_FILE"
# Build current log file for alerts if we have a sufficient environment # Build current log file for alerts if we have a sufficient environment
if [ "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" != "" ]; then if [ "$RUN_DIR/$PROGRAM" != "/" ]; then
echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" echo -e "$logValue" >> "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log"
fi fi
fi fi
@ -362,11 +362,11 @@ function KillChilds {
if kill -0 "$pid" > /dev/null 2>&1; then if kill -0 "$pid" > /dev/null 2>&1; then
kill -s TERM "$pid" kill -s TERM "$pid"
Logger "Sent SIGTERM to process [$pid]." "DEBUG" Logger "Sent SIGTERM to process [$pid]." "DEBUG"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
sleep 15 sleep 15
Logger "Sending SIGTERM to process [$pid] failed." "DEBUG" Logger "Sending SIGTERM to process [$pid] failed." "DEBUG"
kill -9 "$pid" kill -9 "$pid"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Sending SIGKILL to process [$pid] failed." "DEBUG" Logger "Sending SIGKILL to process [$pid] failed." "DEBUG"
return 1 return 1
fi # Simplify the return 0 logic here fi # Simplify the return 0 logic here
@ -391,7 +391,7 @@ function KillAllChilds {
IFS=';' read -a pidsArray <<< "$pids" IFS=';' read -a pidsArray <<< "$pids"
for pid in "${pidsArray[@]}"; do for pid in "${pidsArray[@]}"; do
KillChilds $pid $self KillChilds $pid $self
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
errorcount=$((errorcount+1)) errorcount=$((errorcount+1))
fi fi
done done
@ -472,6 +472,12 @@ SERVICE_DIR_OPENRC=$FAKEROOT/etc/init.d
if [ "$PROGRAM" == "osync" ]; then if [ "$PROGRAM" == "osync" ]; then
SERVICE_NAME="osync-srv" SERVICE_NAME="osync-srv"
TARGET_HELPER_SERVICE_NAME="osync-target-helper-srv"
TARGET_HELPER_SERVICE_FILE_INIT="$TARGET_HELPER_SERVICE_NAME"
TARGET_HELPER_SERVICE_FILE_SYSTEMD_SYSTEM="$TARGET_HELPER_SERVICE_NAME@.service"
TARGET_HELPER_SERVICE_FILE_SYSTEMD_USER="$TARGET_HELPER_SERVICE_NAME@.service.user"
TARGET_HELPER_SERVICE_FILE_OPENRC="$TARGET_HELPER_SERVICE_NAME-openrc"
elif [ "$PROGRAM" == "pmocr" ]; then elif [ "$PROGRAM" == "pmocr" ]; then
SERVICE_NAME="pmocr-srv" SERVICE_NAME="pmocr-srv"
fi fi
@ -527,9 +533,9 @@ function GetLocalOS {
localOsVar="Microsoft" localOsVar="Microsoft"
else else
localOsVar="$(uname -spior 2>&1)" localOsVar="$(uname -spior 2>&1)"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
localOsVar="$(uname -v 2>&1)" localOsVar="$(uname -v 2>&1)"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
localOsVar="$(uname)" localOsVar="$(uname)"
fi fi
fi fi
@ -579,7 +585,7 @@ function GetLocalOS {
localOsName=$(GetConfFileValue "/etc/os-release" "NAME" true) localOsName=$(GetConfFileValue "/etc/os-release" "NAME" true)
localOsVer=$(GetConfFileValue "/etc/os-release" "VERSION" true) localOsVer=$(GetConfFileValue "/etc/os-release" "VERSION" true)
elif [ "$LOCAL_OS" == "BusyBox" ]; then elif [ "$LOCAL_OS" == "BusyBox" ]; then
localOsVer=`ls --help 2>&1 | head -1 | cut -f2 -d' '` localOsVer=$(ls --help 2>&1 | head -1 | cut -f2 -d' ')
localOsName="BusyBox" localOsName="BusyBox"
fi fi
@ -623,7 +629,7 @@ function GetConfFileValue () {
local value local value
value=$(grep "^$name=" "$file") value=$(grep "^$name=" "$file")
if [ $? == 0 ]; then if [ $? -eq 0 ]; then
value="${value##*=}" value="${value##*=}"
echo "$value" echo "$value"
else else
@ -829,6 +835,16 @@ function CopyServiceFiles {
CreateDir "$SERVICE_DIR_SYSTEMD_USER" CreateDir "$SERVICE_DIR_SYSTEMD_USER"
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_SYSTEMD_USER" "$SERVICE_FILE_SYSTEMD_USER" "$SERVICE_FILE_SYSTEMD_USER" "" "" "" true CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_SYSTEMD_USER" "$SERVICE_FILE_SYSTEMD_USER" "$SERVICE_FILE_SYSTEMD_USER" "" "" "" true
fi fi
if [ -f "$SCRIPT_PATH/$TARGET_HELPER_SERVICE_FILE_SYSTEMD_SYSTEM" ]; then
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_SYSTEMD_SYSTEM" "$TARGET_HELPER_SERVICE_FILE_SYSTEMD_SYSTEM" "$SERVICE_FILE_SYSTEMD_SYSTEM" "" "" "" true
Logger "Created optional service [$TARGET_HELPER_SERVICE_NAME] with same specifications as below." "NOTICE"
fi
if [ -f "$SCRIPT_PATH/$TARGET_HELPER_SERVICE_FILE_SYSTEMD_USER" ]; then
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_SYSTEMD_USER" "$TARGET_HELPER_SERVICE_FILE_SYSTEMD_USER" "$SERVICE_FILE_SYSTEMD_USER" "" "" "" true
fi
Logger "Created [$SERVICE_NAME] service in [$SERVICE_DIR_SYSTEMD_SYSTEM] and [$SERVICE_DIR_SYSTEMD_USER]." "NOTICE" Logger "Created [$SERVICE_NAME] service in [$SERVICE_DIR_SYSTEMD_SYSTEM] and [$SERVICE_DIR_SYSTEMD_USER]." "NOTICE"
Logger "Can be activated with [systemctl start SERVICE_NAME@instance.conf] where instance.conf is the name of the config file in $CONF_DIR." "NOTICE" Logger "Can be activated with [systemctl start SERVICE_NAME@instance.conf] where instance.conf is the name of the config file in $CONF_DIR." "NOTICE"
Logger "Can be enabled on boot with [systemctl enable $SERVICE_NAME@instance.conf]." "NOTICE" Logger "Can be enabled on boot with [systemctl enable $SERVICE_NAME@instance.conf]." "NOTICE"
@ -836,14 +852,20 @@ function CopyServiceFiles {
elif ([ "$init" == "initV" ] && [ -f "$SCRIPT_PATH/$SERVICE_FILE_INIT" ] && [ -d "$SERVICE_DIR_INIT" ]); then elif ([ "$init" == "initV" ] && [ -f "$SCRIPT_PATH/$SERVICE_FILE_INIT" ] && [ -d "$SERVICE_DIR_INIT" ]); then
#CreateDir "$SERVICE_DIR_INIT" #CreateDir "$SERVICE_DIR_INIT"
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_INIT" "$SERVICE_FILE_INIT" "$SERVICE_FILE_INIT" "755" "" "" true CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_INIT" "$SERVICE_FILE_INIT" "$SERVICE_FILE_INIT" "755" "" "" true
if [ -f "$SCRIPT_PATH/$TARGET_HELPER_SERVICE_FILE_INIT" ]; then
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_INIT" "$TARGET_HELPER_SERVICE_FILE_INIT" "$SERVICE_FILE_INIT" "755" "" "" true
Logger "Created optional service [$TARGET_HELPER_SERVICE_NAME] with same specifications as below." "NOTICE"
fi
Logger "Created [$SERVICE_NAME] service in [$SERVICE_DIR_INIT]." "NOTICE" Logger "Created [$SERVICE_NAME] service in [$SERVICE_DIR_INIT]." "NOTICE"
Logger "Can be activated with [service $SERVICE_FILE_INIT start]." "NOTICE" Logger "Can be activated with [service $SERVICE_FILE_INIT start]." "NOTICE"
Logger "Can be enabled on boot with [chkconfig $SERVICE_FILE_INIT on]." "NOTICE" Logger "Can be enabled on boot with [chkconfig $SERVICE_FILE_INIT on]." "NOTICE"
elif ([ "$init" == "openrc" ] && [ -f "$SCRIPT_PATH/$SERVICE_FILE_OPENRC" ] && [ -d "$SERVICE_DIR_OPENRC" ]); then elif ([ "$init" == "openrc" ] && [ -f "$SCRIPT_PATH/$SERVICE_FILE_OPENRC" ] && [ -d "$SERVICE_DIR_OPENRC" ]); then
# Rename service to usual service file # Rename service to usual service file
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_OPENRC" "$SERVICE_FILE_OPENRC" "$SERVICE_FILE_INIT" "755" "" "" true CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_OPENRC" "$SERVICE_FILE_OPENRC" "$SERVICE_FILE_INIT" "755" "" "" true
if [ -f "$SCRPT_PATH/$TARGET_HELPER_SERVICE_FILE_OPENRC" ]; then
CopyFile "$SCRIPT_PATH" "$TARGET_HELPER_SERVICE_DIR_OPENRC" "$SERVICE_FILE_OPENRC" "$SERVICE_FILE_INIT" "755" "" "" true
Logger "Created optional service [$TARGET_HELPER_SERVICE_NAME] with same specifications as below." "NOTICE"
fi
Logger "Created [$SERVICE_NAME] service in [$SERVICE_DIR_OPENRC]." "NOTICE" Logger "Created [$SERVICE_NAME] service in [$SERVICE_DIR_OPENRC]." "NOTICE"
Logger "Can be activated with [rc-update add $SERVICE_NAME.instance] where instance is a configuration file found in /etc/osync." "NOTICE" Logger "Can be activated with [rc-update add $SERVICE_NAME.instance] where instance is a configuration file found in /etc/osync." "NOTICE"
else else

View File

@ -57,8 +57,8 @@ function _Logger {
echo -e "$logValue" >> "$LOG_FILE" echo -e "$logValue" >> "$LOG_FILE"
# Build current log file for alerts if we have a sufficient environment # Build current log file for alerts if we have a sufficient environment
if [ "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" != "" ]; then if [ "$RUN_DIR/$PROGRAM" != "/" ]; then
echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" echo -e "$logValue" >> "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log"
fi fi
fi fi

View File

@ -7,12 +7,12 @@ PROGRAM="obackup"
AUTHOR="(C) 2013-2019 by Orsiris de Jong" AUTHOR="(C) 2013-2019 by Orsiris de Jong"
CONTACT="http://www.netpower.fr/obackup - ozy@netpower.fr" CONTACT="http://www.netpower.fr/obackup - ozy@netpower.fr"
PROGRAM_VERSION=2.1-RC1 PROGRAM_VERSION=2.1-RC1
PROGRAM_BUILD=2018110602 PROGRAM_BUILD=2019052001
IS_STABLE=true IS_STABLE=true
_OFUNCTIONS_VERSION=2.3.0-RC2 _OFUNCTIONS_VERSION=2.3.0-RC2
_OFUNCTIONS_BUILD=2019012801 _OFUNCTIONS_BUILD=2019031502
_OFUNCTIONS_BOOTSTRAP=true _OFUNCTIONS_BOOTSTRAP=true
if ! type "$BASH" > /dev/null; then if ! type "$BASH" > /dev/null; then
@ -159,8 +159,8 @@ function _Logger {
echo -e "$logValue" >> "$LOG_FILE" echo -e "$logValue" >> "$LOG_FILE"
# Build current log file for alerts if we have a sufficient environment # Build current log file for alerts if we have a sufficient environment
if [ "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" != "" ]; then if [ "$RUN_DIR/$PROGRAM" != "/" ]; then
echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" echo -e "$logValue" >> "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log"
fi fi
fi fi
@ -356,11 +356,11 @@ function KillChilds {
if kill -0 "$pid" > /dev/null 2>&1; then if kill -0 "$pid" > /dev/null 2>&1; then
kill -s TERM "$pid" kill -s TERM "$pid"
Logger "Sent SIGTERM to process [$pid]." "DEBUG" Logger "Sent SIGTERM to process [$pid]." "DEBUG"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
sleep 15 sleep 15
Logger "Sending SIGTERM to process [$pid] failed." "DEBUG" Logger "Sending SIGTERM to process [$pid] failed." "DEBUG"
kill -9 "$pid" kill -9 "$pid"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Sending SIGKILL to process [$pid] failed." "DEBUG" Logger "Sending SIGKILL to process [$pid] failed." "DEBUG"
return 1 return 1
fi # Simplify the return 0 logic here fi # Simplify the return 0 logic here
@ -385,7 +385,7 @@ function KillAllChilds {
IFS=';' read -a pidsArray <<< "$pids" IFS=';' read -a pidsArray <<< "$pids"
for pid in "${pidsArray[@]}"; do for pid in "${pidsArray[@]}"; do
KillChilds $pid $self KillChilds $pid $self
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
errorcount=$((errorcount+1)) errorcount=$((errorcount+1))
fi fi
done done
@ -422,9 +422,9 @@ function GenericTrapQuit {
# osync/obackup/pmocr script specific mail alert function, use SendEmail function for generic mail sending # osync/obackup/pmocr script specific mail alert function, use SendEmail function for generic mail sending
function SendAlert { function SendAlert {
local runAlert="${1:-false}" # Specifies if current message is sent while running or at the end of a run local runAlert="${1:-false}" # Specifies if current message is sent while running or at the end of a run
local attachment="${2:-true}" # Should we send the log file as attachment
local attachment
local attachmentFile local attachmentFile
local subject local subject
local body local body
@ -438,14 +438,17 @@ function SendAlert {
return 0 return 0
fi fi
eval "cat \"$LOG_FILE\" $COMPRESSION_PROGRAM > $ALERT_LOG_FILE" if [ $attachment == true ]; then
if [ $? != 0 ]; then attachmentFile="$LOG_FILE"
attachment=false if type "$COMPRESSION_PROGRAM" > /dev/null 2>&1; then
else eval "cat \"$LOG_FILE\" \"$COMPRESSION_PROGRAM\" > \"$ALERT_LOG_FILE\""
attachment=true if [ $? -eq 0 ]; then
attachmentFile="$ALERT_LOG_FILE"
fi
fi
fi fi
body="$MAIL_ALERT_MSG"$'\n\n'"Last 1000 lines of log"$'\n\n'"$(tail -n 1000 $RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP)" body="$MAIL_ALERT_MSG"$'\n\n'"Last 1000 lines of current log"$'\n\n'"$(tail -n 1000 "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log")"
if [ $ERROR_ALERT == true ]; then if [ $ERROR_ALERT == true ]; then
subject="Error alert for $INSTANCE_ID" subject="Error alert for $INSTANCE_ID"
@ -461,10 +464,6 @@ function SendAlert {
subject="Finished run - $subject" subject="Finished run - $subject"
fi fi
if [ "$attachment" == true ]; then
attachmentFile="$ALERT_LOG_FILE"
fi
SendEmail "$subject" "$body" "$DESTINATION_MAILS" "$attachmentFile" "$SENDER_MAIL" "$SMTP_SERVER" "$SMTP_PORT" "$SMTP_ENCRYPTION" "$SMTP_USER" "$SMTP_PASSWORD" SendEmail "$subject" "$body" "$DESTINATION_MAILS" "$attachmentFile" "$SENDER_MAIL" "$SMTP_SERVER" "$SMTP_PORT" "$SMTP_ENCRYPTION" "$SMTP_USER" "$SMTP_PASSWORD"
# Delete tmp log file # Delete tmp log file
@ -512,8 +511,7 @@ function SendEmail {
local i local i
if [ "${destinationMails}" != "" ]; then if [ "${destinationMails}" != "" ]; then
# Not quoted since we split at space character, and emails cannot contain spaces for i in "${destinationMails[@]}"; do
for i in ${destinationMails}; do
if [ $(CheckRFC822 "$i") -ne 1 ]; then if [ $(CheckRFC822 "$i") -ne 1 ]; then
Logger "Given email [$i] does not seem to be valid." "WARN" Logger "Given email [$i] does not seem to be valid." "WARN"
fi fi
@ -554,7 +552,7 @@ function SendEmail {
echo -e "Subject:$subject\r\n$message" | $(type -p sendmail) -f "$senderMail" -S "$smtpServer:$smtpPort" -au"$smtpUser" -ap"$smtpPassword" "$destinationMails" echo -e "Subject:$subject\r\n$message" | $(type -p sendmail) -f "$senderMail" -S "$smtpServer:$smtpPort" -au"$smtpUser" -ap"$smtpPassword" "$destinationMails"
fi fi
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot send alert mail via $(type -p sendmail) !!!" "WARN" Logger "Cannot send alert mail via $(type -p sendmail) !!!" "WARN"
# Do not bother try other mail systems with busybox # Do not bother try other mail systems with busybox
return 1 return 1
@ -570,7 +568,7 @@ function SendEmail {
if type mutt > /dev/null 2>&1 ; then if type mutt > /dev/null 2>&1 ; then
# We need to replace spaces with comma in order for mutt to be able to process multiple destinations # We need to replace spaces with comma in order for mutt to be able to process multiple destinations
echo "$message" | $(type -p mutt) -x -s "$subject" "${destinationMails// /,}" $attachment_command echo "$message" | $(type -p mutt) -x -s "$subject" "${destinationMails// /,}" $attachment_command
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot send mail via $(type -p mutt) !!!" "WARN" Logger "Cannot send mail via $(type -p mutt) !!!" "WARN"
else else
Logger "Sent mail using mutt." "NOTICE" Logger "Sent mail using mutt." "NOTICE"
@ -592,10 +590,10 @@ function SendEmail {
fi fi
echo "$message" | $(type -p mail) $attachment_command -s "$subject" "$destinationMails" echo "$message" | $(type -p mail) $attachment_command -s "$subject" "$destinationMails"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot send mail via $(type -p mail) with attachments !!!" "WARN" Logger "Cannot send mail via $(type -p mail) with attachments !!!" "WARN"
echo "$message" | $(type -p mail) -s "$subject" "$destinationMails" echo "$message" | $(type -p mail) -s "$subject" "$destinationMails"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot send mail via $(type -p mail) without attachments !!!" "WARN" Logger "Cannot send mail via $(type -p mail) without attachments !!!" "WARN"
else else
Logger "Sent mail using mail command without attachment." "NOTICE" Logger "Sent mail using mail command without attachment." "NOTICE"
@ -609,7 +607,7 @@ function SendEmail {
if type sendmail > /dev/null 2>&1 ; then if type sendmail > /dev/null 2>&1 ; then
echo -e "Subject:$subject\r\n$message" | $(type -p sendmail) "$destinationMails" echo -e "Subject:$subject\r\n$message" | $(type -p sendmail) "$destinationMails"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot send mail via $(type -p sendmail) !!!" "WARN" Logger "Cannot send mail via $(type -p sendmail) !!!" "WARN"
else else
Logger "Sent mail using sendmail command without attachment." "NOTICE" Logger "Sent mail using sendmail command without attachment." "NOTICE"
@ -643,7 +641,7 @@ function SendEmail {
auth_string="-auth -user \"$smtpUser\" -pass \"$smtpPassword\"" auth_string="-auth -user \"$smtpUser\" -pass \"$smtpPassword\""
fi fi
$(type mailsend.exe) -f "$senderMail" -t "$destinationMails" -sub "$subject" -M "$message" -attach "$attachment" -smtp "$smtpServer" -port "$smtpPort" $encryption_string $auth_string $(type mailsend.exe) -f "$senderMail" -t "$destinationMails" -sub "$subject" -M "$message" -attach "$attachment" -smtp "$smtpServer" -port "$smtpPort" $encryption_string $auth_string
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot send mail via $(type mailsend.exe) !!!" "WARN" Logger "Cannot send mail via $(type mailsend.exe) !!!" "WARN"
else else
Logger "Sent mail using mailsend.exe command with attachment." "NOTICE" Logger "Sent mail using mailsend.exe command with attachment." "NOTICE"
@ -654,7 +652,7 @@ function SendEmail {
# pfSense specific # pfSense specific
if [ -f /usr/local/bin/mail.php ]; then if [ -f /usr/local/bin/mail.php ]; then
echo "$message" | /usr/local/bin/mail.php -s="$subject" echo "$message" | /usr/local/bin/mail.php -s="$subject"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot send mail via /usr/local/bin/mail.php (pfsense) !!!" "WARN" Logger "Cannot send mail via /usr/local/bin/mail.php (pfsense) !!!" "WARN"
else else
Logger "Sent mail using pfSense mail.php." "NOTICE" Logger "Sent mail using pfSense mail.php." "NOTICE"
@ -690,11 +688,11 @@ function LoadConfigFile {
Logger "Wrong configuration file supplied [$configFile]. Cannot start." "CRITICAL" Logger "Wrong configuration file supplied [$configFile]. Cannot start." "CRITICAL"
exit 1 exit 1
else else
revisionPresent=$(GetConfFileValue "$configFile" "CONFIG_FILE_REVISION" true) revisionPresent="$(GetConfFileValue "$configFile" "CONFIG_FILE_REVISION" true)"
if [ "$(IsNumeric $revisionPresent)" -eq 0 ]; then if [ "$(IsNumeric "${revisionPresent%%.*}")" -eq 0 ]; then
Logger "Missing CONFIG_FILE_REVISION. Please provide a valid config file, or run the config update script." "WARN"
Logger "CONFIG_FILE_REVISION does not seem numeric [$revisionPresent]." "DEBUG" Logger "CONFIG_FILE_REVISION does not seem numeric [$revisionPresent]." "DEBUG"
fi elif [ "$revisionRequired" != "" ]; then
if [ "$revisionRequired" != "" ]; then
if [ $(VerComp "$revisionPresent" "$revisionRequired") -eq 2 ]; then if [ $(VerComp "$revisionPresent" "$revisionRequired") -eq 2 ]; then
Logger "Configuration file seems out of date. Required version [$revisionRequired]. Actual version [$revisionPresent]." "CRITICAL" Logger "Configuration file seems out of date. Required version [$revisionRequired]. Actual version [$revisionPresent]." "CRITICAL"
exit 1 exit 1
@ -927,13 +925,18 @@ function ExecTasks {
fi fi
if [ $keepLogging -ne 0 ]; then if [ $keepLogging -ne 0 ]; then
# This log solely exists for readability purposes before having next set of logs
if [ ${#pidsArray[@]} -eq $numberOfProcesses ] && [ $log_ttime -eq 0 ]; then
log_ttime=$exec_time
Logger "There are $((mainItemCount-counter+postponedItemCount)) / $mainItemCount tasks in the queue of which $postponedItemCount are postponed. Currently, ${#pidsArray[@]} tasks running with pids [$(joinString , ${pidsArray[@]})]." "NOTICE"
fi
if [ $(((exec_time + 1) % keepLogging)) -eq 0 ]; then if [ $(((exec_time + 1) % keepLogging)) -eq 0 ]; then
if [ $log_ttime -ne $exec_time ]; then # Fix when sleep time lower than 1 second if [ $log_ttime -ne $exec_time ]; then # Fix when sleep time lower than 1 second
log_ttime=$exec_time log_ttime=$exec_time
if [ $functionMode == "Wait" ]; then if [ $functionMode == "WaitForTaskCompletion" ]; then
Logger "Current tasks still running with pids [$(joinString , ${pidsArray[@]})]." "NOTICE" Logger "Current tasks still running with pids [$(joinString , ${pidsArray[@]})]." "NOTICE"
elif [ $functionMode == "ParallelExec" ]; then elif [ $functionMode == "ParallelExec" ]; then
Logger "There are $((mainItemCount-counter+postponedItemCount)) / $mainItemCount tasks in the queue. Currently, ${#pidsArray[@]} tasks running with pids [$(joinString , ${pidsArray[@]})]." "NOTICE" Logger "There are $((mainItemCount-counter+postponedItemCount)) / $mainItemCount tasks in the queue of which $postponedItemCount are postponed. Currently, ${#pidsArray[@]} tasks running with pids [$(joinString , ${pidsArray[@]})]." "NOTICE"
fi fi
fi fi
fi fi
@ -953,7 +956,7 @@ function ExecTasks {
fi fi
for pid in "${pidsArray[@]}"; do for pid in "${pidsArray[@]}"; do
KillChilds $pid true KillChilds $pid true
if [ $? == 0 ]; then if [ $? -eq 0 ]; then
Logger "Task with pid [$pid] stopped successfully." "NOTICE" Logger "Task with pid [$pid] stopped successfully." "NOTICE"
else else
if [ $noErrorLogsAtAll != true ]; then if [ $noErrorLogsAtAll != true ]; then
@ -1006,7 +1009,7 @@ function ExecTasks {
fi fi
fi fi
KillChilds $pid true KillChilds $pid true
if [ $? == 0 ]; then if [ $? -eq 0 ]; then
Logger "Command with pid [$pid] stopped successfully." "NOTICE" Logger "Command with pid [$pid] stopped successfully." "NOTICE"
else else
if [ $noErrorLogsAtAll != true ]; then if [ $noErrorLogsAtAll != true ]; then
@ -1034,7 +1037,7 @@ function ExecTasks {
Logger "Command was [${commandsArrayPid[$pid]}]." "ERROR" Logger "Command was [${commandsArrayPid[$pid]}]." "ERROR"
fi fi
if [ -f "${commandsArrayOutput[$pid]}" ]; then if [ -f "${commandsArrayOutput[$pid]}" ]; then
Logger "Command output was [\$(cat ${commandsArrayOutput[$pid]})\n]." "ERROR" Logger "Command output was [$(cat ${commandsArrayOutput[$pid]})\n]." "ERROR"
fi fi
fi fi
errorcount=$((errorcount+1)) errorcount=$((errorcount+1))
@ -1173,11 +1176,11 @@ function ExecTasks {
if [ $executeCommand == true ]; then if [ $executeCommand == true ]; then
Logger "Running command [$currentCommand]." "DEBUG" Logger "Running command [$currentCommand]." "DEBUG"
randomOutputName=$(date '+%Y%m%dT%H%M%S').$(PoorMansRandomGenerator 5) randomOutputName=$(date '+%Y%m%dT%H%M%S').$(PoorMansRandomGenerator 5)
eval "$currentCommand" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$randomOutputName.$SCRIPT_PID.$TSTAMP" 2>&1 & eval "$currentCommand" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$id.$pid.$randomOutputName.$SCRIPT_PID.$TSTAMP" 2>&1 &
pid=$! pid=$!
pidsArray+=($pid) pidsArray+=($pid)
commandsArrayPid[$pid]="$currentCommand" commandsArrayPid[$pid]="$currentCommand"
commandsArrayOutput[$pid]="$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$randomOutputName.$SCRIPT_PID.$TSTAMP" commandsArrayOutput[$pid]="$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$id.$pid.$randomOutputName.$SCRIPT_PID.$TSTAMP"
# Initialize pid execution time array # Initialize pid execution time array
pidsTimeArray[$pid]=0 pidsTimeArray[$pid]=0
else else
@ -1375,9 +1378,9 @@ function GetLocalOS {
localOsVar="Microsoft" localOsVar="Microsoft"
else else
localOsVar="$(uname -spior 2>&1)" localOsVar="$(uname -spior 2>&1)"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
localOsVar="$(uname -v 2>&1)" localOsVar="$(uname -v 2>&1)"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
localOsVar="$(uname)" localOsVar="$(uname)"
fi fi
fi fi
@ -1427,7 +1430,7 @@ function GetLocalOS {
localOsName=$(GetConfFileValue "/etc/os-release" "NAME" true) localOsName=$(GetConfFileValue "/etc/os-release" "NAME" true)
localOsVer=$(GetConfFileValue "/etc/os-release" "VERSION" true) localOsVer=$(GetConfFileValue "/etc/os-release" "VERSION" true)
elif [ "$LOCAL_OS" == "BusyBox" ]; then elif [ "$LOCAL_OS" == "BusyBox" ]; then
localOsVer=`ls --help 2>&1 | head -1 | cut -f2 -d' '` localOsVer=$(ls --help 2>&1 | head -1 | cut -f2 -d' ')
localOsName="BusyBox" localOsName="BusyBox"
fi fi
@ -1495,9 +1498,9 @@ function GetOs {
localOsVar="Microsoft" localOsVar="Microsoft"
else else
localOsVar="$(uname -spior 2>&1)" localOsVar="$(uname -spior 2>&1)"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
localOsVar="$(uname -v 2>&1)" localOsVar="$(uname -v 2>&1)"
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
localOsVar="$(uname)" localOsVar="$(uname)"
fi fi
fi fi
@ -1544,7 +1547,7 @@ function GetOs {
GetOs GetOs
ENDSSH ENDSSH
if [ $? != 0 ]; then if [ $? -ne 0 ]; then
Logger "Cannot connect to remote system [$REMOTE_HOST] port [$REMOTE_PORT]." "CRITICAL" Logger "Cannot connect to remote system [$REMOTE_HOST] port [$REMOTE_PORT]." "CRITICAL"
if [ -f "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" ]; then if [ -f "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" ]; then
Logger "$(cat "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP")" "ERROR" Logger "$(cat "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP")" "ERROR"
@ -1720,7 +1723,7 @@ function CheckConnectivityRemoteHost {
ExecTasks $! "${FUNCNAME[0]}" false 0 0 60 180 true $SLEEP_TIME $KEEP_LOGGING ExecTasks $! "${FUNCNAME[0]}" false 0 0 60 180 true $SLEEP_TIME $KEEP_LOGGING
#ExecTasks "${FUNCNAME[0]}" 0 0 60 180 $SLEEP_TIME $KEEP_LOGGING true true false false 1 $! #ExecTasks "${FUNCNAME[0]}" 0 0 60 180 $SLEEP_TIME $KEEP_LOGGING true true false false 1 $!
retval=$? retval=$?
if [ $retval != 0 ]; then if [ $retval -ne 0 ]; then
Logger "Cannot ping [$REMOTE_HOST]. Return code [$retval]." "WARN" Logger "Cannot ping [$REMOTE_HOST]. Return code [$retval]." "WARN"
return $retval return $retval
fi fi
@ -1742,7 +1745,7 @@ function CheckConnectivity3rdPartyHosts {
ExecTasks $! "${FUNCNAME[0]}" false 0 0 60 180 true $SLEEP_TIME $KEEP_LOGGING ExecTasks $! "${FUNCNAME[0]}" false 0 0 60 180 true $SLEEP_TIME $KEEP_LOGGING
#ExecTasks "${FUNCNAME[0]}" 0 0 180 360 $SLEEP_TIME $KEEP_LOGGING true true false false 1 $! #ExecTasks "${FUNCNAME[0]}" 0 0 180 360 $SLEEP_TIME $KEEP_LOGGING true true false false 1 $!
retval=$? retval=$?
if [ $retval != 0 ]; then if [ $retval -ne 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
remote3rdPartySuccess=true remote3rdPartySuccess=true
@ -2160,7 +2163,7 @@ function GetConfFileValue () {
local value local value
value=$(grep "^$name=" "$file") value=$(grep "^$name=" "$file")
if [ $? == 0 ]; then if [ $? -eq 0 ]; then
value="${value##*=}" value="${value##*=}"
echo "$value" echo "$value"
else else
@ -2679,8 +2682,8 @@ function _Logger {
echo -e "$logValue" >> "$LOG_FILE" echo -e "$logValue" >> "$LOG_FILE"
# Build current log file for alerts if we have a sufficient environment # Build current log file for alerts if we have a sufficient environment
if [ "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" != "" ]; then if [ "$RUN_DIR/$PROGRAM" != "/" ]; then
echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" echo -e "$logValue" >> "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log"
fi fi
fi fi
@ -2962,8 +2965,8 @@ function _Logger {
echo -e "$logValue" >> "$LOG_FILE" echo -e "$logValue" >> "$LOG_FILE"
# Build current log file for alerts if we have a sufficient environment # Build current log file for alerts if we have a sufficient environment
if [ "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" != "" ]; then if [ "$RUN_DIR/$PROGRAM" != "/" ]; then
echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" echo -e "$logValue" >> "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log"
fi fi
fi fi
@ -3155,8 +3158,8 @@ function _Logger {
echo -e "$logValue" >> "$LOG_FILE" echo -e "$logValue" >> "$LOG_FILE"
# Build current log file for alerts if we have a sufficient environment # Build current log file for alerts if we have a sufficient environment
if [ "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" != "" ]; then if [ "$RUN_DIR/$PROGRAM" != "/" ]; then
echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" echo -e "$logValue" >> "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log"
fi fi
fi fi
@ -3374,8 +3377,8 @@ function _Logger {
echo -e "$logValue" >> "$LOG_FILE" echo -e "$logValue" >> "$LOG_FILE"
# Build current log file for alerts if we have a sufficient environment # Build current log file for alerts if we have a sufficient environment
if [ "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" != "" ]; then if [ "$RUN_DIR/$PROGRAM" != "/" ]; then
echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" echo -e "$logValue" >> "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log"
fi fi
fi fi
@ -4323,8 +4326,8 @@ function _Logger {
echo -e "$logValue" >> "$LOG_FILE" echo -e "$logValue" >> "$LOG_FILE"
# Build current log file for alerts if we have a sufficient environment # Build current log file for alerts if we have a sufficient environment
if [ "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" != "" ]; then if [ "$RUN_DIR/$PROGRAM" != "/" ]; then
echo -e "$logValue" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" echo -e "$logValue" >> "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log"
fi fi
fi fi
@ -4654,7 +4657,7 @@ function GetCommandlineArguments {
fi fi
for i in "$@"; do for i in "$@"; do
case $i in case "$i" in
--dry) --dry)
_DRYRUN=true _DRYRUN=true
;; ;;
@ -4726,6 +4729,7 @@ function GetCommandlineArguments {
} }
GetCommandlineArguments "$@" GetCommandlineArguments "$@"
if [ "$_DECRYPT_MODE" == true ]; then if [ "$_DECRYPT_MODE" == true ]; then
CheckCryptEnvironnment CheckCryptEnvironnment
GetLocalOS GetLocalOS
@ -4745,6 +4749,10 @@ if [ "$_ENCRYPT_MODE" == true ]; then
fi fi
LoadConfigFile "$1" LoadConfigFile "$1"
# Reload GetCommandlineArguments to override config file with runtime arguments
GetCommandlineArguments "$@"
if [ "$LOGFILE" == "" ]; then if [ "$LOGFILE" == "" ]; then
if [ -w /var/log ]; then if [ -w /var/log ]; then
LOG_FILE="/var/log/$PROGRAM.$INSTANCE_ID.log" LOG_FILE="/var/log/$PROGRAM.$INSTANCE_ID.log"