2016-08-18 21:29:51 +00:00
#!/usr/bin/env bash
SUBPROGRAM = [ prgname]
PROGRAM = " $SUBPROGRAM -batch " # Batch program to run osync / obackup instances sequentially and rerun failed ones
AUTHOR = "(L) 2013-2016 by Orsiris de Jong"
CONTACT = "http://www.netpower.fr - ozy@netpower.fr"
2016-12-04 10:31:02 +00:00
PROGRAM_BUILD = 2016120401
2016-08-18 21:29:51 +00:00
## Runs an osync /obackup instance for every conf file found
## If an instance fails, run it again if time permits
if ! type " $BASH " > /dev/null; then
2016-12-11 19:54:40 +00:00
echo "Please run this script only with bash shell. Tested on bash >= 3.2"
exit 127
2016-08-18 21:29:51 +00:00
fi
## 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
## Specifies the number of total runs an instance may get
MAX_RUNS = 3
## Log file path
if [ -w /var/log ] ; then
LOG_FILE = /var/log/$SUBPROGRAM -batch.log
else
LOG_FILE = ./$SUBPROGRAM -batch.log
fi
# No need to edit under this line ##############################################################
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
prefix = " $( date) - "
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
}
function CheckEnvironment {
## osync / obackup executable full path can be set here if it cannot be found on the system
if ! type $SUBPROGRAM .sh > /dev/null 2>& 1
then
if [ -f /usr/local/bin/$SUBPROGRAM .sh ]
then
SUBPROGRAM_EXECUTABLE = /usr/local/bin/$SUBPROGRAM .sh
else
2016-08-29 16:02:15 +00:00
Logger " Could not find [/usr/local/bin/ $SUBPROGRAM .sh] " "CRITICAL"
2016-12-01 22:14:22 +00:00
( >& 2 echo " Could not find [/usr/local/bin/ $SUBPROGRAM .sh] " )
2016-08-18 21:29:51 +00:00
exit 1
fi
else
SUBPROGRAM_EXECUTABLE = $( type -p $SUBPROGRAM .sh)
fi
2016-11-24 13:48:12 +00:00
if [ " $CONF_FILE_PATH " = = "" ] ; then
Usage
fi
2016-08-18 21:29:51 +00:00
}
function Batch {
2016-12-01 21:18:03 +00:00
local runs = 1 # Number of batch runs
2016-08-18 21:29:51 +00:00
local runList # Actual conf file list to run
local runAgainList # List of failed conf files sto run again
local confFile
local result
2016-12-01 22:14:22 +00:00
local i
2016-08-18 21:29:51 +00:00
2016-12-01 22:14:22 +00:00
# Using -e because find will accept directories or files
if [ ! -e " $CONF_FILE_PATH " ] ; then
2016-08-18 21:29:51 +00:00
Logger " Cannot find conf file path [ $CONF_FILE_PATH ]. " "CRITICAL"
Usage
2016-12-01 22:14:22 +00:00
else
# Ugly hack to read files into an array while preserving special characters
runList = ( )
2016-12-03 08:34:26 +00:00
while IFS = read -d $'\0' -r file; do runList += ( " $file " ) ; done < <( find " $CONF_FILE_PATH " -maxdepth 1 -iname "*.conf" -print0)
2016-12-01 22:14:22 +00:00
while ( [ $MAX_EXECUTION_TIME -gt $SECONDS ] || [ $MAX_EXECUTION_TIME -eq 0 ] ) && [ " ${# runList [@] } " -gt 0 ] && [ $runs -le $MAX_RUNS ] ; do
runAgainList = ( )
Logger " Sequential run n° $runs of $SUBPROGRAM instances for: " "NOTICE"
for confFile in " ${ runList [@] } " ; do
2016-12-03 08:34:26 +00:00
Logger " $( basename $confFile ) " "NOTICE"
2016-12-01 22:14:22 +00:00
done
for confFile in " ${ runList [@] } " ; do
$SUBPROGRAM_EXECUTABLE " $confFile " --silent $opts &
wait $!
result = $?
if [ $result != 0 ] ; then
if [ $result = = 1 ] || [ $result = = 128 ] ; then # Do not handle exit code 128 because it is already handled here
Logger " Instance $( basename $confFile ) failed with exit code [ $result ]. " "ERROR"
runAgainList += ( " $confFile " )
elif [ $result = = 2 ] ; then
Logger " Instance $( basename $confFile ) finished with warnings. " "WARN"
2016-08-18 21:29:51 +00:00
fi
2016-12-01 22:14:22 +00:00
else
Logger " Instance $( basename $confFile ) succeed. " "NOTICE"
2016-08-18 21:29:51 +00:00
fi
2016-12-01 22:14:22 +00:00
done
runList = ( " ${ runAgainList [@] } " )
2017-02-08 07:34:24 +00:00
runs = $(( runs + 1 ))
2016-08-18 21:29:51 +00:00
done
2016-12-01 22:14:22 +00:00
fi
2016-08-18 21:29:51 +00:00
}
function Usage {
echo " $PROGRAM $PROGRAM_BUILD "
2017-02-08 07:34:24 +00:00
echo " $AUTHOR "
echo " $CONTACT "
2016-08-18 21:29:51 +00:00
echo ""
echo "Batch script to sequentially run osync or obackup instances and rerun failed ones."
2016-11-24 13:43:05 +00:00
echo " Usage: $PROGRAM .sh [OPTIONS] [ $SUBPROGRAM OPTIONS] "
2016-08-18 21:29:51 +00:00
echo ""
echo "[OPTIONS]"
echo "--path=/path/to/conf Path to osync / obackup conf files, defaults to /etc/osync or /etc/obackup"
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"
2016-11-24 13:43:05 +00:00
echo " [ $SUBPROGRAM OPTIONS] "
echo " Specify whatever options $PROGRAM accepts. Example "
echo " $PROGRAM .sh --path=/etc/ $SUBPROGRAM --no-maxtime "
2016-11-24 13:48:12 +00:00
echo ""
echo "No output will be written to stdout/stderr."
echo " Verify log file in [ $LOG_FILE ]. "
2016-08-18 21:29:51 +00:00
exit 128
}
opts = ""
for i in " $@ "
do
case $i in
--path= *)
CONF_FILE_PATH = ${ i ##*= }
; ;
--max-runs= *)
MAX_RUNS = ${ i ##*= }
; ;
--max-exec-time= *)
MAX_EXECUTION_TIME = ${ i ##*= }
; ;
--help| -h| -?)
Usage
; ;
*)
2016-12-04 10:31:02 +00:00
opts = " $opts $i "
2016-08-18 21:29:51 +00:00
; ;
esac
done
CheckEnvironment
Logger " $( date) $SUBPROGRAM batch run " "NOTICE"
Batch