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)"
243 function process_action() {
249 # skip over this config if "when" option
250 # is not set to the current time.
251 getconf when "$defaultwhen"
252 if [ "$processnow" == 1 ]; then
253 info "running $file because of --now"
261 if [ $ret == 0 ]; then
262 debug "skipping $file because it is not $w"
265 info "running $file because it is $w"
273 ret=`( . $scriptdir/$suffix $file )`
275 warnings=`echo $ret | grep -e "^Warning: " | wc -l`
276 errors=`echo $ret | grep -e "^Error: \|^Fatal: " | wc -l`
277 if [ $errors != 0 ]; then
278 msg "*failed* -- $file"
279 errormsg="$errormsg\n== errors from $file ==\n\n$ret\n"
280 elif [ $warnings != 0 ]; then
281 msg "*warning* -- $file"
282 errormsg="$errormsg\n== warnings from $file ==\n\n$ret\n"
283 elif [ $retcode == 0 ]; then
284 msg "success -- $file"
286 msg "unknown -- $file"
291 #####################################################
295 conffile="/etc/backupninja.conf"
298 ## process command line options
300 while [ $# -ge 1 ]; do
303 -d|--debug) debug=1;;
304 -t|--test) test=1;debug=1;;
305 -n|--now) processnow=1;;
310 fatal "-f|--conffile option must be followed by an existing filename"
313 # we shift here to avoid processing the file path
317 fatal "Unknown option $1"
324 ## Load and confirm basic configuration values
327 [ -r "$conffile" ] || fatal "Configuration file $conffile not found."
328 scriptdir=`grep scriptdirectory $conffile | awk '{print $3}'`
329 [ -n "$scriptdir" ] || fatal "Cound not find entry 'scriptdirectory' in $conffile"
330 [ -d "$scriptdir" ] || fatal "Script directory $scriptdir not found."
333 # get global config options (second param is the default)
334 getconf configdirectory /etc/backup.d
336 getconf reportsuccess yes
337 getconf reportwarning yes
339 getconf when "Everyday at 01:00"
341 getconf logfile /var/log/backupninja.log
342 getconf usecolors "yes"
343 getconf SLAPCAT /usr/sbin/slapcat
344 getconf RDIFFBACKUP /usr/bin/rdiff-backup
345 getconf MYSQL /usr/bin/mysql
346 getconf MYSQLHOTCOPY /usr/bin/mysqlhotcopy
347 getconf MYSQLDUMP /usr/bin/mysqldump
348 getconf GZIP /bin/gzip
350 [ -d "$configdirectory" ] || fatal "Configuration directory '$configdirectory' not found."
352 if [ "$UID" != "0" ]; then
353 echo "$0 can only be run as root"
357 ## Process each configuration file
359 info "====== starting at "`date`" ======"
361 # by default, don't make files which are world or group readable.
366 for file in $configdirectory/*; do
367 [ -f $file ] || continue;
371 base=`basename $file`
372 if [ "${base:0:1}" == "0" ]; then
373 info "Skipping $file"
377 if [ -e "$scriptdir/$suffix" ]; then
378 process_action $file $suffix
380 error "Can't process file '$file': no handler script for suffix '$suffix'"
381 msg "*missing handler* -- $file"
385 ## mail the messages to the report address
387 if [ "$reportemail" == "" ]; then doit=0
388 elif [ $errors != 0 ]; then doit=1
389 elif [ "$reportsuccess" == "yes" ]; then doit=1
390 elif [ "$reportwarning" == "yes" -a $warnings != 0 ]; then doit=1
394 if [ $doit == 1 ]; then
397 for ((i=0; i < ${#messages[@]} ; i++)); do
401 } | mail $reportemail -s "backupninja: $hostname"
404 info "====== finished at "`date`" ======"
406 ############################################################