2 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
6 # B A C K U P N I N J A /()/
9 # Copyright (C) 2004-05 riseup.net -- property is theft.
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
22 #####################################################
25 function setupcolors () {
33 COLORS=($BLUE $GREEN $YELLOW $RED $PURPLE $CYAN)
36 function colorize () {
37 if [ "$usecolors" == "yes" ]; then
38 local typestr=`echo "$@" | @SED@ 's/\(^[^:]*\).*$/\1/'`
39 [ "$typestr" == "Debug" ] && type=0
40 [ "$typestr" == "Info" ] && type=1
41 [ "$typestr" == "Warning" ] && type=2
42 [ "$typestr" == "Error" ] && type=3
43 [ "$typestr" == "Fatal" ] && type=4
44 [ "$typestr" == "Halt" ] && type=5
45 color=${COLORS[$type]}
47 echo -e "$color$@$endcolor"
53 # We have the following message levels:
55 # 1 - normal messages - green
56 # 2 - warnings - yellow
60 # First variable passed is the error level, all others are printed
62 # if 1, echo out all warnings, errors, or fatal
63 # used to capture output from handlers
69 [ ${#@} -gt 1 ] || return
73 if [ $type == 100 ]; then
74 typestr=`echo "$@" | @SED@ 's/\(^[^:]*\).*$/\1/'`
75 [ "$typestr" == "Debug" ] && type=0
76 [ "$typestr" == "Info" ] && type=1
77 [ "$typestr" == "Warning" ] && type=2
78 [ "$typestr" == "Error" ] && type=3
79 [ "$typestr" == "Fatal" ] && type=4
80 [ "$typestr" == "Halt" ] && type=5
83 types=(Debug Info Warning Error Fatal Halt)
84 typestr="${types[$type]}: "
89 if [ $echo_debug_msg == 1 ]; then
90 echo -e "$typestr$@" >&2
92 colorize "$typestr$@" >&2
95 if [ $print -lt $loglevel ]; then
101 if [ -w "$logfile" ]; then
102 echo -e `LC_ALL=C date "+%h %d %H:%M:%S"` "$@" >> $logfile
106 function passthru() {
132 messages[$msgcount]=$1
137 # enforces very strict permissions on configuration file $file.
140 function check_perms() {
142 debug "check_perms $file"
146 perms=($(stat -L --format='%A' $file))
147 debug "perms: $perms"
148 local gperm=${perms:4:3}
149 debug "gperm: $gperm"
150 local wperm=${perms:7:3}
151 debug "wperm: $wperm"
153 owners=($(stat -L --format='%g %G %u %U' $file))
154 local gid=${owners[0]}
155 local group=${owners[1]}
156 local owner=${owners[2]}
158 if [ "$owner" != 0 ]; then
159 echo "Configuration files must be owned by root! Dying on file $file"
160 fatal "Configuration files must be owned by root! Dying on file $file"
163 if [ "$wperm" != '---' ]; then
164 echo "Configuration files must not be world writable/readable! Dying on file $file"
165 fatal "Configuration files must not be world writable/readable! Dying on file $file"
168 if [ "$gperm" != '---' ]; then
169 case "$admingroup" in
173 if [ "$gid" != 0 ]; then
174 echo "Configuration files must not be writable/readable by group $group! Use the admingroup option in backupninja.conf. Dying on file $file"
175 fatal "Configuration files must not be writable/readable by group $group! Use the admingroup option in backupninja.conf. Dying on file $file"
182 # simple lowercase function
184 echo "$1" | tr '[:upper:]' '[:lower:]'
187 # simple to integer function
189 echo "$1" | tr -d '[:alpha:]'
193 # function isnow(): returns 1 if the time/day passed as $1 matches
194 # the current time/day.
196 # format is <day> at <time>:
202 # we grab the current time once, since processing
203 # all the configs might take more than an hour.
204 nowtime=`LC_ALL=C date +%H`
205 nowday=`LC_ALL=C date +%d`
206 nowdayofweek=`LC_ALL=C date +%A`
207 nowdayofweek=`tolower "$nowdayofweek"`
213 [ "$when" == "manual" ] && return 0
215 whendayofweek=$1; at=$2; whentime=$3;
216 whenday=`toint "$whendayofweek"`
217 whendayofweek=`tolower "$whendayofweek"`
218 whentime=`echo "$whentime" | @SED@ 's/:[0-9][0-9]$//' | @SED@ -r 's/^([0-9])$/0\1/'`
220 if [ "$whendayofweek" == "everyday" -o "$whendayofweek" == "daily" ]; then
221 whendayofweek=$nowdayofweek
224 if [ "$whenday" == "" ]; then
225 if [ "$whendayofweek" != "$nowdayofweek" ]; then
226 whendayofweek=${whendayofweek%s}
227 if [ "$whendayofweek" != "$nowdayofweek" ]; then
231 elif [ "$whenday" != "$nowday" ]; then
235 [ "$at" == "at" ] || return 0
236 [ "$whentime" == "$nowtime" ] || return 0
244 This script allows you to coordinate system backup by dropping a few
245 simple configuration files into @CFGDIR@/backup.d/. Typically, this
246 script is run hourly from cron.
248 The following options are available:
249 -h, --help This usage message
250 -d, --debug Run in debug mode, where all log messages are
251 output to the current shell.
252 -f, --conffile FILE Use FILE for the main configuration instead
253 of @CFGDIR@/backupninja.conf
254 -t, --test Test run mode. This will test if the backup
255 could run, without actually preforming any
256 backups. For example, it will attempt to authenticate
257 or test that ssh keys are set correctly.
258 -n, --now Perform actions now, instead of when they might
259 be scheduled. No output will be created unless also
261 --run FILE Execute the specified action file and then exit.
262 Also puts backupninja in debug mode.
264 When in debug mode, output to the console will be colored:
267 colorize "Debug: Debugging info (when run with -d)"
268 colorize "Info: Informational messages (verbosity level 4)"
269 colorize "Warning: Warnings (verbosity level 3 and up)"
270 colorize "Error: Errors (verbosity level 2 and up)"
271 colorize "Fatal: Errors which halt a given backup action (always shown)"
272 colorize "Halt: Errors which halt the whole backupninja run (always shown)"
276 ## this function handles the running of a backup action
278 ## these globals are modified:
279 ## halts, fatals, errors, warnings, actions_run, errormsg
282 function process_action() {
288 # skip over this config if "when" option
289 # is not set to the current time.
290 getconf when "$defaultwhen"
291 if [ "$processnow" == 1 ]; then
292 info ">>>> starting action $file (because of --now)"
294 elif [ "$when" == "hourly" ]; then
295 info ">>>> starting action $file (because 'when = hourly')"
304 if [ $ret == 0 ]; then
305 debug "skipping $file because current time does not match $w"
307 info ">>>> starting action $file (because current time matches $w)"
314 [ "$run" == "no" ] && return
316 let "actions_run += 1"
319 local bufferfile=`maketemp backupninja.buffer`
320 echo "" > $bufferfile
323 . $scriptdirectory/$suffix $file
326 echo $a >> $bufferfile
327 [ $debug ] && colorize "$a"
331 # ^^^^^^^^ we have a problem! we can't grab the return code "$?". grrr.
334 _warnings=`cat $bufferfile | grep "^Warning: " | wc -l`
335 _errors=`cat $bufferfile | grep "^Error: " | wc -l`
336 _fatals=`cat $bufferfile | grep "^Fatal: " | wc -l`
337 _halts=`cat $bufferfile | grep "^Halt: " | wc -l`
339 ret=`grep "\(^Warning: \|^Error: \|^Fatal: \|Halt: \)" $bufferfile`
341 if [ $_halts != 0 ]; then
342 msg "*halt* -- $file"
343 errormsg="$errormsg\n== halt request from $file==\n\n$ret\n"
344 passthru "Halt: <<<< finished action $file: FAILED"
345 elif [ $_fatals != 0 ]; then
346 msg "*failed* -- $file"
347 errormsg="$errormsg\n== fatal errors from $file ==\n\n$ret\n"
348 passthru "Fatal: <<<< finished action $file: FAILED"
349 elif [ $_errors != 0 ]; then
350 msg "*error* -- $file"
351 errormsg="$errormsg\n== errors from $file ==\n\n$ret\n"
352 error "<<<< finished action $file: ERROR"
353 elif [ $_warnings != 0 ]; then
354 msg "*warning* -- $file"
355 errormsg="$errormsg\n== warnings from $file ==\n\n$ret\n"
356 warning "<<<< finished action $file: WARNING"
358 msg "success -- $file"
359 info "<<<< finished action $file: SUCCESS"
362 let "halts += _halts"
363 let "fatals += _fatals"
364 let "errors += _errors"
365 let "warnings += _warnings"
368 #####################################################
372 conffile="@CFGDIR@/backupninja.conf"
375 ## process command line options
377 while [ $# -ge 1 ]; do
380 -d|--debug) debug=1;;
381 -t|--test) test=1;debug=1;;
382 -n|--now) processnow=1;;
387 echo "-f|--conffile option must be followed by an existing filename"
388 fatal "-f|--conffile option must be followed by an existing filename"
391 # we shift here to avoid processing the file path
400 echo "--run option must be followed by a backupninja action file"
401 fatal "--run option must be followed by a backupninja action file"
408 echo "Unknown option $1"
409 fatal "Unknown option $1"
421 ## Load and confirm basic configuration values
424 if [ ! -r "$conffile" ]; then
425 echo "Configuration file $conffile not found."
426 fatal "Configuration file $conffile not found."
430 libdirectory=`grep '^libdirectory' $conffile | @AWK@ '{print $3}'`
431 if [ -z "$libdirectory" ]; then
432 if [ -d "@libdir@" ]; then
433 libdirectory="@libdir@"
435 echo "Could not find entry 'libdirectory' in $conffile."
436 fatal "Could not find entry 'libdirectory' in $conffile."
439 if [ ! -d "$libdirectory" ]; then
440 echo "Lib directory $libdirectory not found."
441 fatal "Lib directory $libdirectory not found."
445 # include shared functions
446 . $libdirectory/tools
447 . $libdirectory/vserver
451 # get global config options (second param is the default)
452 getconf configdirectory @CFGDIR@/backup.d
453 getconf scriptdirectory @datadir@
454 getconf reportdirectory
458 getconf reportsuccess yes
460 getconf reportwarning yes
462 getconf when "Everyday at 01:00"
464 getconf logfile @localstatedir@/log/backupninja.log
465 getconf usecolors "yes"
466 getconf SLAPCAT /usr/sbin/slapcat
467 getconf LDAPSEARCH /usr/bin/ldapsearch
468 getconf RDIFFBACKUP /usr/bin/rdiff-backup
469 getconf CSTREAM /usr/bin/cstream
470 getconf MYSQLADMIN /usr/bin/mysqladmin
471 getconf MYSQL /usr/bin/mysql
472 getconf MYSQLHOTCOPY /usr/bin/mysqlhotcopy
473 getconf MYSQLDUMP /usr/bin/mysqldump
474 getconf PGSQLDUMP /usr/bin/pg_dump
475 getconf PGSQLDUMPALL /usr/bin/pg_dumpall
476 getconf PGSQLUSER postgres
477 getconf GZIP /bin/gzip
478 getconf RSYNC /usr/bin/rsync
479 getconf admingroup root
481 # initialize vservers support
482 # (get config variables and check real vservers availability)
483 init_vservers nodialog
485 if [ ! -d "$configdirectory" ]; then
486 echo "Configuration directory '$configdirectory' not found."
487 fatal "Configuration directory '$configdirectory' not found."
490 [ -f "$logfile" ] || touch $logfile
492 if [ "$UID" != "0" ]; then
493 echo "`basename $0` can only be run as root"
497 ## Process each configuration file
499 # by default, don't make files which are world or group readable.
502 # these globals are set by process_action()
510 if [ "$singlerun" ]; then
513 files=`find $configdirectory -follow -mindepth 1 -maxdepth 1 -type f ! -name '.*.swp' | sort -n`
515 if [ -z "$files" ]; then
516 fatal "No backup actions configured in '$configdirectory', run ninjahelper!"
520 for file in $files; do
521 [ -f "$file" ] || continue
522 [ "$halts" = "0" ] || continue
524 check_perms ${file%/*} # check containing dir
527 base=`basename $file`
528 if [ "${base:0:1}" == "0" -o "$suffix" == "disabled" ]; then
529 info "Skipping $file"
533 if [ -e "$scriptdirectory/$suffix" ]; then
534 process_action $file $suffix
536 error "Can't process file '$file': no handler script for suffix '$suffix'"
537 msg "*missing handler* -- $file"
541 ## mail the messages to the report address
543 if [ $actions_run == 0 ]; then doit=0
544 elif [ "$reportemail" == "" ]; then doit=0
545 elif [ $fatals != 0 ]; then doit=1
546 elif [ $errors != 0 ]; then doit=1
547 elif [ "$reportsuccess" == "yes" ]; then doit=1
548 elif [ "$reportwarning" == "yes" -a $warnings != 0 ]; then doit=1
552 if [ $doit == 1 ]; then
553 debug "send report to $reportemail"
555 [ $warnings == 0 ] || subject="WARNING"
556 [ $errors == 0 ] || subject="ERROR"
557 [ $fatals == 0 ] || subject="FAILED"
560 for ((i=0; i < ${#messages[@]} ; i++)); do
564 if [ "$reportspace" == "yes" ]; then
566 for i in $(ls "$configdirectory"); do
567 backuploc=$(grep ^directory "$configdirectory"/"$i" | @AWK@ '{print $3}')
568 if [ "$backuploc" != "$previous" -a -n "$backuploc" ]; then
570 previous="$backuploc"
574 } | mail -s "backupninja: $hostname $subject" $reportemail
577 if [ $actions_run != 0 ]; then
578 info "FINISHED: $actions_run actions run. $fatals fatal. $errors error. $warnings warning."
579 if [ "$halts" != "0" ]; then
580 info "Backup was halted prematurely. Some actions may not have run."
584 if [ -n "$reporthost" ]; then
585 debug "send $logfile to $reportuser@$reporthost:$reportdirectory"
586 rsync -qt $logfile $reportuser@$reporthost:$reportdirectory