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 #####################################################
23 CONFFILE="/etc/backupninja.conf"
26 #####################################################
29 function setupcolors() {
30 if [ "$USECOLOURS" == 1 ]
45 returnstring=`$@ 2>&1`
47 RUNERRORS=$[RUNERRORS+RUNERROR]
48 if [ "$RUNERROR" != 0 ]; then
49 debug 3 "Exitcode $RUNERROR returned when running: $@"
50 debug 3 "$returnstring"
52 debug 0 "$returnstring"
57 # We have the following debug levels:
59 # 1 - normal messages - green
60 # 2 - warnings - yellow
63 # First variable passed is the error level, all others are printed
65 # if 1, echo out all warnings, errors, or fatal
66 # used to capture output from handlers
71 [ ${#@} -gt 1 ] || return
73 TYPES=(Debug Info Warning Error Fatal)
74 COLOURS=($BLUE $GREEN $YELLOW $RED $PURPLE)
76 colour=${COLOURS[$type]}
79 if [ "$print" -lt "$loglevel" -o "$DEBUG" == 1 ]; then
80 if [ -z "$logfile" ]; then
81 echo -e "${colour}${TYPES[$type]}: $@${OFF}" >&2
83 if [ "$DEBUG" == 1 -o "$type" == 4 ]; then
84 echo -e "${colour}${TYPES[$type]}: $@${OFF}" >&2
86 echo -e "${colour}${TYPES[$type]}: $@${OFF}" >> $logfile
89 if [ "$echo_debug_msg" != "0" -a "$type" -gt "1" ]; then
90 echo -e "${TYPES[$type]}: $@"
101 messages[$msgcount]=$1
109 function setsection() {
114 # sets a global var with name equal to $1
115 # to the value of the configuration parameter $1
121 ret=`awk -f $scriptdir/parseini S=$CURRENT_SECTION P=$CURRENT_PARAM $CURRENT_CONF_FILE`
122 # if nothing is returned, set the default
123 if [ "$ret" == "" -a "$2" != "" ]; then
127 # replace * with %, so that it is not globbed.
128 ret="${ret//\\*/__star__}"
130 # this is weird, but single quotes are needed to
131 # allow for returned values with spaces. $ret is still expanded
132 # because it is in an 'eval' statement.
137 #####################################################
140 ## process command line options
142 if [ "$1" == "--help" ]; then
143 HELP=1;DEBUG=1;loglevel=4
145 while getopts h,f:,d,t option
148 h) HELP=1;DEBUG=1;loglevel=4;;
149 d) DEBUG=1;loglevel=4;;
150 f) CONFFILE="$OPTARG";;
160 if [ "$HELP" == 1 ]; then
163 This script allows you to coordinate system backup by dropping a few
164 simple configuration files into /etc/backup.d/. In general, this script
165 is run from a cron job late at night.
167 The following options are available:
169 -d Run in debug mode, where all log messages are output to the current shell.
170 -f <file> Use <file> for the main configuration instead of /etc/backupninja.conf
171 -t Run in test mode, no actions are actually taken.
173 When using colored output, there are:
175 debug 0 "Debugging info (when run with -d)"
176 debug 1 "Informational messages (verbosity level 4)"
177 debug 2 "Warnings (verbosity level 3 and up)"
178 debug 3 "Errors (verbosity level 2 and up)"
179 debug 4 "Fatal, halting errors (always shown)"
183 ## Load and confirm basic configuration values
186 [ -r "$CONFFILE" ] || fatal "Configuration file $CONFFILE not found."
187 scriptdir=`grep scriptdirectory $CONFFILE | awk '{print $3}'`
188 [ -n "$scriptdir" ] || fatal "Cound not find entry 'scriptdirectory' in $CONFFILE."
189 [ -d "$scriptdir" ] || fatal "Script directory $scriptdir not found."
192 # get global config options (second param is the default)
193 getconf configdirectory /etc/backup.d
195 getconf reportsuccess yes
196 getconf reportwarning yes
198 getconf logfile /var/log/backupninja.log
199 getconf SLAPCAT /usr/sbin/slapcat
200 getconf RDIFFBACKUP /usr/bin/rdiff-backup
201 getconf MYSQL /usr/bin/mysql
202 getconf MYSQLHOTCOPY /usr/bin/mysqlhotcopy
203 getconf MYSQLDUMP /usr/bin/mysqldump
204 getconf GZIP /bin/gzip
206 [ -d "$configdirectory" ] || fatal "Configuration directory '$configdirectory' not found."
207 [ `id -u` == "0" ] || fatal "Can only be run as root"
209 ## Process each configuration file
211 debug 1 "====== starting at "`date`" ======"
213 # by default, don't make files which are world or group readable.
216 for file in $configdirectory/*; do
217 [ -f $file ] || continue;
221 if [ "$perms" != "------" ]; then
222 fatal "Configuration files must not be group or world readable! Dying on file $file"
224 if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then
225 fatal "Configuration files must be owned by root! Dying on file $file"
228 base=`basename $file`
229 if [ "${base:0:1}" == "0" ]; then
230 debug 1 "Skipping $file"
233 debug 1 "Processing $file"
236 if [ -e "$scriptdir/$suffix" ]; then
239 ret=`( . $scriptdir/$suffix $file )`
241 warnings=`echo $ret | grep -e "^Warning: " | wc -l`
242 errors=`echo $ret | grep -e "^Error: \|^Fatal: " | wc -l`
243 if [ $errors != 0 ]; then
244 msg "*failed* -- $file"
245 errormsg="$error\n== errors from $file ==\n\n$ret\n"
246 elif [ $warnings != 0 ]; then
247 msg "*warning* -- $file"
248 errormsg="$error\n== warnings from $file ==\n\n$ret\n"
249 elif [ $retcode == 0 ]; then
250 msg "success -- $file"
252 msg "unknown -- $file"
256 debug 3 "Can't process file '$file': no handler script for suffix '$suffix'"
257 msg "*missing handler* -- $file"
261 ## mail the messages to the report address
263 if [ "$reportemail" == "" ]; then doit=0
264 elif [ $errors != 0 ]; then doit=1
265 elif [ "$reportsuccess" == "yes" ]; then doit=1
266 elif [ "$reportwarning" == "yes" -a $warnings != 0 ]; then doit=1
270 if [ $doit == 1 ]; then
273 for ((i=0; i < ${#messages[@]} ; i++)); do
277 } | mail $reportemail -s "backupninja: $hostname"
280 debug 1 "====== finished at "`date`" ======"
282 ############################################################