#!/bin/sh
#
# Backup script to create full, incremental or differential backups
# with only tar as backup program
#
# This version uses scp to copy the backup to a remote location,
# another version suitable for 1&1 Rootserver uses a simple
# ftp version and a .netrc for backup
#
# Version: 0.5
#
# written by: Ingo Schaefer - ingo (at) ingo (dash) schaefer (dot) de
#
# This file 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 any later version.
#
# This file 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 file; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  
# 02111-1307 USA
# 
#######################################################################


###     Configuration section      ###########################

# temporaerer Speicherort des Backup-Archivs
BACKUP_TMP="/cygdrive/c/backup/tar/"

# Backup-Modus (full/differential/incremental)
#    full 	   = jeden Tag Vollsicherung
#    differential  = jeden Tag Sicherung der Aenderungen seit letztem Full
#    incremental   = jeden Tag Sicherung der Aenderungen seit letzter Sicherung
#
# MySQL-Datenbanken werden jeden Tag gesichert, sofern MYSQLDUMPDIR gesetzt
#
BACKUP_MODE="incremental"
FULL_DAY="7"     # 1 = Montag

# Hostname des Backup-Servers
BACKUP_SERVER="backupserver.mydomain.com"
BACKUP_USER="backuser"
BACKUP_DIR="backup/clienthost"

# Einstellungen fuer MySQL
MYSQLHOST="localhost"
MYSQLUSER="backuser"
MYSQLPASS="backuserpassword"

#MYSQLDUMPDIR=""
MYSQLDUMPDIR="/cygdrive/c/backup/mysqldump/"
# uncomment and add databases to backup only some dbs, default: all databases
#MYSQLDB=""


# Verzeichnisse die gesichert werden sollen
# in diesem Beispiel sind dies die Kunden Webseiten und E-Mails
BACKUP_DATA="$MYSQLDUMPDIR /cygdrive/c/ /cygdrive/d/" 

# Timestamp-Dateien fuer Full/Daily backup
TIMESTAMP_DAILY="/cygdrive/c/backup/backup.daily.timestamp"
TIMESTAMP_FULL="/cygdrive/c/backup/backup.full.timestamp"

# loesch-Optionen
CLEAN_MYSQL_DAYS="30"
CLEAN_TAR_DAYS="10"

# machine specific path values - usually not needed
# 
MYSQLDUMP="/cygdrive/c/mysql/bin/mysqldump.exe"
DATE="/bin/date"
TAR="/bin/tar"

### END of Configuration section    ##########################
# executable script part - no modification needed after this line!

DOW=`$DATE +%u`
CURDATE=`$DATE +%F`

# do the mysqldump to specified dir
if [ -n "$MYSQLDUMPDIR" ] ; then
	MYSQLDUMPFILE="${MYSQLDUMPDIR}mysqldump.${CURDATE}.sql"
	MYSQLOPTIONS="--add-drop-table --complete-insert --disable-keys --lock-tables --debug "
	if [ -z $MYSQLDB ] ; then
		MYSQLDBSTRING="--all-databases"
	else
		MYSQLDBSTRING="--databases ${MYSQLDB}"
	fi
	$MYSQLDUMP $MYSQLOPTIONS --password=$MYSQLPASS --user=$MYSQLUSER --host=$MYSQLHOST $MYSQLDBSTRING > $MYSQLDUMPFILE
	if [ ! $? -eq 0 ] ; then
		echo "Errors dumping mysql tables."
	fi 
fi

# Name der Backup-Datei
BACKUP_FILE="${CURDATE}-backup.tar.gz"

# kompletter Pfad zum Backup
BACKUP="${BACKUP_TMP}${BACKUP_FILE}"

# alte UMASK sichern
UMASK=`umask`

umask 0077

if [ "$BACKUPMODE" = "full" -o "$DOW" = "$FULL_DAY" -o ! -f $TIMESTAMP_FULL ] ; then
	touch $TIMESTAMP_FULL
	$TAR --exclude="${BACKUP_TMP}/*" --exclude="*.vmem" -czf ${BACKUP} ${BACKUP_DATA}

	if [ ! $? -eq 0 ] ; then
		echo "something was going wrong with full backup"
		rm $TIMESTAMP_FULL
	fi 
	md5sum ${BACKUP} > ${BACKUP}.md5
else
	if [ "$BACKUPMODE" = "incremental" ] ; then
		REFTIME=`date -r $TIMESTAMP_DAILY +"%Y-%m-%d %H:%M:%S"`	
		touch $TIMESTAMP_DAILY
	else
		#diffential
		REFTIME=`date -r $TIMESTAMP_FULL +"%Y-%m-%d %H:%M:%S"`
		touch $TIMESTAMP_DAILY
	fi
	${TAR} --exclude="${BACKUP_TMP}/*" --newer "$REFTIME" -czf ${BACKUP} ${BACKUP_DATA} 2>/dev/null
	if [ ! $? -eq 0 ] ; then
                echo "something was going wrong with full backup"
                rm $TIMESTAMP_DAILY
        fi
	md5sum ${BACKUP} > ${BACKUP}.md5
fi
scp ${BACKUP}* ${BACKUP_USER}@${BACKUP_SERVER}:${BACKUP_DIR} >/dev/null


#cleanup local filesystem
find ${BACKUP_TMP} -mtime +${CLEAN_TAR_DAYS} -exec rm -f '{}' \;
if [ -n "$MYSQLDUMPDIR" ] ; then
	find ${MYSQLDUMPDIR} -mtime +${CLEAN_MYSQL_DAYS} -exec rm -f '{}' \;
fi

#rm -f ${BACKUP} ${BACKUP}.md5
umask ${UMASK}

