Add config
[wizbackup.git] / wizbackup-driver
1 #!/bin/bash
2 #
3 # WizBackup Driver 1.1
4 # Copyright 2011 Bernie Innocenti <bernie@codewiz.org>
5 #
6 #  This program is free software: you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation, either version 3 of the License,
9 #  or (at your option) any later version.
10 #
11 #  This program is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 if [ $# -lt 2 ]
21 then
22         echo "Usage: $0 [MACHINE-TAB] [DEST]"
23         echo
24         echo "The MACHINE-TAB file contains hostnames of machines to be backed up, one per line."
25         echo "Empty lines and lines starting with '#' are ignored."
26         exit 1
27 fi
28
29 HOSTS=`cat $1 | egrep -v '^#|^ *$'`
30 DEST="$2"
31
32 LOGGROUP=`basename $1`
33 LOGDIR=/var/log/wizbackup
34 LOCKDIR=/var/lock/wizbackup
35 EXCLUDES_DIR="$DEST/EXCLUDE"
36
37 today=`date +"%Y%m%d"`
38 mkdir -p $LOGDIR
39 mkdir -p $LOCKDIR
40
41 if [ "`tty`" = "not a tty" ]; then
42         LOGFILE="$LOGDIR/$LOGGROUP-$today.log"
43 else
44         LOGFILE=/dev/stdout
45 fi
46
47 for host in $HOSTS; do
48         opts=""
49         for file in "$EXCLUDES_DIR/ALWAYS" "$EXCLUDES_DIR/$host"; do
50                 [ -f $file ] && opts="$opts --exclude-from $file"
51         done
52
53         start_time=$(date +%s)
54         flock -n $LOCKDIR/$host nice ionice -c2 -n6 \
55                 wizbackup "$host:/" "$DEST/$host/" $opts >>$LOGFILE 2>&1
56         result=$?
57         end_time=$(date +%s)
58
59         report="$(date +%s):$host:$(($end_time-$start_time)):$result"
60         echo $report >>$LOGDIR/$LOGGROUP-report-$today.log
61         [ $result != 0 ] && echo "$report" >>$LOGDIR/$LOGGROUP-fail-$today.log
62 done
63
64 # TODO: send fail log email report
65 # TODO: delete logs older than 45 days