2015-04-24 21:19:30 +02:00
#!/usr/bin/env bash
2015-11-12 01:26:38 +01:00
SUBPROGRAM = obackup
PROGRAM = " $SUBPROGRAM -batch " # Batch program to run osync / obackup instances sequentially and rerun failed ones
2016-03-14 21:50:40 +01:00
AUTHOR = "(L) 2013-2016 by Orsiris de Jong"
2015-11-12 01:26:38 +01:00
CONTACT = "http://www.netpower.fr - ozy@netpower.fr"
2016-03-14 21:50:40 +01:00
PROGRAM_BUILD = 2016031301
2015-04-24 21:19:30 +02:00
2015-11-12 01:26:38 +01:00
## Runs an osync /obackup instance for every conf file found
2015-04-24 21:19:30 +02:00
## If an instance fails, run it again if time permits
2015-11-12 01:26:38 +01: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
2015-04-24 21:19:30 +02: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
## Specifies the number of reruns an instance may get
MAX_RERUNS = 3
## Log file path
2015-11-12 01:26:38 +01:00
if [ -w /var/log ] ; then
LOG_FILE = /var/log/$SUBPROGRAM -batch.log
2015-04-24 21:19:30 +02:00
else
2015-11-12 01:26:38 +01:00
LOG_FILE = ./$SUBPROGRAM -batch.log
2015-04-24 21:19:30 +02:00
fi
# No need to edit under this line ##############################################################
2015-11-12 01:26:38 +01:00
function _logger {
local value = " ${ 1 } " # What to log
echo -e " $value " >> " $LOG_FILE "
2015-04-24 21:19:30 +02:00
2015-11-12 01:26:38 +01:00
if [ $_SILENT -eq 0 ] ; then
echo -e " $value "
fi
2015-04-24 21:19:30 +02:00
}
2015-11-12 01:26:38 +01:00
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
2015-11-18 10:52:43 +01:00
if ! type $SUBPROGRAM .sh > /dev/null 2>& 1
2015-11-12 01:26:38 +01:00
then
if [ -f /usr/local/bin/$SUBPROGRAM .sh ]
then
SUBPROGRAM_EXECUTABLE = /usr/local/bin/$SUBPROGRAM .sh
else
Logger " Could not find $SUBPROGRAM .sh " "CRITICAL"
exit 1
fi
else
SUBPROGRAM_EXECUTABLE = $( type -p $SUBPROGRAM .sh)
fi
2015-08-25 15:41:00 +02:00
## Check for CONF_FILE_PATH
2015-11-12 01:26:38 +01:00
if [ ! -d " $CONF_FILE_PATH " ] ; then
Logger " Cannot find conf file path $CONF_FILE_PATH " "CRITICAL"
Usage
fi
2015-04-26 22:28:56 +02:00
}
2015-04-24 21:19:30 +02:00
2015-11-12 01:26:38 +01:00
function Batch {
2015-04-24 21:19:30 +02:00
## Get list of .conf files
2016-03-14 21:50:40 +01:00
for i in $CONF_FILE_PATH /*.conf
2015-04-24 21:19:30 +02:00
do
2015-11-12 01:26:38 +01:00
if [ " $RUN " = = "" ] ; then
2015-04-24 21:19:30 +02:00
RUN = " $i "
else
RUN = $RUN " $i "
fi
done
RERUNS = 0
2015-08-25 15:41:00 +02:00
while ( [ $MAX_EXECUTION_TIME -gt $SECONDS ] || [ $MAX_EXECUTION_TIME -eq 0 ] ) && [ " $RUN " != "" ] && [ $MAX_RERUNS -gt $RERUNS ]
2015-04-24 21:19:30 +02:00
do
2015-11-12 01:26:38 +01:00
Logger " $SUBPROGRAM instances will be run for: $RUN " "NOTICE"
2015-04-24 21:19:30 +02:00
for i in $RUN
do
2015-11-12 01:26:38 +01:00
$SUBPROGRAM_EXECUTABLE " $i " $opts &
wait $!
if [ $? != 0 ] ; then
Logger " Run instance $( basename $i ) failed " "ERROR"
2016-03-14 21:50:40 +01:00
if [ " $RUN_AGAIN " = = "" ] ; then
2015-04-24 21:19:30 +02:00
RUN_AGAIN = " $i "
else
RUN_AGAIN = $RUN_AGAIN " $i "
fi
2015-04-26 22:28:56 +02:00
else
2015-11-12 01:26:38 +01:00
Logger " Run instance $( basename $i ) succeed. " "NOTICE"
2015-04-24 21:19:30 +02:00
fi
done
RUN = " $RUN_AGAIN "
RUN_AGAIN = ""
RERUNS = $(( $RERUNS + 1 ))
done
}
2015-11-12 01:26:38 +01:00
function Usage {
echo " $PROGRAM $PROGRAM_BUILD "
echo $AUTHOR
echo $CONTACT
echo ""
echo "Batch script to sequentially run osync or obackup instances and rerun failed ones."
echo " Usage: $SUBPROGRAM -batch.sh [OPTIONS] "
echo ""
echo "[OPTIONS]"
echo "--path=/path/to/conf Path to osync / obackup conf files, defaults to /etc/osync or /etc/obackup"
2015-04-24 21:19:30 +02:00
echo "--max-reruns=X Number of runs max for failed instances, (defaults to 3)"
2015-11-12 01:26:38 +01:00
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."
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"
echo "--verbose Increases output"
exit 128
2015-04-24 21:19:30 +02:00
}
opts = ""
for i in " $@ "
do
2015-11-12 01:26:38 +01:00
case $i in
--silent)
2015-04-24 21:19:30 +02:00
opts = $opts " --silent"
2015-11-12 01:26:38 +01:00
; ;
--dry)
2015-04-24 21:19:30 +02:00
opts = $opts " --dry"
2015-11-12 01:26:38 +01:00
; ;
--verbose)
opts = $opts " --verbose"
2015-04-24 21:19:30 +02:00
; ;
2015-08-25 15:41:00 +02:00
--no-maxtime)
opts = $opts " --no-maxtime"
; ;
2015-04-24 21:19:30 +02:00
--path= *)
CONF_FILE_PATH = ${ i ##*= }
; ;
--max-reruns= *)
MAX_RERUNS = ${ i ##*= }
; ;
--max-exec-time= *)
MAX_EXECUTION_TIME = ${ i ##*= }
; ;
2015-08-25 15:41:00 +02:00
--help| -h| -?)
2015-04-24 21:19:30 +02:00
Usage
; ;
*)
2015-11-12 01:26:38 +01:00
Logger " Unknown param ' $i ' " "CRITICAL"
2015-04-24 21:19:30 +02:00
Usage
; ;
esac
done
2015-04-26 22:28:56 +02:00
CheckEnvironment
2015-11-12 01:26:38 +01:00
Logger " $( date) $SUBPROGRAM batch run " "NOTICE"
2015-04-24 21:19:30 +02:00
Batch