#!/bin/bash
#
#########################################################################################
# Script: 		backup_current_environment.sh
# Description: 	Backup selected environment
# Author: 		ABS Laundry Business Solutions
# Version: 		4.5
#### Changelog of script ################################################################
# Date        Author    Version 	Change    
# 19-Jun-2011 JMI		1.3			Initial version
#########################################################################################

backup_abs_component()
{
    case "$1" in
        application)    ABSAPPTYPE="Server";;
        report)         ABSAPPTYPE="Reportserver";;
        web)            ABSAPPTYPE="Web";;
        license)        ABSAPPTYPE="LICENSE";;
        customreports)  ABSAPPTYPE="CUSTOMREPORTS";;
    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)
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 and glassfish server instance binaries
backup_abs_component application
backup_abs_component report
backup_abs_component web
backup_abs_component license
backup_abs_component customreports

# 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
