1a5d0bc8bb06f24b0233ba46f7d0219e27a87ceb
[wizbackup.git] / wizbackup-driver
1 #!/bin/bash
2 #
3 # WizBackup Driver 1.0
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="--exclude-from $EXCLUDES_DIR/ALWAYS"
49         if [ -f "$EXCLUDES_DIR/$host" ]; then
50                 opts="$opts --exclude-from $EXCLUDES_DIR/$host"
51         fi
52
53         start_time=$(date +%s)
54         flock $LOCKDIR/$host wizbackup "$host:/" "$DEST/$host/" $opts >>$LOGFILE 2>&1
55         result=$?
56         end_time=$(date +%s)
57
58         report="$(date +%s):$host:$(($end_time-$start_time)):$result"
59         echo $report >>$LOGDIR/$LOGGROUP-report-$today.log
60         [ $result != 0 ] && echo "$report" >>$LOGDIR/$LOGGROUP-fail-$today.log
61 done
62
63 # TODO: send fail log email report
64 # TODO: delete logs older than 45 days