2
0
mirror of https://github.com/deajan/osync synced 2024-11-03 15:40:14 +00:00
osync/osync-batch.sh

150 lines
3.4 KiB
Bash
Raw Normal View History

2014-11-24 21:24:37 +00:00
#!/usr/bin/env bash
PROGRAM="Osync-batch" # Batch program to run osync instances sequentially and rerun failed ones
AUTHOR="(L) 2013-2014 by Orsiris \"Ozy\" de Jong"
CONTACT="http://www.netpower.fr/osync - ozy@netpower.fr"
PROGRAM_BUILD=0104201501
2014-11-24 21:24:37 +00:00
## Runs an osync instance for every conf file found
## If an instance fails, run it again if time permits
## Configuration file path. The path where all the osync conf files are, usually /etc/osync
CONF_FILE_PATH=/etc/osync
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
## Specifies the number of reruns an instance may get
MAX_RERUNS=3
2014-11-24 21:24:37 +00:00
## Osync executable full path can be set here if it cannot be found on the system
if ! type -p osync.sh > /dev/null 2>&1
then
OSYNC_EXECUTABLE=osync.sh
else
OSYNC_EXECUTABLE=$(type -p osync.sh)
fi
## Log file path
if [ -w /var/log ]
then
LOG_FILE=/var/log/osync-batch.log
else
LOG_FILE=./osync-batch.log
fi
# No need to edit under this line ##############################################################
function Log
{
prefix="TIME: $SECONDS - "
echo -e "$prefix$1" >> "$LOG_FILE"
if [ $silent -eq 0 ]
then
echo -e "$prefix$1"
fi
}
function Batch
{
## Get list of .conf files
for i in $(ls $CONF_FILE_PATH/*.conf)
do
if [ "$RUN" == "" ]
then
RUN="$i"
else
RUN=$RUN" $i"
fi
2014-11-24 21:43:50 +00:00
done
2014-11-24 21:24:37 +00:00
RERUNS=0
while [ $MAX_EXECUTION_TIME -gt $SECONDS ] && [ "$RUN" != "" ] && [ $MAX_RERUNS -gt $RERUNS ]
2014-11-24 21:24:37 +00:00
do
2014-11-24 21:43:50 +00:00
Log "Osync instances will be run for: $RUN"
2014-11-24 21:24:37 +00:00
for i in $RUN
do
$OSYNC_EXECUTABLE "$i" "$opts"
2014-11-24 21:24:37 +00:00
if [ $? != 0 ]
then
Log "Run instance $(basename $i) failed"
if [ "RUN_AGAIN" == "" ]
then
RUN_AGAIN="$i"
else
RUN_AGAIN=$RUN_AGAIN" $i"
fi
2014-11-24 21:43:50 +00:00
elif [ $verbose -eq 1 ]
then
2014-11-24 21:24:37 +00:00
Log "Run instance $(basename $i) succeed."
fi
done
RUN="$RUN_AGAIN"
RUN_AGAIN=""
RERUNS=$(($RERUNS + 1))
2014-11-24 21:24:37 +00:00
done
}
2014-11-24 21:43:50 +00:00
function Usage
{
echo "$PROGRAM $PROGRAM_BUILD"
echo $AUTHOR
echo $CONTACT
echo ""
echo "Batch script to sequentially run osync instances and rerun failed ones."
echo "Usage: osync-batch.sh [OPTIONS]"
echo ""
echo "[OPTIONS]"
echo "--path=/path/to/conf Path to osync conf files, defaults to /etc/osync"
echo "--max-reruns=X Number of runs max for failed instances, (defaults to 3)"
2014-11-24 21:43:50 +00:00
echo "--max-exec-time=X Retry failed instances only if max execution time not reached (defaults to 36000 seconds)"
echo "--dry Will run osync without actually doing anything; just testing"
echo "--silent Will run osync without any output to stdout, used for cron jobs"
echo "--verbose Increases output"
2015-03-29 15:57:06 +00:00
exit 128
2014-11-24 21:43:50 +00:00
}
2014-11-24 21:24:37 +00:00
silent=0
2014-11-24 21:43:50 +00:00
dry=0
verbose=0
opts=""
2014-11-24 21:24:37 +00:00
for i in "$@"
do
case $i in
--silent)
silent=1
2014-11-24 21:43:50 +00:00
opts=$opts" --silent"
;;
--dry)
dry=1
opts=$opts" --dry"
2014-11-24 21:24:37 +00:00
;;
2014-11-24 21:43:50 +00:00
--verbose)
verbose=1
opts=$opts" --verbose"
;;
2014-11-24 21:51:59 +00:00
--path=*)
CONF_FILE_PATH=${i##*=}
2014-11-24 21:43:50 +00:00
;;
--max-reruns=*)
MAX_RERUNS=${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
;;
--help|-h)
Usage
2015-03-29 15:57:06 +00:00
;;
*)
Log "Unknown param '$i'"
Usage
2014-11-24 21:43:50 +00:00
;;
2014-11-24 21:24:37 +00:00
esac
done
Log "$(date) Osync batch run"
Batch