2014-11-24 21:24:37 +00:00
#!/usr/bin/env bash
2015-11-12 00:37:43 +00:00
SUBPROGRAM = osync
PROGRAM = " $SUBPROGRAM -batch " # Batch program to run osync / obackup instances sequentially and rerun failed ones
2016-04-01 19:24:53 +00:00
AUTHOR = "(L) 2013-2016 by Orsiris de Jong"
2015-11-12 00:37:43 +00:00
CONTACT = "http://www.netpower.fr - ozy@netpower.fr"
2016-08-18 21:30:10 +00:00
PROGRAM_BUILD = 2016081704
2014-11-24 21:24:37 +00:00
2015-11-12 00:37:43 +00:00
## Runs an osync /obackup instance for every conf file found
2014-11-24 21:24:37 +00:00
## If an instance fails, run it again if time permits
2016-08-18 21:30:10 +00:00
if ! type " $BASH " > /dev/null; then
echo "Please run this script only with bash shell. Tested on bash >= 3.2"
exit 127
fi
2015-11-12 00:37:43 +00:00
## Configuration file path. The path where all the osync / obackup conf files are, usually /etc/osync or /etc/obackup
CONF_FILE_PATH = /etc/$SUBPROGRAM
2014-11-24 21:24:37 +00:00
## If maximum execution time is not reached, failed instances will be rerun. Max exec time is in seconds. Example is set to 10 hours.
MAX_EXECUTION_TIME = 36000
2016-08-18 21:30:10 +00:00
## Specifies the number of total runs an instance may get
MAX_RUNS = 3
2014-11-24 21:24:37 +00:00
## Log file path
2015-09-08 14:08:14 +00:00
if [ -w /var/log ] ; then
2015-11-12 00:37:43 +00:00
LOG_FILE = /var/log/$SUBPROGRAM -batch.log
2014-11-24 21:24:37 +00:00
else
2015-11-12 00:37:43 +00:00
LOG_FILE = ./$SUBPROGRAM -batch.log
2014-11-24 21:24:37 +00:00
fi
# No need to edit under this line ##############################################################
2015-09-08 14:16:22 +00:00
function _logger {
local value = " ${ 1 } " # What to log
echo -e " $value " >> " $LOG_FILE "
}
function Logger {
local value = " ${ 1 } " # What to log
local level = " ${ 2 } " # Log level: DEBUG, NOTICE, WARN, ERROR, CRITIAL
2015-09-28 12:47:13 +00:00
prefix = " $( date) - "
2015-09-08 14:16:22 +00:00
if [ " $level " = = "CRITICAL" ] ; then
_logger " $prefix \e[41m $value \e[0m "
elif [ " $level " = = "ERROR" ] ; then
_logger " $prefix \e[91m $value \e[0m "
elif [ " $level " = = "WARN" ] ; then
_logger " $prefix \e[93m $value \e[0m "
elif [ " $level " = = "NOTICE" ] ; then
_logger " $prefix $value "
elif [ " $level " = = "DEBUG" ] ; then
if [ " $DEBUG " = = "yes" ] ; then
_logger " $prefix $value "
fi
else
_logger "\e[41mLogger function called without proper loglevel.\e[0m"
_logger " $prefix $value "
fi
2014-11-24 21:24:37 +00:00
}
2015-09-08 14:08:14 +00:00
function CheckEnvironment {
2015-11-12 00:37:43 +00:00
## osync / obackup executable full path can be set here if it cannot be found on the system
2016-03-14 20:51:29 +00:00
if ! type $SUBPROGRAM .sh > /dev/null 2>& 1
2015-09-08 14:16:22 +00:00
then
2015-11-12 00:37:43 +00:00
if [ -f /usr/local/bin/$SUBPROGRAM .sh ]
2015-09-08 14:16:22 +00:00
then
2015-11-12 00:37:43 +00:00
SUBPROGRAM_EXECUTABLE = /usr/local/bin/$SUBPROGRAM .sh
2015-09-08 14:16:22 +00:00
else
2015-11-12 00:37:43 +00:00
Logger " Could not find $SUBPROGRAM .sh " "CRITICAL"
2015-09-08 14:16:22 +00:00
exit 1
fi
else
2015-11-12 00:37:43 +00:00
SUBPROGRAM_EXECUTABLE = $( type -p $SUBPROGRAM .sh)
2015-09-08 14:16:22 +00:00
fi
2016-08-18 21:30:10 +00:00
}
function Batch {
local runs = 0 # Number of batch runs
local runList # Actual conf file list to run
local runAgainList # List of failed conf files sto run again
local confFile
local result
2015-07-19 10:39:44 +00:00
## Check for CONF_FILE_PATH
2016-08-18 21:30:10 +00:00
if [ -d " $CONF_FILE_PATH " ] ; then
## Get list of .conf files
for confFile in $CONF_FILE_PATH /*.conf
do
if [ -f " $confFile " ] ; then
if [ " $runList " = = "" ] ; then
runList = " $confFile "
else
runList = $runList " $confFile "
fi
fi
done
elif [ -f " $CONF_FILE_PATH " ] && [ " ${ CONF_FILE_PATH ##*. } " = = "conf" ] ; then
runList = " $CONF_FILE_PATH "
2015-09-28 12:34:22 +00:00
fi
2014-11-24 21:24:37 +00:00
2016-08-18 21:30:10 +00:00
if [ " $runList " = = "" ] ; then
Logger " Cannot find conf file path [ $CONF_FILE_PATH ]. " "CRITICAL"
Usage
fi
2014-11-24 21:24:37 +00:00
2016-08-18 21:30:10 +00:00
while ( [ $MAX_EXECUTION_TIME -gt $SECONDS ] || [ $MAX_EXECUTION_TIME -eq 0 ] ) && [ " $runList " != "" ] && [ $MAX_RUNS -gt $runs ]
2014-11-24 21:24:37 +00:00
do
2016-08-18 21:30:10 +00:00
Logger " $SUBPROGRAM instances will be run for: $runList " "NOTICE"
for confFile in $runList
2014-11-24 21:24:37 +00:00
do
2016-08-18 21:30:10 +00:00
$SUBPROGRAM_EXECUTABLE " $confFile " $opts &
2015-09-29 08:28:00 +00:00
wait $!
2016-08-18 21:30:10 +00:00
result = $?
if [ $result != 0 ] ; then
if [ $result = = 1 ] || [ $result = = 128 ] ; then # Do not handle exit code 127 because it is already handled here
Logger " Run instance $( basename $confFile ) failed with exit code [ $result ]. " "ERROR"
if [ " $runAgainList " = = "" ] ; then
runAgainList = " $confFile "
else
runAgainList = $runAgainList " $confFile "
fi
elif [ $result = = 2 ] ; then
Logger " Run instance $( basename $confFile ) finished with warnings. " "WARN"
2014-11-24 21:24:37 +00:00
fi
2015-04-26 20:32:43 +00:00
else
2016-08-18 21:30:10 +00:00
Logger " Run instance $( basename $confFile ) succeed. " "NOTICE"
2014-11-24 21:24:37 +00:00
fi
done
2016-08-18 21:30:10 +00:00
runList = " $runAgainList "
runAgainList = ""
runs = $(( $runs + 1 ))
2014-11-24 21:24:37 +00:00
done
}
2015-09-08 14:08:14 +00:00
function Usage {
2015-09-08 14:16:22 +00:00
echo " $PROGRAM $PROGRAM_BUILD "
echo $AUTHOR
echo $CONTACT
echo ""
2015-11-12 00:37:43 +00:00
echo "Batch script to sequentially run osync or obackup instances and rerun failed ones."
echo " Usage: $SUBPROGRAM -batch.sh [OPTIONS] "
2015-09-08 14:16:22 +00:00
echo ""
echo "[OPTIONS]"
2015-11-12 00:37:43 +00:00
echo "--path=/path/to/conf Path to osync / obackup conf files, defaults to /etc/osync or /etc/obackup"
2016-08-18 21:30:10 +00:00
echo "--max-runs=X Number of max runs per instance, (defaults to 3)"
echo "--max-exec-time=X Retry failed instances only if max execution time not reached (defaults to 36000 seconds). Set to 0 to bypass execution time check"
2015-11-12 00:37:43 +00:00
echo "--no-maxtime Run osync / obackup without honoring conf file defined timeouts"
echo "--dry Will run osync / obackup without actually doing anything; just testing"
echo "--silent Will run osync / obackup without any output to stdout, used for cron jobs"
2015-09-08 14:16:22 +00:00
echo "--verbose Increases output"
exit 128
2014-11-24 21:43:50 +00:00
}
opts = ""
2014-11-24 21:24:37 +00:00
for i in " $@ "
do
2015-09-08 14:16:22 +00:00
case $i in
--silent)
2014-11-24 21:43:50 +00:00
opts = $opts " --silent"
2015-09-08 14:16:22 +00:00
; ;
--dry)
2014-11-24 21:43:50 +00:00
opts = $opts " --dry"
2015-09-08 14:16:22 +00:00
; ;
--verbose)
opts = $opts " --verbose"
2014-11-24 21:43:50 +00:00
; ;
2015-07-19 09:57:43 +00:00
--no-maxtime)
opts = $opts " --no-maxtime"
; ;
2014-11-24 21:51:59 +00:00
--path= *)
CONF_FILE_PATH = ${ i ##*= }
2014-11-24 21:43:50 +00:00
; ;
2016-08-18 21:30:10 +00:00
--max-runs= *)
MAX_RUNS = ${ i ##*= }
2014-11-24 21:43:50 +00:00
; ;
2014-11-24 21:51:59 +00:00
--max-exec-time= *)
2014-11-24 21:54:15 +00:00
MAX_EXECUTION_TIME = ${ i ##*= }
2014-11-24 21:43:50 +00:00
; ;
2015-08-25 13:40:22 +00:00
--help| -h| -?)
2014-11-24 21:43:50 +00:00
Usage
2015-03-29 15:57:06 +00:00
; ;
*)
2015-09-08 14:16:22 +00:00
Logger " Unknown param ' $i ' " "CRITICAL"
2015-03-29 15:57:06 +00:00
Usage
2014-11-24 21:43:50 +00:00
; ;
2014-11-24 21:24:37 +00:00
esac
done
2015-04-26 20:32:43 +00:00
CheckEnvironment
2015-11-12 00:37:43 +00:00
Logger " $( date) $SUBPROGRAM batch run " "NOTICE"
2014-11-24 21:24:37 +00:00
Batch