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
196 getconf logfile /var/log/backupninja.log
197 getconf SLAPCAT /usr/sbin/slapcat
198 getconf RDIFFBACKUP /usr/bin/rdiff-backup
199 getconf MYSQL /usr/bin/mysql
200 getconf MYSQLHOTCOPY /usr/bin/mysqlhotcopy
201 getconf MYSQLDUMP /usr/bin/mysqldump
202 getconf GZIP /bin/gzip
204 [ -d "$configdirectory" ] || fatal "Configuration directory '$configdirectory' not found."
205 [ `id -u` == "0" ] || fatal "Can only be run as root"
207 ## Process each configuration file
209 debug 1 "====== starting at "`date`" ======"
211 # by default, don't make files which are world or group readable.
214 for file in $configdirectory/*; do
217 if [ "$perms" != "------" ]; then
218 fatal "Configuration files must not be group or world readable! Dying on file $file"
220 if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then
221 fatal "Configuration files must be owned by root! Dying on file $file"
224 base=`basename $file`
225 if [ "${base:0:1}" == "0" ]; then
226 debug 1 "Skipping $file"
229 debug 1 "Processing $file"
232 if [ -e "$scriptdir/$suffix" ]; then
235 ret=`( . $scriptdir/$suffix $file )`
237 warnings=`echo $ret | grep -e "^Warning: " | wc -l`
238 errors=`echo $ret | grep -e "^Error: \|^Fatal: " | wc -l`
239 if [ $errors != 0 ]; then
240 msg "*failed* -- $file"
241 error="$error\n== errors from $file ==\n\n$ret\n"
242 elif [ $warnings != 0 ]; then
243 msg "*warning* -- $file"
244 error="$error\n== warnings from $file ==\n\n$ret\n"
245 elif [ $retcode == 0 ]; then
246 msg "success -- $file"
248 msg "unknown -- $file"
252 debug 3 "Can't process file '$file': no handler script for suffix '$suffix'"
253 msg "*missing handler* -- $file"
257 ## mail the messages to the report address
259 if [ "$reportemail" != "" ]; then
262 for ((i=0; i < ${#messages[@]} ; i++)); do
266 } | mail $reportemail -s "backupninja: $hostname"
269 debug 1 "====== finished at "`date`" ======"
271 ############################################################