10090c90482ce3a5970e41a6c730971dd217e26c
[wizbackup.git] / wizbackup-driver
1 #!/bin/bash
2 #
3 # WizBackup Driver 1.0
4 # Copyleft 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 if [ $# -lt 1 ]
12 then
13         echo "Usage: $0 [MACHINE-TAB]"
14         echo
15         echo "The MACHINE-TAB file contains hostnames of machines to be backed up, one per line."
16         echo "Empty lines and lines starting with '#' are ignored."
17         exit 1
18 fi
19
20 HOSTS=`cat $1 | egrep -v '^#|^ *$'`
21
22 LOGGROUP=`basename $1`
23 LOGDIR=/var/log/wizbackup
24 LOCKDIR=/var/lock/wizbackup
25 EXCLUDES_DIR=/root/rsync-backups/exclude
26 DEST=/backup
27
28 today=`date +"%Y%m%d"`
29 mkdir -p $LOGDIR
30 mkdir -p $LOCKDIR
31
32 if [ "`tty`" = "not a tty" ]; then
33         OUTLOGFILE="$LOGDIR/$LOGGROUP-$today.log"
34 else
35         LOGFILE=/dev/stdout
36 fi
37
38 for host in $HOSTS; do
39         opts="--exclude-from $EXCLUDES_DIR/ALWAYS"
40         if [ -f "$EXCLUDES_DIR/$host" ]; then
41                 opts="$opts --exclude-from $EXCLUDES_DIR/$host"
42         fi
43
44         start_time=$(date +%s)
45         flock $LOCKDIR/$host wizbackup "$host:/" "$DEST/$host/" $opts >>$LOGFILE 2>&1
46         result=$?
47         end_time=$(date +%s)
48
49         report="$(date):$host:$(($end_time-$start_time)):$result"
50         echo $report >>$LOGDIR/$LOGGROUP-report-$today.log
51         [ $result != 0 ] && echo "$report" >>$LOGDIR/$LOGGROUP-fail-$today.log
52 done
53
54 # TODO: send fail log email report
55 # TODO: delete logs older than 45 days