#!/bin/bash

backupDir="/m23/root-only/serverBackups/"

if [ -e /m23/root-only/serverBackup.conf ]
then
	. /m23/root-only/serverBackup.conf
else
	echo "/m23/root-only/serverBackup.conf NOT found!"
	exit 1
fi





checkForDebconf()
{
	#Force installation of debconf
	if [ ! -e /usr/bin/debconf-get-selections ]
	then
		export DEBIAN_FRONTEND=noninteractive
		apt-get update
		apt-get --allow-downgrades --allow-unauthenticated -y install debconf-utils
	fi
}





createBackupList()
{
	#Create the backup list
	backupList="/tmp/m23Backup$$.list"
	rm $backupList 2> /dev/null

	#Add symlinks in the tftp directory (these are the client dependend links to the Etherboot boot image)
		find /m23/tftp/ -type l >> $backupList

	#Add complete PXE client settings
		echo "/m23/tftp/pxelinux.cfg/" >> $backupList

	#Add the package status files of the clients
		echo "/m23/var/cache/clients/" >> $backupList

	#Add the bills an other data of m23Shared
		echo "/m23/var/m23shared/" >> $backupList

	#Add the http password
		echo "/m23/etc/" >> $backupList

	#Add the logs (if any)
		echo "/m23/log/" >> $backupList

	#Add openLDAP if not run on UCS
	if [ ! -f '/usr/sbin/udm' ]
	then
		echo "/var/lib/ldap/" >> $backupList
	fi

	#Add BackupPC settings
		echo "/etc/backuppc/" >> $backupList

	#Add the RAS tunnel settings
		ls /m23/bin/m23RemoteAdministrationServiceOpenTunnel-* >> $backupList

	#Add CloudStack configuration
		if [ -f /m23/inc/CloudStackConf.php ]
		then
			echo "/m23/inc/CloudStackConf.php" >> $backupList
		fi

	#Add mail configuration
		if [ -f /m23/inc/mailConf.php ]
		then
			echo "/m23/inc/mailConf.php" >> $backupList
		fi

	#Add home directories
		# Check for UCS
		if [ -f '/usr/sbin/udm' ]
		then
			echo "/home/grdmgpg/" >> $backupList
		else
			echo "/home/" >> $backupList
			echo "/root/" >> $backupList
		fi

	#Add user generated GUIs (if existing)
		if [ -d /m23/inc/userGUIAddons ]
		then
			echo "/m23/inc/userGUIAddons/" >> $backupList
		fi

	#Add user generated custom patches (if existing)
		if [ -d /m23/m23customPatch ]
		then
			echo "/m23/m23customPatch/" >> $backupList
		fi

	#Add firewall configuration
		if [ -f /m23/bin/firewall.sh ]
		then
			echo "/m23/bin/firewall.sh" >> $backupList
		fi

		if [ -f /m23/bin/firewall.iptables ]
		then
			echo "/m23/bin/firewall.iptables" >> $backupList
		fi

	#Add user addons and scripts (if existing)
		if [ -d /m23/data+scripts/packages/userPackages ]
		then
			echo "/m23/data+scripts/packages/userPackages/" >> $backupList
		fi

	#Add LDAP keytab, Kereros configuration and stash (master password) and FusionDirectory config
		for fn in /etc/ldap/ldap.keytab /etc/krb5.conf /etc/krb5kdc/stash /etc/fusiondirectory/fusiondirectory.conf
		do
			if [ -f $fn ]
			then
				echo "$fn" >> $backupList
			fi
		done

	#Add config files of phpMyAdmin and phpldapadmin
		echo "/m23/data+scripts/m23admin/phpMyAdmin/config.inc.php
/m23/data+scripts/m23admin/phpldapadmin/config.php" >> $backupList

	#Save the current IP of the server
		/m23/bin/serverInfoIP > /tmp/serverInfoIP.txt
		echo "/tmp/serverInfoIP.txt" >> $backupList

	export backupList
}





backupPackages()
{
	echo -n "Storing information about installed packages..."
		dpkg --get-selections | grep -v deinstall$ | tr -d [:blank:] | sed 's/install$//g' | awk -v ORS='' '{print($0" ")}' | bzip2 -9 -z -c > packages.bz2
	echo " done"
}





backupDebconf()
{
	echo -n "Storing packages configuration..."
	#Disable reconfiguration of the SSL and SSH keys to make the restored server use the same keys as the backupped server
		debconf-get-selections | \
		sed 's#m23\/configureSSL[\t ]*boolean[\t ]*true#m23\/configureSSL boolean false#g' | \
		sed 's#m23\/configureSSH[\t ]*boolean[\t ]*true#m23\/configureSSH boolean false#g' | \
		bzip2 -9 -z -c > debconf.bz2
	echo " done"
}





backupDB()
{
	echo -n "Storing m23 and m23captured databases..."
		dbuser=`/m23/bin/getMySQL-UserParam.sh`
		dbadmpassParam=`/m23/bin/getMySQL-PasswordParam.sh`

		mysqldump $dbuser $dbadmpassParam m23 | sed 's/ DEFAULT CHARSET=latin1//g' | bzip2 -9 -z -c > m23.sql.bz2
		mysqldump $dbuser $dbadmpassParam m23captured | sed 's/ DEFAULT CHARSET=latin1//g' | bzip2 -9 -z -c > m23captured.sql.bz2
	echo " done"
}



backupKerberos()
{
	
	if [ -f /etc/krb5.conf ]
	then
		kdb5_util dump -verbose kdb5.dump
		bzip2 -9 kdb5.dump
	fi
}



backupFiles()
{
	# Stop openLDAP to get a clean copy of the DB state
	/etc/init.d/slapd stop


	tarParam="cjp --same-owner -f"
	echo -n "Storing m23 config files..."
		echo -n ".svn" | tar $tarParam m23config.tar.bz2 -X- -T $backupList
	echo " done"
	
	
	echo -n "Storing /etc..."
		tar $tarParam etc.tar.bz2 /etc/*
	echo " done"

	# Start oepnLDAP again
	/etc/init.d/slapd start

}





uploadBackup()
{
	#Check if the backup should be encrypted for upload
	if [ $uploadServices != "-" ]
	then
		tar cfv "m23serverBackup-$backupName.tar" *
		cat "m23serverBackup-$backupName.tar" | sudo -u $CONF_GPG_USER gpg --homedir $CONF_GPG_HOME --trust-model always --batch --no-tty --yes --digest-algo sha1 -e -r "$gpgKey" --no-use-agent > m23serverBackup-$backupName.tar.gpg
		rm m23serverBackup-$backupName.tar
	fi
	
	#Upload with the selected uploader
	case $uploadServices in
		scp)
			scp -o VerifyHostKeyDNS=no -o PreferredAuthentications=publickey -o PasswordAuthentication=no -o CheckHostIP=no -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -C m23serverBackup-$backupName.tar.gpg $user@$scpServer:$scpStoreDirectory
		break;;
		
	esac
}





if [ -f /etc/krb5.conf ]
then
	# Stop Kerberos KDC
	service krb5-kdc stop
fi

checkForDebconf
createBackupList

backupName=`date +%F-%H-%M`
mkdir -p $backupDir
cd $backupDir
mkdir -p "$backupName"
cd "$backupName"

backupPackages
backupDebconf
backupDB
backupKerberos
backupFiles
uploadBackup

if [ -f /etc/krb5.conf ]
then
	# Start Kerberos KDC again
	service krb5-kdc start
fi