3 # B A C K U P N I N J A /()/
6 # Copyright (C) 2004 riseup.net -- property is theft.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
19 #####################################################
22 function setupcolors() {
30 COLORS=($BLUE $GREEN $YELLOW $RED $PURPLE)
34 if [ "$usecolors" == "yes" ]; then
35 local typestr=`echo "$@" | sed 's/\(^[^:]*\).*$/\1/'`
36 [ "$typestr" == "Debug" ] && type=0
37 [ "$typestr" == "Info" ] && type=1
38 [ "$typestr" == "Warning" ] && type=2
39 [ "$typestr" == "Error" ] && type=3
40 [ "$typestr" == "Fatal" ] && type=4
41 color=${COLORS[$type]}
43 echo -e "$color$@$endcolor"
49 # We have the following message levels:
51 # 1 - normal messages - green
52 # 2 - warnings - yellow
55 # First variable passed is the error level, all others are printed
57 # if 1, echo out all warnings, errors, or fatal
58 # used to capture output from handlers
64 [ ${#@} -gt 1 ] || return
68 if [ $type == 100 ]; then
69 typestr=`echo "$@" | sed 's/\(^[^:]*\).*$/\1/'`
70 [ "$typestr" == "Debug" ] && type=0
71 [ "$typestr" == "Info" ] && type=1
72 [ "$typestr" == "Warning" ] && type=2
73 [ "$typestr" == "Error" ] && type=3
74 [ "$typestr" == "Fatal" ] && type=4
77 types=(Debug Info Warning Error Fatal)
78 typestr="${types[$type]}: "
83 if [ $echo_debug_msg == 1 ]; then
84 echo -e "$typestr$@" >&2
86 colorize "$typestr$@" >&2
89 if [ $print -lt $loglevel ]; then
90 if [ -w "$logfile" ]; then
91 colorize "$typestr$@" >> $logfile
118 messages[$msgcount]=$1
126 function setsection() {
131 # sets a global var with name equal to $1
132 # to the value of the configuration parameter $1
138 ret=`awk -f $scriptdir/parseini S=$CURRENT_SECTION P=$CURRENT_PARAM $CURRENT_CONF_FILE`
139 # if nothing is returned, set the default
140 if [ "$ret" == "" -a "$2" != "" ]; then
144 # replace * with %, so that it is not globbed.
145 ret="${ret//\\*/__star__}"
147 # this is weird, but single quotes are needed to
148 # allow for returned values with spaces. $ret is still expanded
149 # because it is in an 'eval' statement.
154 # enforces very strict permissions on configuration file $file.
157 function check_perms() {
159 local perms=`ls -ld $file`
161 if [ "$perms" != "------" ]; then
162 echo "Configuration files must not be group or world readable! Dying on file $file"
163 fatal "Configuration files must not be group or world readable! Dying on file $file"
165 if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then
166 echo "Configuration files must be owned by root! Dying on file $file"
167 fatal "Configuration files must be owned by root! Dying on file $file"
171 # simple lowercase function
173 echo "$1" | tr [:upper:] [:lower:]
176 # simple to integer function
178 echo "$1" | tr [:alpha:] -d
182 # function isnow(): returns 1 if the time/day passed as $1 matches
183 # the current time/day.
185 # format is <day> at <time>:
191 # we grab the current time once, since processing
192 # all the configs might take more than an hour.
195 nowdayofweek=`date +%A`
196 nowdayofweek=`tolower "$nowdayofweek"`
201 whendayofweek=$1; at=$2; whentime=$3;
202 whenday=`toint "$whendayofweek"`
203 whendayofweek=`tolower "$whendayofweek"`
204 whentime=`echo "$whentime" | sed 's/:[0-9][0-9]$//' | sed -r 's/^([0-9])$/0\1/'`
206 if [ "$whendayofweek" == "everyday" -o "$whendayofweek" == "daily" ]; then
207 whendayofweek=$nowdayofweek
210 if [ "$whenday" == "" ]; then
211 if [ "$whendayofweek" != "$nowdayofweek" ]; then
212 whendayofweek=${whendayofweek%s}
213 if [ "$whendayofweek" != "$nowdayofweek" ]; then
217 elif [ "$whenday" != "$nowday" ]; then
221 [ "$at" == "at" ] || return 0
222 [ "$whentime" == "$nowtime" ] || return 0
230 This script allows you to coordinate system backup by dropping a few
231 simple configuration files into /etc/backup.d/. Typically, this
232 script is run hourly from cron.
234 The following options are available:
235 -h, --help This usage message
236 -d, --debug Run in debug mode, where all log messages are
237 output to the current shell.
238 -f, --conffile FILE Use FILE for the main configuration instead
239 of /etc/backupninja.conf
240 -t, --test Run in test mode, no actions are actually taken.
241 -n, --now Perform actions now, instead of when they
243 --run FILE Execute the specified action file and then exit.
244 When using colored output, there are:
247 debug "Debugging info (when run with -d)"
248 info "Informational messages (verbosity level 4)"
249 warning "Warnings (verbosity level 3 and up)"
250 error "Errors (verbosity level 2 and up)"
251 fatal "Fatal, halting errors (always shown)"
255 ## this function handles the running of a backup action
257 ## these globals are modified:
258 ## fatals, errors, warnings, actions_run, errormsg
261 function process_action() {
267 # skip over this config if "when" option
268 # is not set to the current time.
269 getconf when "$defaultwhen"
270 if [ "$processnow" == 1 ]; then
271 info "running $file because of --now"
272 elif [ "$when" == "hourly" ]; then
273 debug "running $file because 'when = hourly'"
281 if [ $ret == 0 ]; then
282 debug "skipping $file because it is not $w"
285 info "running $file because it is $w"
291 let "actions_run += 1"
294 local bufferfile="/tmp/backupninja.buffer.$$"
295 echo "" > $bufferfile
298 . $scriptdir/$suffix $file
301 echo $a >> $bufferfile
302 [ $debug ] && colorize "$a"
306 # ^^^^^^^^ we have a problem! we can't grab the return code "$?". grrr.
309 _warnings=`cat $bufferfile | grep "^Warning: " | wc -l`
310 _errors=`cat $bufferfile | grep "^Error: " | wc -l`
311 _fatals=`cat $bufferfile | grep "^Fatal: " | wc -l`
313 ret=`grep "\(^Warning: \|^Error: \|^Fatal: \)" $bufferfile`
315 if [ $_fatals != 0 ]; then
316 msg "*failed* -- $file"
317 errormsg="$errormsg\n== failures from $file ==\n\n$ret\n"
318 elif [ $_errors != 0 ]; then
319 msg "*error* -- $file"
320 errormsg="$errormsg\n== errors from $file ==\n\n$ret\n"
321 elif [ $_warnings != 0 ]; then
322 msg "*warning* -- $file"
323 errormsg="$errormsg\n== warnings from $file ==\n\n$ret\n"
325 msg "success -- $file"
326 # elif [ $retcode == 0 ]; then
327 # msg "success -- $file"
329 # msg "unknown -- $file"
332 let "fatals += _fatals"
333 let "errors += _errors"
334 let "warnings += _warnings"
337 #####################################################
341 conffile="/etc/backupninja.conf"
344 ## process command line options
346 while [ $# -ge 1 ]; do
349 -d|--debug) debug=1;;
350 -t|--test) test=1;debug=1;;
351 -n|--now) processnow=1;;
356 echo "-f|--conffile option must be followed by an existing filename"
357 fatal "-f|--conffile option must be followed by an existing filename"
360 # we shift here to avoid processing the file path
369 echo "--run option must be fallowed by a backupninja action file"
370 fatal "--run option must be fallowed by a backupninja action file"
377 echo "Unknown option $1"
378 fatal "Unknown option $1"
390 ## Load and confirm basic configuration values
393 if [ ! -r "$conffile" ]; then
394 echo "Configuration file $conffile not found."
395 fatal "Configuration file $conffile not found."
398 scriptdir=`grep scriptdirectory $conffile | awk '{print $3}'`
399 if [ ! -n "$scriptdir" ]; then
400 echo "Cound not find entry 'scriptdirectory' in $conffile"
401 fatal "Cound not find entry 'scriptdirectory' in $conffile"
404 if [ ! -d "$scriptdir" ]; then
405 echo "Script directory $scriptdir not found."
406 fatal "Script directory $scriptdir not found."
411 # get global config options (second param is the default)
412 getconf configdirectory /etc/backup.d
414 getconf reportsuccess yes
415 getconf reportwarning yes
417 getconf when "Everyday at 01:00"
419 getconf logfile /var/log/backupninja.log
420 getconf usecolors "yes"
421 getconf SLAPCAT /usr/sbin/slapcat
422 getconf LDAPSEARCH /usr/bin/ldapsearch
423 getconf RDIFFBACKUP /usr/bin/rdiff-backup
424 getconf MYSQL /usr/bin/mysql
425 getconf MYSQLHOTCOPY /usr/bin/mysqlhotcopy
426 getconf MYSQLDUMP /usr/bin/mysqldump
427 getconf GZIP /bin/gzip
428 getconf RSYNC /usr/bin/rsync
430 if [ ! -d "$configdirectory" ]; then
431 echo "Configuration directory '$configdirectory' not found."
432 fatal "Configuration directory '$configdirectory' not found."
435 [ -f "$logfile" ] || touch $logfile
437 if [ "$UID" != "0" ]; then
438 echo "$0 can only be run as root"
442 ## Process each configuration file
444 # by default, don't make files which are world or group readable.
447 # these globals are set by process_action()
454 if [ "$singlerun" ]; then
457 files=`find $configdirectory -mindepth 1 | sort -n`
460 for file in $files; do
461 [ -f "$file" ] || continue
465 base=`basename $file`
466 if [ "${base:0:1}" == "0" ]; then
467 info "Skipping $file"
471 if [ -e "$scriptdir/$suffix" ]; then
472 process_action $file $suffix
474 error "Can't process file '$file': no handler script for suffix '$suffix'"
475 msg "*missing handler* -- $file"
479 ## mail the messages to the report address
481 if [ $actions_run == 0 ]; then doit=0
482 elif [ "$reportemail" == "" ]; then doit=0
483 elif [ $fatals != 0 ]; then doit=1
484 elif [ $errors != 0 ]; then doit=1
485 elif [ "$reportsuccess" == "yes" ]; then doit=1
486 elif [ "$reportwarning" == "yes" -a $warnings != 0 ]; then doit=1
490 if [ $doit == 1 ]; then
491 debug "send report to $reportemail"
493 [ $warnings == 0 ] || subject="WARNING"
494 [ $errors == 0 ] || subject="ERROR"
495 [ $fatals == 0 ] || subject="FAILED"
498 for ((i=0; i < ${#messages[@]} ; i++)); do
502 } | mail $reportemail -s "backupninja: $hostname $subject"