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 # enforces very strict permissions on configuration file $file.
140 function check_perms() {
142 local perms=`ls -ld $file`
144 if [ "$perms" != "------" ]; then
145 fatal "Configuration files must not be group or world readable! Dying on file $file"
147 if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then
148 fatal "Configuration files must be owned by root! Dying on file $file"
152 # simple lowercase function
154 echo "$1" | tr [:upper:] [:lower:]
157 # simple to integer function
159 echo "$1" | tr [:alpha:] -d
163 # function isnow(): returns 1 if the time/day passed as $1 matches
164 # the current time/day.
166 # format is <day> at <time>:
172 # we grab the current time once, since processing
173 # all the configs might take more than an hour.
176 nowdayofweek=`date +%A`
177 nowdayofweek=`tolower "$nowdayofweek"`
182 whendayofweek=$1; at=$2; whentime=$3;
183 whenday=`toint "$whendayofweek"`
184 whendayofweek=`tolower "$whendayofweek"`
185 whentime=`echo "$whentime" | sed 's/:[0-9][0-9]$//'`
187 if [ "$whendayofweek" == "everyday" ]; then
188 whendayofweek=$nowdayofweek
191 if [ "$whenday" == "" ]; then
192 if [ "$whendayofweek" != "$nowdayofweek" ]; then
193 whendayofweek=${whendayofweek%s}
194 if [ "$whendayofweek" != "$nowdayofweek" ]; then
198 elif [ "$whenday" != "$nowday" ]; then
202 [ "$at" == "at" ] || return 0
203 [ "$whentime" == "$nowtime" ] || return 0
208 #####################################################
211 ## process command line options
213 if [ "$1" == "--help" ]; then
214 HELP=1;DEBUG=1;loglevel=4
216 while getopts h,f:,d,t option
219 h) HELP=1;DEBUG=1;loglevel=4;;
220 d) DEBUG=1;loglevel=4;;
221 f) CONFFILE="$OPTARG";;
231 if [ "$HELP" == 1 ]; then
234 This script allows you to coordinate system backup by dropping a few
235 simple configuration files into /etc/backup.d/. In general, this script
236 is run from a cron job late at night.
238 The following options are available:
240 -d Run in debug mode, where all log messages are output to the current shell.
241 -f <file> Use <file> for the main configuration instead of /etc/backupninja.conf
242 -t Run in test mode, no actions are actually taken.
244 When using colored output, there are:
246 debug 0 "Debugging info (when run with -d)"
247 debug 1 "Informational messages (verbosity level 4)"
248 debug 2 "Warnings (verbosity level 3 and up)"
249 debug 3 "Errors (verbosity level 2 and up)"
250 debug 4 "Fatal, halting errors (always shown)"
254 ## Load and confirm basic configuration values
257 [ -r "$CONFFILE" ] || fatal "Configuration file $CONFFILE not found."
258 scriptdir=`grep scriptdirectory $CONFFILE | awk '{print $3}'`
259 [ -n "$scriptdir" ] || fatal "Cound not find entry 'scriptdirectory' in $CONFFILE."
260 [ -d "$scriptdir" ] || fatal "Script directory $scriptdir not found."
263 # get global config options (second param is the default)
264 getconf configdirectory /etc/backup.d
266 getconf reportsuccess yes
267 getconf reportwarning yes
269 getconf when "Everyday at 01:00"
271 getconf logfile /var/log/backupninja.log
272 getconf SLAPCAT /usr/sbin/slapcat
273 getconf RDIFFBACKUP /usr/bin/rdiff-backup
274 getconf MYSQL /usr/bin/mysql
275 getconf MYSQLHOTCOPY /usr/bin/mysqlhotcopy
276 getconf MYSQLDUMP /usr/bin/mysqldump
277 getconf GZIP /bin/gzip
279 [ -d "$configdirectory" ] || fatal "Configuration directory '$configdirectory' not found."
280 [ `id -u` == "0" ] || fatal "Can only be run as root"
282 ## Process each configuration file
284 debug 1 "====== starting at "`date`" ======"
286 # by default, don't make files which are world or group readable.
291 for file in $configdirectory/*; do
292 [ -f $file ] || continue;
296 base=`basename $file`
297 if [ "${base:0:1}" == "0" ]; then
298 debug 1 "Skipping $file"
301 debug 1 "Processing $file"
304 if [ -e "$scriptdir/$suffix" ]; then
307 # skip over this config if "when" option
308 # is not set to the current time.
309 getconf when "$defaultwhen"
316 if [ $ret == 0 ]; then
317 debug 0 "skipping $file because it is not $w"
320 debug 0 "running $file because it is $w"
328 ret=`( . $scriptdir/$suffix $file )`
330 warnings=`echo $ret | grep -e "^Warning: " | wc -l`
331 errors=`echo $ret | grep -e "^Error: \|^Fatal: " | wc -l`
332 if [ $errors != 0 ]; then
333 msg "*failed* -- $file"
334 errormsg="$error\n== errors from $file ==\n\n$ret\n"
335 elif [ $warnings != 0 ]; then
336 msg "*warning* -- $file"
337 errormsg="$error\n== warnings from $file ==\n\n$ret\n"
338 elif [ $retcode == 0 ]; then
339 msg "success -- $file"
341 msg "unknown -- $file"
345 debug 3 "Can't process file '$file': no handler script for suffix '$suffix'"
346 msg "*missing handler* -- $file"
350 ## mail the messages to the report address
352 if [ "$reportemail" == "" ]; then doit=0
353 elif [ $errors != 0 ]; then doit=1
354 elif [ "$reportsuccess" == "yes" ]; then doit=1
355 elif [ "$reportwarning" == "yes" -a $warnings != 0 ]; then doit=1
359 if [ $doit == 1 ]; then
362 for ((i=0; i < ${#messages[@]} ; i++)); do
366 } | mail $reportemail -s "backupninja: $hostname"
369 debug 1 "====== finished at "`date`" ======"
371 ############################################################