X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fupstream%2Fbackupninja.git;a=blobdiff_plain;f=backupninja;h=b7d4a149e2e73683745e92f224faf899abd1c71f;hp=236fb67b1aa0bee5d572e0c3fcb0d6b2a3cbb21e;hb=7a8a4455bed618e18254dcadb319c69a95edd64d;hpb=ef78e14b29df0a9f010c3c2a07572bef5668d079 diff --git a/backupninja b/backupninja index 236fb67..b7d4a14 100755 --- a/backupninja +++ b/backupninja @@ -16,45 +16,37 @@ # GNU General Public License for more details. # -##################################################### -## DEFAULTS - -DEBUG=${DEBUG:=0} -CONFFILE="/etc/backupninja.conf" -USECOLOURS=1 - ##################################################### ## FUNCTIONS function setupcolors() { - if [ "$USECOLOURS" == 1 ] - then - BLUE="\033[34;01m" - GREEN="\033[32;01m" - YELLOW="\033[33;01m" - PURPLE="\033[35;01m" - RED="\033[31;01m" - OFF="\033[0m" - CYAN="\033[36;01m" - fi + BLUE="\033[34;01m" + GREEN="\033[32;01m" + YELLOW="\033[33;01m" + PURPLE="\033[35;01m" + RED="\033[31;01m" + OFF="\033[0m" + CYAN="\033[36;01m" + COLORS=($BLUE $GREEN $YELLOW $RED $PURPLE) } -function run() { - RUNERROR=0 - debug 0 "$@" - returnstring=`$@ 2>&1` - RUNERROR=$? - RUNERRORS=$[RUNERRORS+RUNERROR] - if [ "$RUNERROR" != 0 ]; then - debug 3 "Exitcode $RUNERROR returned when running: $@" - debug 3 "$returnstring" +function colorize() { + if [ "$usecolors" == "yes" ]; then + local typestr=`echo "$@" | sed 's/\(^[^:]*\).*$/\1/'` + [ "$typestr" == "Debug" ] && type=0 + [ "$typestr" == "Info" ] && type=1 + [ "$typestr" == "Warning" ] && type=2 + [ "$typestr" == "Error" ] && type=3 + [ "$typestr" == "Fatal" ] && type=4 + color=${COLORS[$type]} + endcolor=$OFF + echo -e "$color$@$endcolor" else - debug 0 "$returnstring" + echo -e "$@" fi - return $RUNERROR } -# We have the following debug levels: +# We have the following message levels: # 0 - debug - blue # 1 - normal messages - green # 2 - warnings - yellow @@ -66,33 +58,58 @@ function run() { # used to capture output from handlers echo_debug_msg=0 -function debug() { +usecolors=yes +function printmsg() { [ ${#@} -gt 1 ] || return - - TYPES=(Debug Info Warning Error Fatal) - COLOURS=($BLUE $GREEN $YELLOW $RED $PURPLE) + type=$1 - colour=${COLOURS[$type]} shift + if [ $type == 100 ]; then + typestr=`echo "$@" | sed 's/\(^[^:]*\).*$/\1/'` + [ "$typestr" == "Debug" ] && type=0 + [ "$typestr" == "Info" ] && type=1 + [ "$typestr" == "Warning" ] && type=2 + [ "$typestr" == "Error" ] && type=3 + [ "$typestr" == "Fatal" ] && type=4 + typestr="" + else + types=(Debug Info Warning Error Fatal) + typestr="${types[$type]}: " + fi + print=$[4-type] - if [ "$print" -lt "$loglevel" -o "$DEBUG" == 1 ]; then - if [ -z "$logfile" ]; then - echo -e "${colour}${TYPES[$type]}: $@${OFF}" >&2 - else - if [ "$DEBUG" == 1 -o "$type" == 4 ]; then - echo -e "${colour}${TYPES[$type]}: $@${OFF}" >&2 - fi - echo -e "${colour}${TYPES[$type]}: $@${OFF}" >> $logfile - fi + + if [ $echo_debug_msg == 1 ]; then + echo -e "$typestr$@" >&2 + elif [ $debug ]; then + colorize "$typestr$@" >&2 fi - if [ "$echo_debug_msg" != "0" -a "$type" -gt "1" ]; then - echo -e "${TYPES[$type]}: $@" + + if [ $print -lt $loglevel ]; then + if [ -w "$logfile" ]; then + colorize "$typestr$@" >> $logfile + fi fi } +function passthru() { + printmsg 100 "$@" +} +function debug() { + printmsg 0 "$@" +} +function info() { + printmsg 1 "$@" +} +function warning() { + printmsg 2 "$@" +} +function error() { + printmsg 3 "$@" +} function fatal() { - debug 4 "$@" + printmsg 4 "$@" exit 2 } @@ -133,139 +150,330 @@ function getconf() { eval $1='$ret' } +# +# enforces very strict permissions on configuration file $file. +# -##################################################### -## MAIN +function check_perms() { + local file=$1 + local perms=`ls -ld $file` + perms=${perms:4:6} + if [ "$perms" != "------" ]; then + fatal "Configuration files must not be group or world readable! Dying on file $file" + fi + if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then + fatal "Configuration files must be owned by root! Dying on file $file" + fi +} -## process command line options +# simple lowercase function +function tolower() { + echo "$1" | tr [:upper:] [:lower:] +} -if [ "$1" == "--help" ]; then - HELP=1;DEBUG=1;loglevel=4 -else - while getopts h,f:,d,t option - do - case "$option" in - h) HELP=1;DEBUG=1;loglevel=4;; - d) DEBUG=1;loglevel=4;; - f) CONFFILE="$OPTARG";; - t) test=1;DEBUG=1;; - esac - done -fi - -setupcolors +# simple to integer function +function toint() { + echo "$1" | tr [:alpha:] -d +} -## Print help +# +# function isnow(): returns 1 if the time/day passed as $1 matches +# the current time/day. +# +# format is at