#!/bin/bash # |\_ # B A C K U P N I N J A /()/ # `\| # # Copyright (C) 2004 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 # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # 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 } # We have the following debug levels: # 0 - debug - blue # 1 - normal messages - green # 2 - warnings - yellow # 3 - errors - orange # 4 - fatal - red # First variable passed is the error level, all others are printed # if 1, echo out all warnings, errors, or fatal # used to capture output from handlers echo_debug_msg=0 function debug() { [ ${#@} -gt 1 ] || return TYPES=(Debug Info Warning Error Fatal) COLOURS=($BLUE $GREEN $YELLOW $RED $PURPLE) 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 fi if [ "$echo_debug_msg" != "0" -a "$type" -gt "1" ]; then echo -e "${TYPES[$type]}: $@" fi } function fatal() { debug 4 "$@" exit 2 } msgcount=0 function msg { messages[$msgcount]=$1 let "msgcount += 1" } function setfile() { CURRENT_CONF_FILE=$1 } function setsection() { CURRENT_SECTION=$1 } # # sets a global var with name equal to $1 # to the value of the configuration parameter $1 # $2 is the default. # function getconf() { CURRENT_PARAM=$1 ret=`awk -f $scriptdir/parseini S=$CURRENT_SECTION P=$CURRENT_PARAM $CURRENT_CONF_FILE` # if nothing is returned, set the default if [ "$ret" == "" -a "$2" != "" ]; then ret="$2" fi # replace * with %, so that it is not globbed. ret="${ret//\\*/__star__}" # this is weird, but single quotes are needed to # allow for returned values with spaces. $ret is still expanded # because it is in an 'eval' statement. eval $1='$ret' } # # enforces very strict permissions on configuration file $file. # 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 } # simple lowercase function function tolower() { echo "$1" | tr [:upper:] [:lower:] } # 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