+function usage() {
+ cat << EOF
+$0 usage:
+This script allows you to coordinate system backup by dropping a few
+simple configuration files into /etc/backup.d/. Typically, this
+script is run hourly from cron.
+
+The following options are available:
+-h, --help This usage message
+-d, --debug Run in debug mode, where all log messages are
+ output to the current shell.
+-f, --conffile FILE Use FILE for the main configuration instead
+ of /etc/backupninja.conf
+-t, --test Test run mode. This will test if the backup
+ could run, without actually preforming any
+ backups. For example, it will attempt to authenticate
+ or test that ssh keys are set correctly.
+-n, --now Perform actions now, instead of when they might
+ be scheduled. No output will be created unless also
+ run with -d.
+ --run FILE Execute the specified action file and then exit.
+ Also puts backupninja in debug mode.
+
+When in debug mode, output to the console will be colored:
+EOF
+ debug=1
+ debug "Debugging info (when run with -d)"
+ info "Informational messages (verbosity level 4)"
+ warning "Warnings (verbosity level 3 and up)"
+ error "Errors (verbosity level 2 and up)"
+ fatal "Fatal, halting errors (always shown)"
+}
+
+##
+## this function handles the running of a backup action
+##
+## these globals are modified:
+## fatals, errors, warnings, actions_run, errormsg
+##
+
+function process_action() {
+ local file="$1"
+ local suffix="$2"
+
+ setfile $file
+
+ # skip over this config if "when" option
+ # is not set to the current time.
+ getconf when "$defaultwhen"
+ if [ "$processnow" == 1 ]; then
+ info ">>>> starting action $file (because of --now)"
+ elif [ "$when" == "hourly" ]; then
+ info ">>>> starting action $file (because 'when = hourly')"
+ else
+ IFS=$'\t\n'
+ for w in $when; do
+ IFS=$' \t\n'
+ isnow "$w"
+ ret=$?
+ IFS=$'\t\n'
+ if [ $ret == 0 ]; then
+ debug "skipping $file because it is not $w"
+ return
+ else
+ info ">>>> starting action $file (because it is $w)"
+ fi
+ done
+ IFS=$' \t\n'
+ fi
+
+ let "actions_run += 1"
+
+ # call the handler:
+ local bufferfile="/tmp/backupninja.buffer.$$"
+ echo "" > $bufferfile
+ echo_debug_msg=1
+ (
+ . $scriptdir/$suffix $file
+ ) 2>&1 | (
+ while read a; do
+ echo $a >> $bufferfile
+ [ $debug ] && colorize "$a"
+ done
+ )
+ retcode=$?
+ # ^^^^^^^^ we have a problem! we can't grab the return code "$?". grrr.
+ echo_debug_msg=0
+
+ _warnings=`cat $bufferfile | grep "^Warning: " | wc -l`
+ _errors=`cat $bufferfile | grep "^Error: " | wc -l`
+ _fatals=`cat $bufferfile | grep "^Fatal: " | wc -l`
+
+ ret=`grep "\(^Warning: \|^Error: \|^Fatal: \)" $bufferfile`
+ rm $bufferfile
+ if [ $_fatals != 0 ]; then
+ msg "*failed* -- $file"
+ errormsg="$errormsg\n== fatal errors from $file ==\n\n$ret\n"
+ passthru "Fatal: <<<< finished action $file: FAILED"
+ elif [ $_errors != 0 ]; then
+ msg "*error* -- $file"
+ errormsg="$errormsg\n== errors from $file ==\n\n$ret\n"
+ error "<<<< finished action $file: ERROR"
+ elif [ $_warnings != 0 ]; then
+ msg "*warning* -- $file"
+ errormsg="$errormsg\n== warnings from $file ==\n\n$ret\n"
+ warning "<<<< finished action $file: WARNING"
+ else
+ msg "success -- $file"
+ info "<<<< finished action $file: SUCCESS"
+ fi
+
+ let "fatals += _fatals"
+ let "errors += _errors"
+ let "warnings += _warnings"
+}
+