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 [ "$usecolor" == "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 fatal "Configuration files must not be group or world readable! Dying on file $file"
164 if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then
165 fatal "Configuration files must be owned by root! Dying on file $file"
169 # simple lowercase function
171 echo "$1" | tr [:upper:] [:lower:]
174 # simple to integer function
176 echo "$1" | tr [:alpha:] -d
180 # function isnow(): returns 1 if the time/day passed as $1 matches
181 # the current time/day.
183 # format is <day> at <time>:
189 # we grab the current time once, since processing
190 # all the configs might take more than an hour.
193 nowdayofweek=`date +%A`
194 nowdayofweek=`tolower "$nowdayofweek"`
199 whendayofweek=$1; at=$2; whentime=$3;
200 whenday=`toint "$whendayofweek"`
201 whendayofweek=`tolower "$whendayofweek"`
202 whentime=`echo "$whentime" | sed 's/:[0-9][0-9]$//'`
204 if [ "$whendayofweek" == "everyday" ]; then
205 whendayofweek=$nowdayofweek
208 if [ "$whenday" == "" ]; then
209 if [ "$whendayofweek" != "$nowdayofweek" ]; then
210 whendayofweek=${whendayofweek%s}
211 if [ "$whendayofweek" != "$nowdayofweek" ]; then
215 elif [ "$whenday" != "$nowday" ]; then
219 [ "$at" == "at" ] || return 0
220 [ "$whentime" == "$nowtime" ] || return 0
228 This script allows you to coordinate system backup by dropping a few
229 simple configuration files into /etc/backup.d/. Typically, this
230 script is run hourly from cron.
232 The following options are available:
233 -h, --help This usage message
234 -d, --debug Run in debug mode, where all log messages are
235 output to the current shell.
236 -f, --conffile FILE Use FILE for the main configuration instead
237 of /etc/backupninja.conf
238 -t, --test Run in test mode, no actions are actually taken.
239 -n, --now Perform actions now, instead of when they
241 When using colored output, there are:
244 debug "Debugging info (when run with -d)"
245 info "Informational messages (verbosity level 4)"
246 warning "Warnings (verbosity level 3 and up)"
247 error "Errors (verbosity level 2 and up)"
248 fatal "Fatal, halting errors (always shown)"
252 ## this function handles the running of a backup action
254 ## these globals are modified:
255 ## fatals, errors, warnings, actions_run, errormsg
258 function process_action() {
264 # skip over this config if "when" option
265 # is not set to the current time.
266 getconf when "$defaultwhen"
267 if [ "$processnow" == 1 ]; then
268 info "running $file because of --now"
276 if [ $ret == 0 ]; then
277 debug "skipping $file because it is not $w"
280 info "running $file because it is $w"
286 let "actions_run += 1"
289 local bufferfile="/tmp/backupninja.buffer.$$"
290 echo "" > $bufferfile
293 . $scriptdir/$suffix $file
296 echo $a >> $bufferfile
297 [ $debug ] && colorize "$a"
301 # ^^^^^^^^ we have a problem! we can't grab the return code "$?". grrr.
304 _warnings=`cat $bufferfile | grep "^Warning: " | wc -l`
305 _errors=`cat $bufferfile | grep "^Error: " | wc -l`
306 _fatals=`cat $bufferfile | grep "^Fatal: " | wc -l`
308 ret=`grep "\(^Warning: \|^Error: \|^Fatal: \)" $bufferfile`
310 if [ $_fatals != 0 ]; then
311 msg "*failed* -- $file"
312 errormsg="$errormsg\n== failures from $file ==\n\n$ret\n"
313 elif [ $_errors != 0 ]; then
314 msg "*error* -- $file"
315 errormsg="$errormsg\n== errors from $file ==\n\n$ret\n"
316 elif [ $_warnings != 0 ]; then
317 msg "*warning* -- $file"
318 errormsg="$errormsg\n== warnings from $file ==\n\n$ret\n"
319 elif [ $retcode == 0 ]; then
320 msg "success -- $file"
322 msg "unknown -- $file"
325 let "fatals += _fatals"
326 let "errors += _errors"
327 let "warnings += _warnings"
330 #####################################################
334 conffile="/etc/backupninja.conf"
337 ## process command line options
339 while [ $# -ge 1 ]; do
342 -d|--debug) debug=1;;
343 -t|--test) test=1;debug=1;;
344 -n|--now) processnow=1;;
349 fatal "-f|--conffile option must be followed by an existing filename"
352 # we shift here to avoid processing the file path
356 fatal "Unknown option $1"
363 ## Load and confirm basic configuration values
366 [ -r "$conffile" ] || fatal "Configuration file $conffile not found."
367 scriptdir=`grep scriptdirectory $conffile | awk '{print $3}'`
368 [ -n "$scriptdir" ] || fatal "Cound not find entry 'scriptdirectory' in $conffile"
369 [ -d "$scriptdir" ] || fatal "Script directory $scriptdir not found."
372 # get global config options (second param is the default)
373 getconf configdirectory /etc/backup.d
375 getconf reportsuccess yes
376 getconf reportwarning yes
378 getconf when "Everyday at 01:00"
380 getconf logfile /var/log/backupninja.log
381 getconf usecolors "yes"
382 getconf SLAPCAT /usr/sbin/slapcat
383 getconf RDIFFBACKUP /usr/bin/rdiff-backup
384 getconf MYSQL /usr/bin/mysql
385 getconf MYSQLHOTCOPY /usr/bin/mysqlhotcopy
386 getconf MYSQLDUMP /usr/bin/mysqldump
387 getconf GZIP /bin/gzip
388 getconf RSYNC /usr/bin/rsync
390 [ -d "$configdirectory" ] || fatal "Configuration directory '$configdirectory' not found."
392 if [ "$UID" != "0" ]; then
393 echo "$0 can only be run as root"
397 ## Process each configuration file
399 # by default, don't make files which are world or group readable.
402 # these globals are set by process_action()
409 for file in $configdirectory/*; do
410 [ -f $file ] || continue;
414 base=`basename $file`
415 if [ "${base:0:1}" == "0" ]; then
416 info "Skipping $file"
420 if [ -e "$scriptdir/$suffix" ]; then
421 process_action $file $suffix
423 error "Can't process file '$file': no handler script for suffix '$suffix'"
424 msg "*missing handler* -- $file"
428 ## mail the messages to the report address
430 if [ $actions_run == 0 ]; then doit=0
431 elif [ "$reportemail" == "" ]; then doit=0
432 elif [ $fatals != 0 ]; then doit=1
433 elif [ $errors != 0 ]; then doit=1
434 elif [ "$reportsuccess" == "yes" ]; then doit=1
435 elif [ "$reportwarning" == "yes" -a $warnings != 0 ]; then doit=1
439 if [ $doit == 1 ]; then
440 debug "send report to $reportemail"
442 [ $warnings == 0 ] || subject="WARNING"
443 [ $errors == 0 ] || subject="ERROR"
444 [ $fatals == 0 ] || subject="FAILED"
447 for ((i=0; i < ${#messages[@]} ; i++)); do
451 } | mail $reportemail -s "backupninja: $hostname $subject"