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"
297 ## process command line options
299 while [ $# -ge 1 ]; do
302 -d|--debug) debug=1;;
303 -t|--test) test=1;debug=1;;
304 -n|--now) processnow=1;;
309 fatal "-f|--conffile option must be followed by an existing filename"
312 # we shift here to avoid processing the file path
316 fatal "Unknown option $1"
323 ## Load and confirm basic configuration values
326 [ -r "$conffile" ] || fatal "Configuration file $conffile not found."
327 scriptdir=`grep scriptdirectory $conffile | awk '{print $3}'`
328 [ -n "$scriptdir" ] || fatal "Cound not find entry 'scriptdirectory' in $conffile"
329 [ -d "$scriptdir" ] || fatal "Script directory $scriptdir not found."
332 # get global config options (second param is the default)
333 getconf configdirectory /etc/backup.d
335 getconf reportsuccess yes
336 getconf reportwarning yes
338 getconf when "Everyday at 01:00"
340 getconf logfile /var/log/backupninja.log
341 getconf usecolors "yes"
342 getconf SLAPCAT /usr/sbin/slapcat
343 getconf RDIFFBACKUP /usr/bin/rdiff-backup
344 getconf MYSQL /usr/bin/mysql
345 getconf MYSQLHOTCOPY /usr/bin/mysqlhotcopy
346 getconf MYSQLDUMP /usr/bin/mysqldump
347 getconf GZIP /bin/gzip
349 [ -d "$configdirectory" ] || fatal "Configuration directory '$configdirectory' not found."
351 if [ "$UID" != "0" ]; then
352 echo "$0 can only be run as root"
356 ## Process each configuration file
358 info "====== starting at "`date`" ======"
360 # by default, don't make files which are world or group readable.
365 for file in $configdirectory/*; do
366 [ -f $file ] || continue;
370 base=`basename $file`
371 if [ "${base:0:1}" == "0" ]; then
372 info "Skipping $file"
376 if [ -e "$scriptdir/$suffix" ]; then
377 process_action $file $suffix
379 error "Can't process file '$file': no handler script for suffix '$suffix'"
380 msg "*missing handler* -- $file"
384 ## mail the messages to the report address
386 if [ "$reportemail" == "" ]; then doit=0
387 elif [ $errors != 0 ]; then doit=1
388 elif [ "$reportsuccess" == "yes" ]; then doit=1
389 elif [ "$reportwarning" == "yes" -a $warnings != 0 ]; then doit=1
393 if [ $doit == 1 ]; then
396 for ((i=0; i < ${#messages[@]} ; i++)); do
400 } | mail $reportemail -s "backupninja: $hostname"
403 info "====== finished at "`date`" ======"
405 ############################################################