2
0
mirror of https://github.com/deajan/osync synced 2024-11-17 09:25:42 +00:00
osync/osync-batch.sh

192 lines
4.6 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"
2015-09-08 14:16:22 +00:00
PROGRAM_BUILD=2015090801
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
## Log file path
2015-09-08 14:08:14 +00:00
if [ -w /var/log ]; then
2015-09-08 14:16:22 +00:00
LOG_FILE=/var/log/osync-batch.log
2014-11-24 21:24:37 +00:00
else
2015-09-08 14:16:22 +00:00
LOG_FILE=./osync-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"
2014-11-24 21:24:37 +00:00
2015-09-08 14:16:22 +00:00
if [ $silent -eq 0 ]; then
echo -e "$value"
fi
}
function Logger {
local value="${1}" # What to log
local level="${2}" # Log level: DEBUG, NOTICE, WARN, ERROR, CRITIAL
# Special case in daemon mode we should timestamp instead of counting seconds
if [ $sync_on_changes -eq 1 ]; then
prefix="$(date) - "
else
prefix="TIME: $SECONDS - "
fi
if [ "$level" == "CRITICAL" ]; then
_logger "$prefix\e[41m$value\e[0m"
ERROR_ALERT=1
elif [ "$level" == "ERROR" ]; then
_logger "$prefix\e[91m$value\e[0m"
ERROR_ALERT=1
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-09-08 14:16:22 +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
if [ -f /usr/local/bin/osync.sh ]
then
OSYNC_EXECUTABLE=/usr/local/bin/osync.sh
elif [ -f ./osync.sh ]
then
OSYNC_EXECUTABLE=./osync.sh
else
Logger "Could not find osync.sh" "CRITICAL"
exit 1
fi
else
OSYNC_EXECUTABLE=$(type -p osync.sh)
fi
2015-07-19 10:39:44 +00:00
## Check for CONF_FILE_PATH
2015-09-08 14:08:14 +00:00
if [ ! -d "$CONF_FILE_PATH" ]; then
2015-09-08 14:16:22 +00:00
Logger "Cannot find conf file path $CONF_FILE_PATH" "CRITICAL"
2015-07-19 10:39:44 +00:00
Usage
fi
}
2014-11-24 21:24:37 +00:00
2015-09-08 14:08:14 +00:00
function Batch {
2014-11-24 21:24:37 +00:00
## Get list of .conf files
for i in $(ls $CONF_FILE_PATH/*.conf)
do
2015-09-08 14:08:14 +00:00
if [ "$RUN" == "" ]; then
2014-11-24 21:24:37 +00:00
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 ] || [ $MAX_EXECUTION_TIME -eq 0 ]) && [ "$RUN" != "" ] && [ $MAX_RERUNS -gt $RERUNS ]
2014-11-24 21:24:37 +00:00
do
2015-09-08 14:16:22 +00:00
Logger "Osync instances will be run for: $RUN" "NOTICE"
2014-11-24 21:24:37 +00:00
for i in $RUN
do
2015-04-01 21:19:07 +00:00
$OSYNC_EXECUTABLE "$i" $opts
2015-09-08 14:08:14 +00:00
if [ $? != 0 ]; then
2015-09-08 14:16:22 +00:00
Logger "Run instance $(basename $i) failed" "ERROR"
2015-09-08 14:08:14 +00:00
if [ "RUN_AGAIN" == "" ]; then
2014-11-24 21:24:37 +00:00
RUN_AGAIN="$i"
else
RUN_AGAIN=$RUN_AGAIN" $i"
fi
else
2015-09-08 14:16:22 +00:00
Logger "Run instance $(basename $i) succeed." "NOTICE"
2014-11-24 21:24:37 +00:00
fi
done
RUN="$RUN_AGAIN"
RUN_AGAIN=""
RERUNS=$(($RERUNS + 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 ""
echo "Batch script to sequentially run osync instances and rerun failed ones."
echo "Usage: osync-batch.sh [OPTIONS]"
echo ""
echo "[OPTIONS]"
2014-11-24 21:43:50 +00:00
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)"
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-08-25 13:40:22 +00:00
echo "--no-maxtime Run osync without honoring conf file defined timeouts"
2015-09-08 14:16:22 +00:00
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"
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
2015-09-08 14:16:22 +00:00
case $i in
--silent)
silent=1
2014-11-24 21:43:50 +00:00
opts=$opts" --silent"
2015-09-08 14:16:22 +00:00
;;
--dry)
dry=1
2014-11-24 21:43:50 +00:00
opts=$opts" --dry"
2015-09-08 14:16:22 +00:00
;;
--verbose)
verbose=1
opts=$opts" --verbose"
2014-11-24 21:43:50 +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
;;
--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
;;
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
CheckEnvironment
2015-09-08 14:16:22 +00:00
Logger "$(date) Osync batch run" "NOTICE"
2014-11-24 21:24:37 +00:00
Batch