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() {
35 # returnstring=`$@ 2>&1`
37 # RUNERRORS=$[RUNERRORS+RUNERROR]
38 # if [ "$RUNERROR" != 0 ]; then
39 # debug 3 "Exitcode $RUNERROR returned when running: $@"
40 # debug 3 "$returnstring"
42 # debug 0 "$returnstring"
47 # We have the following message levels:
49 # 1 - normal messages - green
50 # 2 - warnings - yellow
53 # First variable passed is the error level, all others are printed
55 # if 1, echo out all warnings, errors, or fatal
56 # used to capture output from handlers
62 [ ${#@} -gt 1 ] || return
64 types=(Debug Info Warning Error Fatal)
67 typestr=${types[$type]}
68 if [ "$usecolor" == "yes" ]; then
69 colors=($BLUE $GREEN $YELLOW $RED $PURPLE)
70 color=${colors[$type]}
76 if [ "$echo_debug_msg" != "0" -a "$type" -gt "1" ]; then
77 echo -e "$typestr: $@"
80 if [ "$debug" == 1 ]; then
81 echo -e "${color}$typestr: $@${endcolor}" >&2
84 if [ $print -lt $loglevel ]; then
85 if [ -w "$logfile" ]; then
86 echo -e "${color}$typestr: $@${endcolor}" >> $logfile
110 messages[$msgcount]=$1
118 function setsection() {
123 # sets a global var with name equal to $1
124 # to the value of the configuration parameter $1
130 ret=`awk -f $scriptdir/parseini S=$CURRENT_SECTION P=$CURRENT_PARAM $CURRENT_CONF_FILE`
131 # if nothing is returned, set the default
132 if [ "$ret" == "" -a "$2" != "" ]; then
136 # replace * with %, so that it is not globbed.
137 ret="${ret//\\*/__star__}"
139 # this is weird, but single quotes are needed to
140 # allow for returned values with spaces. $ret is still expanded
141 # because it is in an 'eval' statement.
146 # enforces very strict permissions on configuration file $file.
149 function check_perms() {
151 local perms=`ls -ld $file`
153 if [ "$perms" != "------" ]; then
154 fatal "Configuration files must not be group or world readable! Dying on file $file"
156 if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then
157 fatal "Configuration files must be owned by root! Dying on file $file"
161 # simple lowercase function
163 echo "$1" | tr [:upper:] [:lower:]
166 # simple to integer function
168 echo "$1" | tr [:alpha:] -d
172 # function isnow(): returns 1 if the time/day passed as $1 matches
173 # the current time/day.
175 # format is <day> at <time>:
181 # we grab the current time once, since processing
182 # all the configs might take more than an hour.
185 nowdayofweek=`date +%A`
186 nowdayofweek=`tolower "$nowdayofweek"`
191 whendayofweek=$1; at=$2; whentime=$3;
192 whenday=`toint "$whendayofweek"`
193 whendayofweek=`tolower "$whendayofweek"`
194 whentime=`echo "$whentime" | sed 's/:[0-9][0-9]$//'`
196 if [ "$whendayofweek" == "everyday" ]; then
197 whendayofweek=$nowdayofweek
200 if [ "$whenday" == "" ]; then
201 if [ "$whendayofweek" != "$nowdayofweek" ]; then
202 whendayofweek=${whendayofweek%s}
203 if [ "$whendayofweek" != "$nowdayofweek" ]; then
207 elif [ "$whenday" != "$nowday" ]; then
211 [ "$at" == "at" ] || return 0
212 [ "$whentime" == "$nowtime" ] || return 0
220 This script allows you to coordinate system backup by dropping a few
221 simple configuration files into /etc/backup.d/. Typically, this
222 script is run hourly from cron.
224 The following options are available:
225 -h, --help This usage message
226 -d, --debug Run in debug mode, where all log messages are
227 output to the current shell.
228 -f, --conffile FILE Use FILE for the main configuration instead
229 of /etc/backupninja.conf
230 -t, --test Run in test mode, no actions are actually taken.
231 -n, --now Perform actions now, instead of when they
233 When using colored output, there are:
236 debug "Debugging info (when run with -d)"
237 info "Informational messages (verbosity level 4)"
238 warning "Warnings (verbosity level 3 and up)"
239 error "Errors (verbosity level 2 and up)"
240 fatal "Fatal, halting errors (always shown)"
244 ## this function handles the running of a backup action
246 ## these globals are modified:
247 ## fatals, errors, warnings, actions_run, errormsg
250 function process_action() {
256 # skip over this config if "when" option
257 # is not set to the current time.
258 getconf when "$defaultwhen"
259 if [ "$processnow" == 1 ]; then
260 info "running $file because of --now"
268 if [ $ret == 0 ]; then
269 debug "skipping $file because it is not $w"
272 info "running $file because it is $w"
278 let "actions_run += 1"
282 ret=`( . $scriptdir/$suffix $file )`
285 _warnings=`echo $ret | grep "Warning: " | wc -l`
286 _errors=`echo $ret | grep "Error: " | wc -l`
287 _fatals=`echo $ret | grep "Fatal: " | wc -l`
289 if [ $_fatals != 0 ]; then
290 msg "*failed* -- $file"
291 errormsg="$errormsg\n== failures from $file ==\n\n$ret\n"
292 elif [ $_errors != 0 ]; then
293 msg "*error* -- $file"
294 errormsg="$errormsg\n== errors from $file ==\n\n$ret\n"
295 elif [ $_warnings != 0 ]; then
296 msg "*warning* -- $file"
297 errormsg="$errormsg\n== warnings from $file ==\n\n$ret\n"
298 elif [ $retcode == 0 ]; then
299 msg "success -- $file"
301 msg "unknown -- $file"
305 let "fatals += _fatals"
306 let "errors += _errors"
307 let "warnings += _warnings"
310 #####################################################
314 conffile="/etc/backupninja.conf"
317 ## process command line options
319 while [ $# -ge 1 ]; do
322 -d|--debug) debug=1;;
323 -t|--test) test=1;debug=1;;
324 -n|--now) processnow=1;;
329 fatal "-f|--conffile option must be followed by an existing filename"
332 # we shift here to avoid processing the file path
336 fatal "Unknown option $1"
343 ## Load and confirm basic configuration values
346 [ -r "$conffile" ] || fatal "Configuration file $conffile not found."
347 scriptdir=`grep scriptdirectory $conffile | awk '{print $3}'`
348 [ -n "$scriptdir" ] || fatal "Cound not find entry 'scriptdirectory' in $conffile"
349 [ -d "$scriptdir" ] || fatal "Script directory $scriptdir not found."
352 # get global config options (second param is the default)
353 getconf configdirectory /etc/backup.d
355 getconf reportsuccess yes
356 getconf reportwarning yes
358 getconf when "Everyday at 01:00"
360 getconf logfile /var/log/backupninja.log
361 getconf usecolors "yes"
362 getconf SLAPCAT /usr/sbin/slapcat
363 getconf RDIFFBACKUP /usr/bin/rdiff-backup
364 getconf MYSQL /usr/bin/mysql
365 getconf MYSQLHOTCOPY /usr/bin/mysqlhotcopy
366 getconf MYSQLDUMP /usr/bin/mysqldump
367 getconf GZIP /bin/gzip
369 [ -d "$configdirectory" ] || fatal "Configuration directory '$configdirectory' not found."
371 if [ "$UID" != "0" ]; then
372 echo "$0 can only be run as root"
376 ## Process each configuration file
378 # by default, don't make files which are world or group readable.
381 # these globals are set by process_action()
388 for file in $configdirectory/*; do
389 [ -f $file ] || continue;
393 base=`basename $file`
394 if [ "${base:0:1}" == "0" ]; then
395 info "Skipping $file"
399 if [ -e "$scriptdir/$suffix" ]; then
400 process_action $file $suffix
402 error "Can't process file '$file': no handler script for suffix '$suffix'"
403 msg "*missing handler* -- $file"
407 ## mail the messages to the report address
409 if [ $actions_run == 0 ]; then doit=0
410 elif [ "$reportemail" == "" ]; then doit=0
411 elif [ $fatals != 0 ]; then doit=1
412 elif [ $errors != 0 ]; then doit=1
413 elif [ "$reportsuccess" == "yes" ]; then doit=1
414 elif [ "$reportwarning" == "yes" -a $warnings != 0 ]; then doit=1
418 if [ $doit == 1 ]; then
419 debug "send report to $reportemail"
421 [ $warnings == 0 ] || subject="WARNING"
422 [ $errors == 0 ] || subject="ERROR"
423 [ $fatals == 0 ] || subject="FAILED"
426 for ((i=0; i < ${#messages[@]} ; i++)); do
430 } | mail $reportemail -s "backupninja: $hostname $subject"