summaryrefslogblamecommitdiffstats
path: root/scripts/system-restore
blob: 1c2f6bb35699df04f11a6141aa80805e0a996b15 (plain) (tree)
1
2
3
4
5
6
7
8
9

           

                        




                                               






















                                                                                                 























                                                                                




                                               



                                                                                              











                                                                       



                                                                                           
                            
                          

                                                 

                                                                                               





                                                                                                                                   
    
                                 

                                   
                                                                             



                                                                                                        
                                                         




                                                                                                            



                                                                            

                               
                                        


                                                                                                                                       
                                                                                                                                                                          








                                                                                                                      























                                                                                                                                                                                                                                                        

  


                                                                 






                  
#!/bin/bash

TMDIR="/opt/taskmanager"

BACKUP="$1"
if [ -z "$BACKUP" ] || [ ! -f "$BACKUP" ]; then
	echo "Backup file not found: $BACKUP"
	exit 1
fi
shift

RES_OPENSLX=0
RES_SAT=0
while [ $# -gt 0 ]; do
	case "$1" in
		openslx)
			RES_OPENSLX=1
			echo "Restoring system config"
			;;
		dozmod)
			RES_SAT=1
			echo "Restoring VM and lecture db"
			;;
		*)
			echo "Error: Restore mode params must be one of openslx, dozmod (Got $1)"
			exit 1
			;;
	esac
	shift
done

[ "$RES_OPENSLX$RES_SAT" = "00" ] && exit 1

if [ "$(whoami)" != "root" ]; then
	echo "Must be running as root!"
	exit 1
fi

DIR="/root/restore/$(date +%s)"

if [ -d "$DIR" ]; then
	echo "Restore already running!?"
	exit 1
fi

mkdir -p "$DIR"
if ! cd "$DIR"; then
	echo "Could not cd to $DIR"
	exit 1
fi

if ! tar --ignore-failed-read -x -f "$BACKUP"; then
	echo "Could not extract $BACKUP - make sure it's a valid .tar.gz / .tgz"
	exit 1
fi

if [ -f db.sql ]; then
	DB_OLD=1
elif [ -f openslx.sql ] || [ -f sat.sql ]; then
	DB_OLD=0
else
	echo "Error: database dump not found in backup - are you sure this is a valid backup?"
	exit 1
fi

if [ $RES_SAT -eq 1 -a $DB_OLD -eq 0 -a ! -f sat.sql ]; then
	echo "Error: this backup does not contain the DozMod database"
	echo "Error: cannot restore VM/lecture information"
	exit 1
fi
if [ $RES_OPENSLX -eq 1 -a $DB_OLD -eq 0 -a ! -f openslx.sql ]; then
	echo "Error: this backup does not contain the OpenSLX database"
	echo "Error: cannot restore satellite configuration"
	exit 1
fi

if [ $RES_OPENSLX -eq 1 -a ! -f files.tgz ]; then
	echo "Error: files.tgz not found in backup - are your sure this is a valid backup?"
	exit 1
fi

echo "-- Restoring Database"
if [ $DB_OLD -eq 1 ]; then
	echo "--- Importing legacy database dump"
	# Restoring from dozmod v1.0 db
	mysql --defaults-extra-file=/etc/mysql/debian.cnf --default-character-set=utf8 < db.sql
	RET=$?
	if [ $RES_SAT -eq 1 ]; then
		echo "--- Trying to convert dozmod data (this might not work too well...)"
		mysql --defaults-extra-file=/etc/mysql/debian.cnf --default-character-set=utf8 < "${TMDIR}/data/dozmod-upgrade.sql"
	else
		echo "DROP DATABASE bwLehrpool" | mysql --defaults-extra-file=/etc/mysql/debian.cnf --default-character-set=utf8
	fi
else
	# Restoring from v1.1+ db
	RET=0
	if [ $RES_SAT -eq 1 ]; then
		echo "--- Importing dozmod database (vms/lectures meta data)"
		mysql --defaults-extra-file=/etc/mysql/debian.cnf --default-character-set=utf8 < sat.sql
		RET=$?
	fi
	if [ $RET -eq 0 -a $RES_OPENSLX -eq 1 ]; then
		echo "--- Importing system configuration"
		mysql --defaults-extra-file=/etc/mysql/debian.cnf --default-character-set=utf8 < openslx.sql
		RET=$?
	fi
fi

if [ $RET -ne 0 ]; then
	echo "Error: Restoring database contents failed with exit code $RET"
	exit 1
fi

if [ $RES_OPENSLX -eq 1 ]; then
	echo "-- Restoring system files"
	# Since we came that far we'll delete some old configs (if existent)
	rm -rf /opt/ldadp/{configs,pid,logs}/* /opt/openslx/configs/* /srv/openslx/www/boot/default/config.tgz 2> /dev/null
	# Force triggering IP detection/setting, which should in turn regenerate ldadp configs and launch ldadp instances if applicable
	mysql --defaults-extra-file=/etc/mysql/debian.cnf --default-character-set=utf8 -e "UPDATE openslx.property SET value = 'invalid' WHERE name = 'server-ip' LIMIT 1"

	tar --ignore-failed-read -x -f files.tgz -C /
	RET=$?
	if [ $RET -ne 0 ]; then
		echo "WARNING: Restoring filesystem contents failed with exit code $RET - backup might be incomplete!"
	fi
	# Make sure the directory tree is owned by taskmanager, as tar will create intermediate
	# directories as owned by root if they do not exist.
	chown -R taskmanager /srv/openslx/www/boot /opt/openslx/configs

	# Try to update the db (if required)
	(
		cd /srv/openslx/www/slx-admin
		./install-all
	)

	# config.tgz symlink -> db entry
	if [ -L /srv/openslx/www/boot/default/config.tgz ]; then
		CONFTGZ=$(readlink /srv/openslx/www/boot/default/config.tgz | sed "s/'/\\\'/g")
		echo "Config.tgz links to '$CONFTGZ'"
		mysql --defaults-extra-file=/etc/mysql/debian.cnf --default-character-set=utf8 -e "INSERT IGNORE INTO openslx.configtgz_location (locationid, configid) SELECT 0, configid FROM openslx.configtgz WHERE filepath = '$CONFTGZ' LIMIT 1" \
			|| echo "Could not convert default config.tgz setting - do so manually"
		rm -f -- /srv/openslx/www/boot/default/config.tgz
	fi

	sleep 0.5
	for i in 1 1 1 1 1 2 2 3 4 END; do
		CB=$(sudo -u www-data -n php /srv/openslx/www/slx-admin/api.php cb)
		[ "x$CB" != "xTrue" ] && break
		[ "$i" = "END" ] && break
		sleep $i
	done

fi

# Sync redirect flag file with setting stored in DB
/opt/taskmanager/scripts/install-https --redirect --redirect-only

rm -rf -- "$DIR"
rm -f -- "$BACKUP"

echo "Success."

exit 0