#!/bin/sh
#
# Copyright (C) 1997-2005 Daniel Dominik Rudnicki
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
#
#	begin		: Friday November 18 2005
#	copyright	: Daniel Dominik Rudnicki
#	author		: sardzent ( sardzent@pld-linux.org )
#	version		: 0.4
#	www		: http://www.sardzent.org/backup/
#
###########################################################
# Configuration						  #
###########################################################
#	who should get mail with report 
MAILADDR=backup@domain.foo
# 	what from date will be downloaded
BACKUPFROM=rsync://host.domain.foo:873
#	where to backup files
ARCHIVEROOT=/home/services/backup/host.domain.foo
#	what remote resources should be backuped
BACKUPRESOURCES="resource1 resource2 resourceN"
#	name of dir where current data is stored
CURRENT=current
#	nice level, 0<x<19
NICE_LEVEL="5"

###########################################################
# Paths for nedded binaries				  #
###########################################################
AWK="/bin/awk"
DU="/usr/bin/du"
INSTALL="/usr/bin/install"
MD5="/usr/bin/md5sum"
MUTT="/usr/bin/mutt"
NICE="/bin/nice"
TAR="/bin/tar"
RSYNC="/usr/bin/rsync"


###########################################################
# -- Nothing below this point should need modification -- #
###########################################################

INCREMENTDIR=`date +%Y-%m-%d[%H:%M:%S]`
HUMANDATE=`date +%H:%M:%S\ %A\ %d\ %B\ %Y`
HOSTNAME=`hostname`"."`domainname`

# create temp files
raport1=`/bin/mktemp`
log1=`/bin/mktemp`

# prepare headers for e-mail
echo $INCREMENTDIR > $log1
echo " backup date:	$HUMANDATE" > $raport1
echo " backup from:	$HOSTNAME" >> $raport1
echo "	REPORT:" >> $raport1
echo >> $raport1

# download each remote resource
for WHAT in $BACKUPRESOURCES; do
	# options for rsync
	OPTIONS="--force --ignore-errors --delete --backup --backup-dir=$ARCHIVEROOT/$INCREMENTDIR/$WHAT -av"
	# make dirs
	$INSTALL -d $ARCHIVEROOT/$CURRENT/$WHAT > /dev/null
	echo " Backup Resources:	$BACKUPFROM/$WHAT" >> $raport1
	# rsync
	$NICE -n $NICE_LEVEL $RSYNC $OPTIONS $BACKUPFROM/$WHAT $ARCHIVEROOT/$CURRENT/$WHAT >> $log1
	# if something new then notice that
	if [ -d $ARCHIVEROOT/$INCREMENTDIR/$WHAT ]; then
		echo "		files changed, compressing diff files" >> $raport1
		# compress
		$NICE -n $NICE_LEVEL $TAR -zcf $ARCHIVEROOT/$INCREMENTDIR/$WHAT.tar.gz $ARCHIVEROOT/$INCREMENTDIR/$WHAT 2> /dev/null
		# get size
		SIZE=`$DU -sh $ARCHIVEROOT/$INCREMENTDIR/$WHAT.tar.gz`
		# get checksum
		CHSUM=`$NICE -n $NICE_LEVEL $MD5 $ARCHIVEROOT/$INCREMENTDIR/$WHAT.tar.gz | $AWK ' { print $1 } '`
		echo "			$SIZE" >> $raport1
		echo "			md5sum	$CHSUM" >> $raport1
		# remove dir
		rm -rf $ARCHIVEROOT/$INCREMENTDIR/$WHAT > /dev/null
	fi
	echo >> $raport1
done
#
echo >> $raport1
echo "################################################" >> $raport1
echo "	Total backup size" >> $raport1
# count total backup size
$DU -sh $ARCHIVEROOT/* >> $raport1
echo >> $raport1
echo >> $raport1
# send e-mail with report
$MUTT $MAILADDR -a $log1 -s "[administartiva] Backup Report $HOSTNAME" < $raport1
# remove temp files
rm -f $raport1 > /dev/null
rm -f $log1 > /dev/null
