Updated ofunctions with better busybox support

This commit is contained in:
deajan 2016-10-23 18:23:44 +02:00
parent 06d2171a31
commit bddd7c87da
1 changed files with 102 additions and 46 deletions

View File

@ -1,6 +1,6 @@
#### MINIMAL-FUNCTION-SET BEGIN #### #### MINIMAL-FUNCTION-SET BEGIN ####
## FUNC_BUILD=2016102303 ## FUNC_BUILD=2016102311
## 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:
@ -8,6 +8,8 @@
## INSTANCE_ID=program-instance-name ## INSTANCE_ID=program-instance-name
## _DEBUG=yes/no ## _DEBUG=yes/no
#TODO(high): Refactor GetRemoteOs into big oneliner for faster execution (if busybox else uname else uname -spio in one statement)
#TODO(high): Implement busybox support in SendEmail function
#TODO: Windows checks, check sendmail & mailsend #TODO: Windows checks, check sendmail & mailsend
if ! type "$BASH" > /dev/null; then if ! type "$BASH" > /dev/null; then
@ -267,7 +269,7 @@ function SendAlert {
fi fi
# <OSYNC SPECIFIC> # <OSYNC SPECIFIC>
if [ "$_QUICK_SYNC" -eq 2 ]; then if [ "$_QUICK_SYNC" == "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
@ -299,6 +301,23 @@ function SendAlert {
if [ "$mail_no_attachment" -eq 0 ]; then if [ "$mail_no_attachment" -eq 0 ]; then
attachment_command="-a $ALERT_LOG_FILE" attachment_command="-a $ALERT_LOG_FILE"
fi fi
if [ "$LOCAL_OS" == "BUSYBOX" ]; then
if type sendmail > /dev/null 2>&1; then
echo -e "Subject:$subject\r\n$body" | $(type -p sendmail) -f "$SENDER_MAIL" -S "$SMTP_SERVER:$SMTP_PORT" -au"$SMTP_USER" -ap"$SMTP_PASSWORD" $DESTINATION_MAILS
if [ $? != 0 ]; then
Logger "Cannot send alert mail via $(type -p sendmail) !!!" "WARN"
# Don't bother try other mail systems with busybox
return 1
else
return 0
fi
else
Logger "Sendmail not present. Won't send any mail" "WARN"
return 1
fi
fi
if type mutt > /dev/null 2>&1 ; then if type mutt > /dev/null 2>&1 ; then
echo "$body" | $(type -p mutt) -x -s "$subject" $DESTINATION_MAILS $attachment_command echo "$body" | $(type -p mutt) -x -s "$subject" $DESTINATION_MAILS $attachment_command
if [ $? != 0 ]; then if [ $? != 0 ]; then
@ -407,21 +426,21 @@ function SendAlert {
# SendEmail "subject" "Body text" "receiver@example.com receiver2@otherdomain.com" "/path/to/attachment.file" # SendEmail "subject" "Body text" "receiver@example.com receiver2@otherdomain.com" "/path/to/attachment.file"
# Usage (Windows, make sure you have mailsend.exe in executable path, see http://github.com/muquit/mailsend) # Usage (Windows, make sure you have mailsend.exe in executable path, see http://github.com/muquit/mailsend)
# attachment is optional but must be in windows format like "c:\\some\path\\my.file", or "" # attachment is optional but must be in windows format like "c:\\some\path\\my.file", or ""
# smtp_server.domain.tld is mandatory, as is smtp_port (should be 25, 465 or 587) # smtp_server.domain.tld is mandatory, as is smtpPort (should be 25, 465 or 587)
# encryption can be set to tls, ssl or none # encryption can be set to tls, ssl or none
# smtp_user and smtp_password are optional # smtpUser and smtpPassword are optional
# SendEmail "subject" "Body text" "receiver@example.com receiver2@otherdomain.com" "/path/to/attachment.file" "sender_email@example.com" "smtp_server.domain.tld" "smtp_port" "encryption" "smtp_user" "smtp_password" # SendEmail "subject" "Body text" "receiver@example.com receiver2@otherdomain.com" "/path/to/attachment.file" "senderEmail@example.com" "smtpServer.domain.tld" "smtpPort" "encryption" "smtpUser" "smtpPassword"
function SendEmail { function SendEmail {
local subject="${1}" local subject="${1}"
local message="${2}" local message="${2}"
local destination_mails="${3}" local destinationMails="${3}"
local attachment="${4}" local attachment="${4}"
local sender_email="${5}" local senderEmail="${5}"
local smtp_server="${6}" local smtpServer="${6}"
local smtp_port="${7}" local smtpPort="${7}"
local encryption="${8}" local encryption="${8}"
local smtp_user="${9}" local smtpUser="${9}"
local smtp_password="${10}" local smtpPassword="${10}"
# CheckArguments will report a warning that can be ignored if used in Windows with paranoia debug enabled # CheckArguments will report a warning that can be ignored if used in Windows with paranoia debug enabled
__CheckArguments 4 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG __CheckArguments 4 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
@ -439,8 +458,24 @@ function SendEmail {
mail_no_attachment=0 mail_no_attachment=0
fi fi
if [ "$LOCAL_OS" == "BUSYBOX" ]; then
if type sendmail > /dev/null 2>&1; then
echo -e "Subject:$subject\r\n$message" | $(type -p sendmail) -f "$senderEmail" -S "$smtpServer:$smtpPort" -au"$smtpUser" -ap"$smtpPassword" "$destinationMails"
if [ $? != 0 ]; then
Logger "Cannot send alert mail via $(type -p sendmail) !!!" "WARN"
# Don't bother try other mail systems with busybox
return 1
else
return 0
fi
else
Logger "Sendmail not present. Won't send any mail" "WARN"
return 1
fi
fi
if type mutt > /dev/null 2>&1 ; then if type mutt > /dev/null 2>&1 ; then
echo "$message" | $(type -p mutt) -x -s "$subject" "$destination_mails" $attachment_command echo "$message" | $(type -p mutt) -x -s "$subject" "$destinationMails" $attachment_command
if [ $? != 0 ]; then if [ $? != 0 ]; then
Logger "Cannot send mail via $(type -p mutt) !!!" "WARN" Logger "Cannot send mail via $(type -p mutt) !!!" "WARN"
else else
@ -457,10 +492,10 @@ function SendEmail {
else else
attachment_command="" attachment_command=""
fi fi
echo "$message" | $(type -p mail) $attachment_command -s "$subject" "$destination_mails" echo "$message" | $(type -p mail) $attachment_command -s "$subject" "$destinationMails"
if [ $? != 0 ]; then if [ $? != 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" "$destination_mails" echo "$message" | $(type -p mail) -s "$subject" "$destinationMails"
if [ $? != 0 ]; then if [ $? != 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
@ -474,7 +509,7 @@ function SendEmail {
fi fi
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) "$destination_mails" echo -e "Subject:$subject\r\n$message" | $(type -p sendmail) "$destinationMails"
if [ $? != 0 ]; then if [ $? != 0 ]; then
Logger "Cannot send mail via $(type -p sendmail) !!!" "WARN" Logger "Cannot send mail via $(type -p sendmail) !!!" "WARN"
else else
@ -485,17 +520,17 @@ function SendEmail {
# Windows specific # Windows specific
if type "mailsend.exe" > /dev/null 2>&1 ; then if type "mailsend.exe" > /dev/null 2>&1 ; then
if [ "$sender_email" == "" ]; then if [ "$senderEmail" == "" ]; then
Logger "Missing sender email." "ERROR" Logger "Missing sender email." "ERROR"
return 1 return 1
fi fi
if [ "$smtp_server" == "" ]; then if [ "$smtpServer" == "" ]; then
Logger "Missing smtp port." "ERROR" Logger "Missing smtp port." "ERROR"
return 1 return 1
fi fi
if [ "$smtp_port" == "" ]; then if [ "$smtpPort" == "" ]; then
Logger "Missing smtp port, assuming 25." "WARN" Logger "Missing smtp port, assuming 25." "WARN"
smtp_port=25 smtpPort=25
fi fi
if [ "$encryption" != "tls" ] && [ "$encryption" != "ssl" ] && [ "$encryption" != "none" ]; then if [ "$encryption" != "tls" ] && [ "$encryption" != "ssl" ] && [ "$encryption" != "none" ]; then
Logger "Bogus smtp encryption, assuming none." "WARN" Logger "Bogus smtp encryption, assuming none." "WARN"
@ -505,10 +540,10 @@ function SendEmail {
elif [ "$encryption" == "ssl" ]:; then elif [ "$encryption" == "ssl" ]:; then
encryption_string=-ssl encryption_string=-ssl
fi fi
if [ "$smtp_user" != "" ] && [ "$smtp_password" != "" ]; then if [ "$smtpUser" != "" ] && [ "$smtpPassword" != "" ]; then
auth_string="-auth -user \"$smtp_user\" -pass \"$smtp_password\"" auth_string="-auth -user \"$smtpUser\" -pass \"$smtpPassword\""
fi fi
$(type mailsend.exe) -f "$sender_email" -t "$destination_mails" -sub "$subject" -M "$message" -attach "$attachment" -smtp "$smtp_server" -port "$smtp_port" $encryption_string $auth_string $(type mailsend.exe) -f "$senderEmail" -t "$destinationMails" -sub "$subject" -M "$message" -attach "$attachment" -smtp "$smtpServer" -port "$smtpPort" $encryption_string $auth_string
if [ $? != 0 ]; then if [ $? != 0 ]; then
Logger "Cannot send mail via $(type mailsend.exe) !!!" "WARN" Logger "Cannot send mail via $(type mailsend.exe) !!!" "WARN"
else else
@ -1366,7 +1401,7 @@ function RsyncPatterns {
RsyncPatternsFromAdd "include" "$RSYNC_INCLUDE_FROM" RsyncPatternsFromAdd "include" "$RSYNC_INCLUDE_FROM"
fi fi
# Use default include first for quicksync runs # Use default include first for quicksync runs
elif [ "$RSYNC_PATTERN_FIRST" == "include" ] || [ $_QUICK_SYNC -eq 2 ]; then elif [ "$RSYNC_PATTERN_FIRST" == "include" ] || [ "$_QUICK_SYNC" == "2" ]; then
if [ "$RSYNC_INCLUDE_PATTERN" != "" ]; then if [ "$RSYNC_INCLUDE_PATTERN" != "" ]; then
RsyncPatternsAdd "include" "$RSYNC_INCLUDE_PATTERN" RsyncPatternsAdd "include" "$RSYNC_INCLUDE_PATTERN"
fi fi
@ -1387,6 +1422,8 @@ function RsyncPatterns {
function PreInit { function PreInit {
__CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG __CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
local compressionString
## SSH compression ## SSH compression
if [ "$SSH_COMPRESSION" != "no" ]; then if [ "$SSH_COMPRESSION" != "no" ]; then
SSH_COMP=-C SSH_COMP=-C
@ -1477,24 +1514,42 @@ function PreInit {
fi fi
## Set compression executable and extension ## Set compression executable and extension
if [ "$(IsInteger $COMPRESSION_LEVEL)" -eq 0 ]; then
COMPRESSION_LEVEL=3 COMPRESSION_LEVEL=3
fi
## Busybox fix (Termux xz command doesn't support compression at all)
if [ "$LOCAL_OS" == "BUSYBOX" ] || [ "$REMOTE_OS" == "BUSYBOX" ]; then
compressionString=""
if type gzip > /dev/null 2>&1
then
COMPRESSION_PROGRAM="| gzip -c$compressionString"
COMPRESSION_EXTENSION=.gz
# obackup specific
else
COMPRESSION_PROGRAM=
COMPRESSION_EXTENSION=
fi
else
compressionString=" -$COMPRESSION_LEVEL"
if type xz > /dev/null 2>&1 if type xz > /dev/null 2>&1
then then
COMPRESSION_PROGRAM="| xz -$COMPRESSION_LEVEL" COMPRESSION_PROGRAM="| xz -c$compressionString"
COMPRESSION_EXTENSION=.xz COMPRESSION_EXTENSION=.xz
elif type lzma > /dev/null 2>&1 elif type lzma > /dev/null 2>&1
then then
COMPRESSION_PROGRAM="| lzma -$COMPRESSION_LEVEL" COMPRESSION_PROGRAM="| lzma -c$compressionString"
COMPRESSION_EXTENSION=.lzma COMPRESSION_EXTENSION=.lzma
elif type pigz > /dev/null 2>&1 elif type pigz > /dev/null 2>&1
then then
COMPRESSION_PROGRAM="| pigz -$COMPRESSION_LEVEL" COMPRESSION_PROGRAM="| pigz -c$compressionString"
COMPRESSION_EXTENSION=.gz COMPRESSION_EXTENSION=.gz
# obackup specific # obackup specific
COMPRESSION_OPTIONS=--rsyncable COMPRESSION_OPTIONS=--rsyncable
elif type gzip > /dev/null 2>&1 elif type gzip > /dev/null 2>&1
then then
COMPRESSION_PROGRAM="| gzip -$COMPRESSION_LEVEL" COMPRESSION_PROGRAM="| gzip -c$compressionString"
COMPRESSION_EXTENSION=.gz COMPRESSION_EXTENSION=.gz
# obackup specific # obackup specific
COMPRESSION_OPTIONS=--rsyncable COMPRESSION_OPTIONS=--rsyncable
@ -1502,6 +1557,7 @@ function PreInit {
COMPRESSION_PROGRAM= COMPRESSION_PROGRAM=
COMPRESSION_EXTENSION= COMPRESSION_EXTENSION=
fi fi
fi
ALERT_LOG_FILE="$ALERT_LOG_FILE$COMPRESSION_EXTENSION" ALERT_LOG_FILE="$ALERT_LOG_FILE$COMPRESSION_EXTENSION"
} }