X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=backupninja;h=6b888cb006e85609f04bbc2a369184510aaec687;hb=030f94dfdccfa6824e1f4e9c7bfdf713cb814d02;hp=921cb96754d3e76673b51ab080d2281bd359b9c4;hpb=27387185180f1ef7ecfbe44405e911a796becb77;p=matthijs%2Fupstream%2Fbackupninja.git diff --git a/backupninja b/backupninja index 921cb96..6b888cb 100755 --- a/backupninja +++ b/backupninja @@ -3,7 +3,7 @@ # B A C K U P N I N J A /()/ # `\| # -# Copyright (C) 2004 riseup.net -- property is theft. +# Copyright (C) 2004-05 riseup.net -- property is theft. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -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,62 @@ 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 + logmsg "$typestr$@" fi } +function logmsg() { + if [ -w "$logfile" ]; then + echo -e `date "+%h %d %H:%M:%S"` "$@" >> $logfile + 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,61 +154,262 @@ 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 + echo "Configuration files must not be group or world readable! Dying on file $file" + fatal "Configuration files must not be group or world readable! Dying on file $file" + fi + if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then + echo "Configuration files must be owned by root! Dying on file $file" + 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