#!/bin/bash
#
#########################################################################################
# Script: 		backup_current_environment.sh
# Description: 	Backup selected environment
# Author: 		ABS Laundry Business Solutions
# Version: 		9.08.00
#### Changelog of script ################################################################
# Date        Author    Version 	Change    
# 19-Jun-2011 JMI		1.3			Initial version
# 20-Mar-2024 RAY       6.0         Support for Glassfish4, Payara5, Tomee8, Tomee91 and Tomcat90
# 03-Apr-2024 RAY       9.08.00		Renamed version
# 25-Apr-2024 RAY       9.08.00     Added Support for Tomcat9 - ABS-164468
# 01-Jul-2024 RAY       9.08.01     Added Support for Tomcat9 and Tomcat10 - ABS-165658
# 21-Aug-2024 RAY       9.08.01     TI changes for replacing TomEE with Tomcat in 9.08.00 - ABS-167946
# 04-Nov-2024 RAY		9.09.00 	Absmenu update supporting Java21 for ABSSolute 9.09.00 - ABS-170835
# 04-Nov-2024 RAY		9.09.00 	Absmenu update supporting Plugins folder - INSTALLER-176
# 04-Nov-2024 RAY		9.09.00 	Absmenu update supporting Application Server new Classpath location - ABS-170842
# 01-Jun-2025 RAY       9.09.02         ABSSolute Server - Standardization & hot redeploy - TS-839
#########################################################################################

backup_abs_component()
{
    case "$1" in
        application)    ABSAPPTYPE="Server";;
        report)         ABSAPPTYPE="Reportserver";;
        web)            ABSAPPTYPE="Web";;
        license)        ABSAPPTYPE="LICENSE";;
        customreports)  ABSAPPTYPE="CUSTOMREPORTS";;
        fonts)  		ABSAPPTYPE="FONTS";;
    esac
    echo "Backup binaries for $1 server instance "
    export ABSMESSAGE="     Backup binaries for $1 server instance"; absmenu_log
    if [ "$ABS_ROOTSERVER" == "$ABSMENUBACKUPSERVER" ] 
    then
        rsync -tav -e "ssh -q" --link-dest=$ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/last_backup $ABSMENUREPOSITORYSERVER:$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Current/$ABSAPPTYPE $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/$ABSMENU_CURRENTBACKUPLOCATION
    else
        ssh -n -q $ABSMENUBACKUPSERVER "rsync -tav -e \"ssh -q\" --link-dest=$ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/last_backup $ABSMENUREPOSITORYSERVER:$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Current/$ABSAPPTYPE $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/$ABSMENU_CURRENTBACKUPLOCATION"
    fi
}

ABSMENU_CURRENTDATEANDTIME=$(date +%Y_%b%d_%H%M%S)

# Added to resolve wrong backup folder name
ABSMENU_CURRENT_ENV_VERSION=$ABSMENU_CURRENT_ENV_SERVERJAR_VERSION

ABSMENU_CURRENTBACKUPLOCATION=$ABSMENU_CURRENT_ENV_LICCODE"_"$ABSMENU_CURRENT_ENV_VERSION"_"$ABSMENU_CURRENTDATEANDTIME

export ABSMESSAGE="Backup $ABSMENUREPOSITORYSERVER:$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Current to $ABSMENUBACKUPSERVER:$ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/$ABSMENU_CURRENTBACKUPLOCATION"
absmenu_log

# check if backup and repository locations for environment exists. If not then create with dummy current_backup
if [ "$ABS_ROOTSERVER" == "$ABSMENUBACKUPSERVER" ] 
then
    if [ ! -d "$ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/dummy_backup" ]
    then
        mkdir -p $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/dummy_backup
        ln -s $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/dummy_backup $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/last_backup
    fi
else
    ssh -q $ABSMENUBACKUPSERVER "if [ ! -d "$ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/dummy_backup" ]; then mkdir -p $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/dummy_backup; ln -s $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/dummy_backup $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/last_backup; fi;"
fi
if [ "$ABS_ROOTSERVER" == "$ABSMENUBACKUPSERVER" ] 
then
    if [ ! -d "$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT" ]
    then
        mkdir -p $ABSMENUREPOSITORYSERVER/$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Current
        mkdir -p $ABSMENUREPOSITORYSERVER/$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future
    fi
else
    ssh -q $ABSMENUREPOSITORYSERVER "if [ ! -d "$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT" ]; then mkdir -p $ABSMENUREPOSITORYSERVER/$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Current; mkdir -p $ABSMENUREPOSITORYSERVER/$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future; fi;"
fi

# backup current app, rep, web, license, customreports and fonts
backup_abs_component application
backup_abs_component report
backup_abs_component web
backup_abs_component license
backup_abs_component customreports
backup_abs_component fonts

# Relinking current backup
export ABSMESSAGE="     Relinking current backup"; absmenu_log
if [ "$ABS_ROOTSERVER" == "$ABSMENUBACKUPSERVER" ] 
then
    rm -f $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/last_backup
    ln -s $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/$ABSMENU_CURRENTBACKUPLOCATION $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/last_backup
else
    ssh -n -q $ABSMENUBACKUPSERVER "rm -f $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/last_backup"
    ssh -n -q $ABSMENUBACKUPSERVER "ln -s $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/$ABSMENU_CURRENTBACKUPLOCATION $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/last_backup"
fi
