#!/bin/bash
#####################################################################
# Backup (b) and Restore (r) script for the database and ini files. 
# Usage: ./dtb-backup <b|r>                                         
#                                                                                   
# Changelog:                                                              
# rpm62 - added wsi.xml to backup / restore process						
# rpm66 - checking the DTBServer.ini for vnr_liste parameter			
# rpm67 - removing the backup and restore of all .req, .cfg, .pfx files and leaving only DTB_WSI.req
#		- reason: the results of DISPLAY_NAME and PERSDAT are mixed if the config files are overwritten
# rpm68 - adding french language
# rpm69 - adding SyncTrig.cfg
#		- adding chmod 777 for syncstatus
# rpm71 - using DTBconfigFile.xml
# rpm77 - moving from /var/tmp/dtb into /var/pgsql/dtb_config/
# rpm80 - excluding inputstyle.txt
#		- deleting the config.bak file from winedir to prevent restoring the config.dtb from it. Reason: the config.dtb can be modified in dtb_backup after DTB is stopped
# rpm2.0-3 - OSFOURK-23022 - excluding conversion.txt from backup
# rpm2.0-4 - OSFOURK-23074 - PRB000062027 System restart caused by high DTB CPU load on CCA
#####################################################################

# Import definitions
CMD=$(basename $0)
if [ -f /opt/dtb/bin/_common_definitions ]; then
    . /opt/dtb/bin/_common_definitions
else
	logger -t $CMD -p ERROR -s "The /opt/dtb/bin/_common_definitions file does not exist, exiting the script..."
	exit 1
fi

# Evaluate parameters: if the number of parameters is less than 1 or is not b or r then print out the help.
if [ $# -lt 1 ] || [ !"$1" == "b" ] || [ !"$1" == "r" ]; then
    logger -t $CMD -p ERROR -s "Usage: $0 <b|r> where b = Backup; r = Restore"
    exit
fi

BACKUP_DIR=/var/pgsql/dtb_backup
DTB_WRAPPER=/opt/dtb/bin/dtb-wrapper

# Create the backup directory if it doesn't exist.
if ([ ! -d "$BACKUP_DIR" ] || [ ! -d "$BACKUP_DIR/DTBServer" ] || [ ! -d "$BACKUP_DIR/DTBMaster" ] || [ ! -d "$BACKUP_DIR/config" ]); then
		mkdir -p $BACKUP_DIR
		mkdir -p $BACKUP_DIR/DTBServer
		mkdir -p $BACKUP_DIR/DTBMaster
		mkdir -p $BACKUP_DIR/config
		chmod -R 777 $BACKUP_DIR
		chown -R dtbuser:users $BACKUP_DIR
		plog "Backup folder created."
fi

# Check the parameters mode and log it!
syncmode=$(/opt/webservice/scripts/gethaipsetupentry.sh system.dtb-sync)
if [ $syncmode == 0 ]; then plog "Synchronization mode: OFF"
	elif [ $syncmode == 1 ]; then plog "Synchronization mode: OpenScape 4000 Assistant (PERSDAT)"
	elif [ $syncmode == 2 ]; then plog "Synchronization mode: OpenScape 4000 Manager (PERSDAT)"
	elif [ $syncmode == 3 ]; then plog "Synchronization mode: SFTP"
	elif [ $syncmode == 4 ]; then plog "Synchronization mode: OpenScape 4000 Assistant (DISPLAY_NAME)"
	elif [ $syncmode == 5 ]; then plog "Synchronization mode: OpenScape 4000 Manager (DISPLAY_NAME)"
	elif [ $syncmode == 6 ]; then plog "Synchronization mode: LDAP"
fi
lang=$(/opt/webservice/scripts/gethaipsetupentry.sh system.dtb-lang)
if [ $lang == 0 ]; then plog "Language: English"
elif [ $lang == 1 ]; then plog "Language: German"
else plog "Language: French"
fi
synctime=$(/opt/webservice/scripts/gethaipsetupentry.sh system.dtb-syncTime)
plog "Synchronization time: $synctime"

# If DTB is running then stop it, to update his files. The HA will restart the DTB if needed.
checkproc "$DTB_MASTER_EXE"
if [ $? == 0 ]; then
	plog "$DTB_MASTER_EXE is running."
	$DTB_WRAPPER DTBMaster stop
	sleep 1
fi

checkproc "$DTB_SERVER_EXE"
if [ $? == 0 ]; then
	plog "$DTB_SERVER_EXE is running."
	$DTB_WRAPPER DTBServer stop
	sleep 1
fi
		
if [ $1 = "b" ]; then
	plog "Backup into /var/pgsql/dtb_backup"
	
	#List all dtb processes (there should be none)
	plog "These DTB processes are still running (should be empty):"
	for pid in $(ps aux | grep dtb | awk '{print $2}'); do 
		plog $(ps -p $pid -o comm=)
	done
    
	# Backup DTBServer: database files and ini
		sudo -u dtbuser cp $DTB_DRBD_SHARE/DTBServer/winedir/*.nam $BACKUP_DIR/DTBServer; [ $? -eq 0 ] && plog "all .nam backed up." || plog ".nam backup failed."
		sudo -u dtbuser cp $DTB_DRBD_SHARE/DTBServer/winedir/*.nax $BACKUP_DIR/DTBServer; [ $? -eq 0 ] && plog "all .nax backed up." || plog ".nax backup failed."
		sudo -u dtbuser cp $DTB_DRBD_SHARE/DTBServer/winedir/*.rnr $BACKUP_DIR/DTBServer; [ $? -eq 0 ] && plog "all .rnr backed up." || plog ".rnr backup failed."
		sudo -u dtbuser cp $DTB_DRBD_SHARE/DTBServer/winedir/*.rnx $BACKUP_DIR/DTBServer; [ $? -eq 0 ] && plog "all .rnx backed up." || plog ".rnx backup failed."
		sudo -u dtbuser cp $DTB_DRBD_SHARE/DTBServer/winedir/*.t9i $BACKUP_DIR/DTBServer; [ $? -eq 0 ] && plog "all .t9i backed up." || plog ".t9i backup failed."
		sudo -u dtbuser cp $DTB_DRBD_SHARE/DTBServer/winedir/*.dtb $BACKUP_DIR/DTBServer; [ $? -eq 0 ] && plog "all .dtb backed up." || plog ".dtb backup failed."
		sudo -u dtbuser cp $DTB_DRBD_SHARE/DTBServer/config/*.ini $BACKUP_DIR/DTBServer;  [ $? -eq 0 ] && plog "all .ini backed up." || plog ".ini backup failed."
	# Backup language files
		sudo -u dtbuser cp $DTB_DRBD_SHARE/DTBServer/winedir/*.txt $BACKUP_DIR/DTBServer;  [ $? -eq 0 ] && plog "all .txt backed up." || plog ".txt backup failed."
		sudo -u dtbuser cp $DTB_DRBD_SHARE/DTBServer/winedir/*.TXT $BACKUP_DIR/DTBServer;  [ $? -eq 0 ] && plog "all .TXT backed up." || plog ".TXT backup failed."
	# Remove the DTBServerLog.txt and inputstyle.txt
		sudo -u dtbuser rm -f $BACKUP_DIR/DTBServer/DTBServerLog.txt;
		sudo -u dtbuser rm -f $BACKUP_DIR/DTBServer/inputstyle.txt;
		sudo -u dtbuser rm -f $BACKUP_DIR/DTBMaster/conversion.txt;
	# Remove the config.bak from winedir
		sudo -u dtbuser rm -f $DTB_DRBD_SHARE/DTBServer/winedir/config.bak;
		oldlang=`cat /var/opt/dtb_lang`
	# Backup the language setting
		sudo -u dtbuser echo $oldlang > $BACKUP_DIR/dtb_lang;  							   [ $? -eq 0 ] && plog "Language backed up." || plog "Language backup failed."
	# Backup DTBMaster: ini, DTB_WSI.req, SyncTrig.cfg
		sudo -u dtbuser cp $DTB_DRBD_SHARE/DTBMaster/config/*.ini $BACKUP_DIR/DTBMaster; [ $? -eq 0 ] && plog "dtbmaster.ini backed up." || plog "dtbmaster.ini backup failed."
		sudo -u dtbuser cp $DTB_DRBD_SHARE/DTBMaster/winedir/DTB_WSI.req $BACKUP_DIR/DTBMaster; [ $? -eq 0 ] && plog "DTB_WSI.req backed up." || plog "DTB_WSI.req backup failed."
		sudo -u dtbuser cp $DTB_DRBD_SHARE/DTBMaster/winedir/SyncTrig.cfg $BACKUP_DIR/DTBMaster; [ $? -eq 0 ] && plog "SyncTrig.cfg backed up." || plog "SyncTrig.cfg backup failed."
	# Backup the config files
		cp $CONFIG_FILE $BACKUP_DIR/config;    [ $? -eq 0 ] && plog "config file backed up." || plog "config file backup failed."
		cp $DTB_SYNCSTATUS $BACKUP_DIR/config; [ $? -eq 0 ] && plog "syncstatus file backed up." || plog "syncstatus file backup failed."
		cp $DTB_CFGDIR/wsi.xml $BACKUP_DIR/config; [ $? -eq 0 ] && plog "WSI config file backed up." || plog "WSI config file backup failed."
fi

if [ $1 = "r" ]; then
    # Change the file's Owner to dtbuser
	chmod -R 777 $BACKUP_DIR
	chown -R dtbuser:users $BACKUP_DIR
	plog "Restore from /var/pgsql/dtb_backup"
		
	# Check if NodeDir is configured to OFF. If it is, then delete the possible nax and rnx files (otherwise the search will not work)
	inicontent="$(cat $BACKUP_DIR/DTBMaster/dtbmaster.ini)"; # whole content of the dtbmaster.ini
	if [[ "$inicontent" == *"NodeDirs=off"* ]]; then
		plog "dtbmaster.ini: NodeDirs=off. Removing possible nax and rnx files.";
		sudo -u dtbuser rm -f $BACKUP_DIR/DTBServer/*.nax;
		sudo -u dtbuser rm -f $BACKUP_DIR/DTBServer/*.rnx;
		sudo -u dtbuser rm -f $DTB_DRBD_SHARE/DTBServer/winedir/*.nax;
		sudo -u dtbuser rm -f $DTB_DRBD_SHARE/DTBServer/winedir/*.rnx;
	fi
	# Restore DTBServer: database files and ini
	sudo -u dtbuser cp $BACKUP_DIR/DTBServer/*.nam $DTB_DRBD_SHARE/DTBServer/winedir; [ $? -eq 0 ] && plog "all .nam restored." || plog ".nam restore failed."
	sudo -u dtbuser cp $BACKUP_DIR/DTBServer/*.nax $DTB_DRBD_SHARE/DTBServer/winedir; [ $? -eq 0 ] && plog "all .nax restored." || plog ".nax restore failed."
	sudo -u dtbuser cp $BACKUP_DIR/DTBServer/*.rnr $DTB_DRBD_SHARE/DTBServer/winedir; [ $? -eq 0 ] && plog "all .rnr restored." || plog ".rnr restore failed."
	sudo -u dtbuser cp $BACKUP_DIR/DTBServer/*.rnx $DTB_DRBD_SHARE/DTBServer/winedir; [ $? -eq 0 ] && plog "all .rnx restored." || plog ".rnx restore failed."
	sudo -u dtbuser cp $BACKUP_DIR/DTBServer/*.t9i $DTB_DRBD_SHARE/DTBServer/winedir; [ $? -eq 0 ] && plog "all .t9i restored." || plog ".t9i restore failed."
	sudo -u dtbuser cp $BACKUP_DIR/DTBServer/*.dtb $DTB_DRBD_SHARE/DTBServer/winedir; [ $? -eq 0 ] && plog "all .dtb restored." || plog ".dtb restore failed."
	sudo -u dtbuser cp $BACKUP_DIR/DTBServer/*.ini $DTB_DRBD_SHARE/DTBServer/winedir; [ $? -eq 0 ] && plog "all .ini restored." || plog ".ini restore failed."
	# starting from rpm61, the VNR feature introduces the vnr_liste parameter in DTBServer.ini. This should be checked before restoring the old ini file.
	if grep -q "vnr_liste" "$BACKUP_DIR/DTBServer/DTBServer.ini"; then
		# if the vnr_liste is there, then it can be restored from backup dir
		sudo -u dtbuser cp $BACKUP_DIR/DTBServer/*.ini $DTB_DRBD_SHARE/DTBServer/config;  [ $? -eq 0 ] && plog "DTBServer.ini restored." || plog "DTBServer.ini restore failed."
	else
		# if it is not present, then it should be added after the filter_liste, and restored
		sed -i -e "/filter_liste=filtlist.rnr/a vnr_liste=vnrlist.rnr" < $BACKUP_DIR/DTBServer/DTBServer.ini $BACKUP_DIR/DTBServer/DTBServer.ini
		sudo -u dtbuser cp $BACKUP_DIR/DTBServer/*.ini $DTB_DRBD_SHARE/DTBServer/config;  [ $? -eq 0 ] && plog "DTBServer.ini restored." || plog "DTBServer.ini restore failed."
		# it does not matter if it is present or not in the winedir, because the backup file contains the user's data.
	fi
	# Restore language files - only if the install language doesn't changed
	oldlang=`cat $BACKUP_DIR/dtb_lang`
	if [ $lang == 0 ]; then plog "New language is English"
		elif [ $lang == 1 ]; then plog "New language is German"
		else plog "New language is French"
	fi
	if [ $oldlang == 0 ]; then plog "Old language is English"
		elif [ $oldlang == 1 ]; then plog "Old language is German"
		else plog "Old language is French"
	fi
	if [[ "$lang" == "$oldlang" ]]; then
		sudo -u dtbuser cp $BACKUP_DIR/DTBServer/*.txt $DTB_DRBD_SHARE/DTBServer/winedir; [ $? -eq 0 ] && plog "all .txt restored." || plog ".txt restore failed."
		sudo -u dtbuser cp $BACKUP_DIR/DTBServer/*.TXT $DTB_DRBD_SHARE/DTBServer/winedir; [ $? -eq 0 ] && plog "all .TXT restored." || plog ".TXT restore failed."
	else
		plog "Installation lanuage has been changed. No language files will be restored."
	fi
	# Restore DTBMaster: ini, DTB_WSI.req, SyncTrig.cfg
	sudo -u dtbuser cp $BACKUP_DIR/DTBMaster/*.ini $DTB_DRBD_SHARE/DTBMaster/winedir 2>&1 | logger; [ $? -eq 0 ] && plog "dtbmaster.ini restored." || plog "dtbmaster.ini restore failed."
	sudo -u dtbuser cp $BACKUP_DIR/DTBMaster/*.ini $DTB_DRBD_SHARE/DTBMaster/config 2>&1 | logger;  [ $? -eq 0 ] && plog "dtbmaster.ini restored." || plog "dtbmaster.ini restore failed."
	sudo -u dtbuser cp $BACKUP_DIR/DTBMaster/DTB_WSI.req $DTB_DRBD_SHARE/DTBMaster/winedir 2>&1 | logger; [ $? -eq 0 ] && plog "DTB_WSI.req restored." || plog "DTB_WSI.req restore failed."
	sudo -u dtbuser cp $BACKUP_DIR/DTBMaster/SyncTrig.cfg $DTB_DRBD_SHARE/DTBMaster/winedir 2>&1 | logger; [ $? -eq 0 ] && plog "SyncTrig.cfg restored." || plog "SyncTrig.cfg restore failed."
	# Restore the config files - causes a problem when DTB is OFF and the config / WSI parameters are changed. When DTB starts up, the parameters will be overwritten by the restored files.
	# But the files must be copied when a switchover happens, so we can check their existence before the copy
	if [[ ! -f "$DTB_CFGDIR/wsi.xml" && -f "$BACKUP_DIR/config/wsi.xml" ]]; then
		cp $BACKUP_DIR/config/wsi.xml $DTB_CFGDIR/; [ $? -eq 0 ] && plog "XML config file restored." || plog "XML config file restore failed."
		chmod 755 $DTB_CFGDIR/wsi.xml
	fi
	# Same for config
	if [[ ! -f "$CONFIG_FILE" && -f "$BACKUP_DIR/config/DTBconfigFile.xml" ]]; then
		cp $BACKUP_DIR/config/DTBconfigFile.xml $DTB_CFGDIR/; [ $? -eq 0 ] && plog "config file restored." || plog "config file restore failed."
		chmod 755 $CONFIG_FILE
	fi
	# The syncstatus file must be overwritten only if there is no other syncstatus file present. 
	# Otherwise, when the import directory has changed, DTB will be restarted and the syncstatus will be the old one.
	if [[ ! -f "$DTB_SYNCSTATUS" && -f "$BACKUP_DIR/config/syncstatus" ]]; then
		cp $BACKUP_DIR/config/syncstatus $DTB_CFGDIR/; [ $? -eq 0 ] && plog "syncstatus file restored." || plog "syncstatus file restore failed."
		chmod 777 $DTB_SYNCSTATUS
	fi
fi
