X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=backupninja;h=962ff899543336621a37896cf9204fc10a9613b8;hb=cc8aa6a27a68a8d8df9c29a937ac435883abfc8f;hp=236fb67b1aa0bee5d572e0c3fcb0d6b2a3cbb21e;hpb=ef78e14b29df0a9f010c3c2a07572bef5668d079;p=matthijs%2Fupstream%2Fbackupninja.git diff --git a/backupninja b/backupninja index 236fb67..962ff89 100755 --- a/backupninja +++ b/backupninja @@ -16,45 +16,35 @@ # 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 -} - -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" - else - debug 0 "$returnstring" - fi - return $RUNERROR + 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" } -# We have the following debug levels: +#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" +# else +# debug 0 "$returnstring" +# fi +# return $RUNERROR +#} + +# We have the following message levels: # 0 - debug - blue # 1 - normal messages - green # 2 - warnings - yellow @@ -66,33 +56,52 @@ function run() { # used to capture output from handlers echo_debug_msg=0 -function debug() { +usecolor=yes +function printmsg() { [ ${#@} -gt 1 ] || return - TYPES=(Debug Info Warning Error Fatal) - COLOURS=($BLUE $GREEN $YELLOW $RED $PURPLE) + types=(Debug Info Warning Error Fatal) type=$1 - colour=${COLOURS[$type]} - shift 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 + typestr=${types[$type]} + if [ "$usecolor" == "yes" ]; then + colors=($BLUE $GREEN $YELLOW $RED $PURPLE) + color=${colors[$type]} + endcolor=$OFF fi + + shift + if [ "$echo_debug_msg" != "0" -a "$type" -gt "1" ]; then - echo -e "${TYPES[$type]}: $@" + echo -e "$typestr: $@" + fi + + if [ "$debug" == 1 ]; then + echo -e "${color}$typestr: $@${endcolor}" >&2 + fi + + if [ $print -lt $loglevel ]; then + if [ -w "$logfile" ]; then + echo -e "${color}$typestr: $@${endcolor}" >> $logfile + fi fi } +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,67 +142,204 @@ 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 +} + +# +# function isnow(): returns 1 if the time/day passed as $1 matches +# the current time/day. +# +# format is at