#!/bin/bash # script to run once a day to: # a. say which backup drive is connected # b. generate an rsnapshot du report # c. collect accumulated hourly logs # d. mail it all to the rsnapshot admins # must be run as root # assumes /etc/crontab something like this: #0 */8 * * * root /usr/bin/rsnapshot hourly 2>&1 | \ # tee /tmp/rsnapreport-hourly.log | /usr/bin/rsnapreport.pl \ # >>/root/cron/rsnapshot-hourly.log && /bin/echo >> \ # /root/cron/rsnapshot-hourly.log #50 23 * * * root /usr/bin/rsnapshot daily > /tmp/rsnapreport-daily.log #40 23 * * 6 root /usr/bin/rsnapshot weekly > /tmp/rsnapshot-weekly.log #30 23 1 * * root /usr/bin/rsnapshot monthly > /tmp/rsnapshot-monthly.log #1 6 * * * root /usr/local/sbin/rsnapshot-du AdminEmail=rsnapshot RsnapDuCmd="/usr/bin/rsnapshot du" BackupDevice=/dev/sdd1 BackupDir=/misc/backup FsLabel="" ScriptBase=$(basename $0) LogFile=/var/log/rsnapshot HourlyLogs=/root/cron/rsnapshot-hourly.log # are we running as root? [ $(id -u) -eq 0 ] || \ { echo "$0 must be run as root" >&2; exit 1; }; # clean up if we abort. trap "rm -f $RsnapDuFile; exit 3" TERM INT # make a tempfile to hold the du output so we can mail it later. RsnapDuFile=$(mktemp -t ${ScriptBase}.XXXXX) || \ { echo "failed to create tempfile." >&2; exit 1; }; # check that one of the backup drives is connected. FsLabel=$(/sbin/e2label $BackupDevice) || { echo "failed to get label of $BackupDevice - external drive not connected?" >&2; \ exit 1; }; echo -n "rsnapshot backup disk usage for " > $RsnapDuFile date +%D >> $RsnapDuFile echo >> $RsnapDuFile echo "Attached backup disk: $FsLabel" >> $RsnapDuFile echo >> $RsnapDuFile # Check amount of free space on the backup drive. DfLine=$(df -h $BackupDir | tail -1) # convert DfLine into a bash array... DfLine=(${DfLine// / }) # "disk: ${DfLine[0]}" # "size: ${DfLine[1]}" # "used: ${DfLine[2]}" # "avail: ${DfLine[3]}" # "cap: ${DfLine[4]}" # "mount: ${DfLine[5]}" echo -n ${DfLine[3]} >> $RsnapDuFile echo " free space remaining on $FsLabel" >> $RsnapDuFile echo >> $RsnapDuFile # generate the rsnapshot du report echo "$RsnapDuCmd says:" >> $RsnapDuFile echo >> $RsnapDuFile $RsnapDuCmd >> $RsnapDuFile || { echo "problem running $RsnapDuCmd" >&2; exit 1; }; # Dump collected hourly logs into the report. echo >> $RsnapDuFile echo "Hourly rsnapshot logs from the last 24 hours:" >> $RsnapDuFile echo >> $RsnapDuFile cat $HourlyLogs >> $RsnapDuFile # Reset until next run. rm -f $HourlyLogs # ok now we are ready to mail and delete the tempfile. cat $RsnapDuFile | mail -s "rsnapshot disk usage report" $AdminEmail rm -f $RsnapDuFile # log which disk is attached, might be useful for later stats LogDate=$(date "+[%d/%b/%Y:%H:%M:%S]") echo "$LogDate backup disk $FsLabel is attached" >> $LogFile exit 0