#!/bin/bash
# oss_recover.sh
#
# $Id: oss_recover.sh,v 1.4 2006/08/30 00:43:51 lrupp Exp $

DEBUG="yes"
CONFIG=/etc/sysconfig/schoolserver
LOGFILE=/var/log/oss-recover.log

function usage (){
    echo
    echo "Usage: `basename $0` [OPTIONS]"
    echo
	echo "       --help        : this message"
    echo "       --egroupware  : recover eGroupware database"
    echo "       --home        : recover /home-directory"
	echo "       --joomla      : recover Joomla database"
    echo "       --ldap        : recover LDAP database"
	echo "       --mail        : recover Emails"
	echo "       --moodle      : revover Moodle data"
    echo "       --openxchange : recover openXchange database"
	echo "       --proxy       : recover proxy settings"
	echo "       --samba       : recover samba settings"
	echo "       --ssh         : recover ssh settings"
	echo "       --ssl         : recover ssl settings"
    echo "       --tfk         : recover time4kids database"
	echo
	echo "       default: recover all above if possible"
	echo
    exit $1
}

function expand_network (){
	case $1 in
		10)
			TMP="10\.0\."
		;;
		192)
			TMP="192\.168\."
		;;
		172)
			TMP="172\.16\."
		;;
		*)
		;;
	esac
}

# the sorting is done in alphabetical order via the options (see PARAM variable below)
EGROUPWARE="no"
HOMEDIRS="no"
JOOMLA="no"
LDAP="no"
EMAIL="no"
MOODLE="no"
OPENXCHANGE="no"
PROXY="no"
SAMBA="no"
SSH="no"
SSL="no"
TFK="no"

if [ -z $1 ]; then
	EGROUPWARE="yes"
	HOMEDIRS="yes"
	JOOMLA="yes"
	LDAP="yes"
	EMAIL="yes"
	MOODLE="yes"
	OPENXCHANGE="yes"
	PROXY="yes"	
	SAMBA="yes"
	SSH="yes"
	SSL="yes"
	TFK="yes"
fi

for PARAM in $* ; do
    case $PARAM in
        -h|-H|--help)    
			usage 0
        	;;
		--egroupware)
			EGROUPWARE="yes"
			;;
		--home)
			HOMEDIRS="yes"
			;;
		--joomla)
			JOOMLA="yes"
			;;
		--ldap)
			LDAP="yes"
			;;
		--mail)
			EMAIL="yes"
			;;
		--moodle)
			MOODLE="yes"
			;;
		--openxchange|--openexchange)	
			OPENXCHANGE="yes"
			;;
		--proxy)
			PROXY="yes"
			;;
		--samba)
			SAMBA="yes"
			;;
		--ssh)	
			SSH="yes"
			;;
		--ssl)
			SSL="yes"
			;;
		--tfk)
			TFK="yes"
			;;
        \?)     
			echo "UNKNOWN argument \"-$OPTARG\"." >&2
            usage 1
            ;;
		:)      
			echo "Option \"-$OPTARG\" needs an argument." >&2
            usage 1
            ;;
        *)
		    echo "Wrong arguments" >&2
            usage 1
            ;;
    esac
done

if [ ! -f $CONFIG ]; then
    echo -e "\033[0;31;1mThis script is for Open School Server only!\033[\0m"
    echo -e "\033[0;31;1m*********         exiting         *********\033[\0m"
    exit 1
fi

. $CONFIG

# egroupware
if [ "$SCHOOL_USE_EGROUPWARE" = "yes" -a -e $SCHOOL_BACKUP_FULL_DIR/egroupware.gz -a "$EGROUPWARE" = "yes" ]; then
  echo "restoring eGroupware" >> $LOGFILE
  gunzip $SCHOOL_BACKUP_FULL_DIR/egroupware.gz
  mysql -f egroupware < $SCHOOL_BACKUP_FULL_DIR/egroupware  >> $LOGFILE  2>&1
fi

# home
if [ -d $SCHOOL_BACKUP_FULL_DIR/home -a "$HOMEDIRS" = "yes" ]; then
  	echo "restoring /home-directories" >> $LOGFILE
    rsync -aH $SCHOOL_BACKUP_FULL_DIR/home/ /home/ >> $LOGFILE  2>&1
	if [ -e $SCHOOL_BACKUP_FULL_DIR/home_acls.gz -a "$HOMEDIRS" = "yes" ]; then
	  gunzip $SCHOOL_BACKUP_FULL_DIR/home_acls.gz
	  setacl --restore=$SCHOOL_BACKUP_FULL_DIR/home_facls >> $LOGFILE  2>&1
	fi
fi


# joomla
if [ "$SCHOOL_USE_JOOMLA" = "yes" -a -e $SCHOOL_BACKUP_FULL_DIR/joomla.gz -a "$JOOMLA" = "yes" ]; then
	echo "restoring Joomla" >> $LOGFILE
	gunzip $SCHOOL_BACKUP_FULL_DIR/joomla.gz
	mysql -f joomla < $SCHOOL_BACKUP_FULL_DIR/joomla >> $LOGFILE  2>&1
fi

# ldap
if [ -e $SCHOOL_BACKUP_FULL_DIR/SLAPCAT.gz -a "$LDAP" = "yes" ]; then	
	echo "restoring LDAP database" >> $LOGFILE
	gunzip $SCHOOL_BACKUP_FULL_DIR/SLAPCAT.gz
	/etc/init.d/ldap stop
	tar czf /var/lib/ldap.org.tgz /var/lib/ldap/
	rm /var/lib/ldap/*
	/usr/sbin/slapadd < SLAPCAT >> $LOGFILE  2>&1
	/etc/init.d/ldap start
fi

# email
if [ -d $SCHOOL_BACKUP_FULL_DIR/var/spool/imap/ -a "$EMAIL" = "yes" ]; then
	echo "restoring Email (includes settings)" >> $LOGFILE
	pushd $SCHOOL_BACKUP_FULL_DIR 1>/dev/null
	for i in var/spool/imap/ var/lib/imap/ ; do
	    echo "syncing Mail"  >> $LOGFILE  2>&1
	    if [ -d $i ]; then
		    rsync -aA $i /$i >> $LOGFILE  2>&1
	    fi
	done
	popd 1>/dev/null
	if [ -f $SCHOOL_BACKUP_FULL_DIR/etc/uucp/config ]; then
	   	rsync -aH $SCHOOL_BACKUP_FULL_DIR/etc/uucp/ /etc/uucp/ >> $LOGFILE  2>&1
	fi
	if [ -d $SCHOOL_BACKUP_FULL_DIR/etc/postfix ]; then
	   	rsync -aH $SCHOOL_BACKUP_FULL_DIR/etc/postfix/ /etc/postfix/ >> $LOGFILE  2>&1
		rsync -a $SCHOOL_BACKUP_FULL_DIR/etc/aliases /etc/ >> $LOGFILE  2>&1
	fi
fi

# moodle
if [ "$SCHOOL_USE_MOODLE" = "yes" -a -e $SCHOOL_BACKUP_FULL_DIR/moodle.gz -a "$MOODLE" = "yes" ]; then
	echo "restoring Moodle data" >> $LOGFILE
	gunzip $SCHOOL_BACKUP_FULL_DIR/moodle.gz
	mysql -f moodle < $SCHOOL_BACKUP_FULL_DIR/moodle >> $LOGFILE  2>&1
	rsync -a $SCHOOL_BACKUP_FULL_DIR/etc/moodle-config.php /etc/
	if [ -d $SCHOOL_BACKUP_FULL_DIR/srv/www/moodledata ]; then
	  rsync -aH $SCHOOL_BACKUP_FULL_DIR/srv/www/moodledata /srv/www/moodledata >> $LOGFILE  2>&1
	fi
fi

# openxchange
if [ "$SCHOOL_USE_OX" = "yes" -a -e $SCHOOL_BACKUP_FULL_DIR/openexchange.gz -a "$OPENXCHANGE" = "yes" ]; then
	echo "restoring openXchange" >> $LOGFILE
	gunzip $SCHOOL_BACKUP_FULL_DIR/openexchange.gz
	su postgres -c "psql -d openexchange -f $SCHOOL_BACKUP_FULL_DIR/openexchange" >> $LOGFILE  2>&1
fi

if [ -d $SCHOOL_BACKUP_FULL_DIR/var/lib/squidGuard/db -a "$PROXY" = "yes" ]; then
	echo "restoring Proxy settings" >> $LOGFILE
	rsync -aH $SCHOOL_BACKUP_FULL_DIR/var/lib/squidGuard/db/custom/ /var/lib/squidGuard/db/custom/ >> $LOGFILE  2>&1
	rsync -a  $SCHOOL_BACKUP_FULL_DIR/etc/squid/ /etc/squid/ >> $LOGFILE  2>&1
	squidGuard -c /etc/squid/squidguard.conf -C all
	rcsquid restart
fi

# samba
if [ -d $SCHOOL_BACKUP_FULL_DIR/var/lib/samba -a "$SAMBA" = "yes" ]; then
	echo "restoring samba settings" >> $LOGFILE
	rcsmb stop 1>/dev/null
	rcnmb stop 1>/dev/null
	if pidof smbd >/dev/null; then 
		echo "WARNING: Couldn't stop smbd!" >> $LOGFILE
		killall -9 smbd
		killall -9 nmbd
	fi
	rsync -a $SCHOOL_BACKUP_FULL_DIR/etc/samba/ /etc/samba/ >> $LOGFILE  2>&1
	rsync -a $SCHOOL_BACKUP_FULL_DIR/var/lib/samba/ /var/lib/samba/ >> $LOGFILE  2>&1
	pushd /var/lib/samba/ 1>/dev/null
	tdbbackup -v *.tdb >> $LOGFILE  2>&1
	popd 1>/dev/null
	pushd /etc/samba 1>/dev/null
	tdbbackup -v *.tdb >> $LOGFILE  2>&1
	popd 1>/dev/null
	rcnmb start 1>/dev/null
	rcsmb start 1>/dev/null
fi

# SSH
if [ -d $SCHOOL_BACKUP_FULL_DIR/root -a "SSH" = "yes" ]; then
	echo "restoring SSH settings" >> $LOGFILE
	tar cfz /root.org.tgz /root/
	rsync -aH $SCHOOL_BACKUP_FULL_DIR/root/.ssh/ /root/.ssh/ >> $LOGFILE  2>&1
	rsync -aH $SCHOOL_BACKUP_FULL_DIR/etc/ssh/ /etc/ssh/ >> $LOGFILE  2>&1
fi

# SSL
if [ -d etc/ssl -a "$SSL" = "yes" ]; then
	echo "restoring SSL settings" >> $LOGFILE
	tar cfz /etc/ssl.org.tgz /etc/ssl/
	rsync -aH $SCHOOL_BACKUP_FULL_DIR/etc/ssl/ /etc/ssl/ >> $LOGFILE  2>&1
fi

# time4kids
if [ "$SCHOOL_USE_TFK" = "yes" -a -e $SCHOOL_BACKUP_FULL_DIR/tfk.gz -a "$TFK" = "yes" ]; then
  echo "restoring time4kids" >> $LOGFILE
  gunzip $SCHOOL_BACKUP_FULL_DIR/tfk.gz
  mysql -f tfk < $SCHOOL_BACKUP_FULL_DIR/tfk  >> $LOGFILE  2>&1
fi

