#!/bin/bash
#
#########################################################################################
# Script: 		restore_current_environment.sh
# Description: 	Restore selected environment from latest backup 
# Author: 		ABS Laundry Business Solutions
# Version: 		4.5
#### Changelog of script ################################################################
# Date        Author    Version 	Change				                             
# 19-jun-2011 JMI		1.3     	Initial setup
#									Binaries from repository/current will be included in incremental backup
###################################################################################

restore_abs_component()
{
    case "$1" in
        application)    ABSAPPTYPE="Server";;
        report)         ABSAPPTYPE="Reportserver";;
        web)            ABSAPPTYPE="Web";;
    esac
    echo "Restore binaries for $1 server instance"
    export ABSMESSAGE="     Restore binaries for $1 server instance"; absmenu_log
    if [ "$ABS_ROOTSERVER" == "$ABSMENUBACKUPSERVER" ] 
    then
        rsync -tav --delete -e "ssh -q" $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/last_backup/$ABSAPPTYPE $ABSMENUREPOSITORYSERVER:$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future
    else
        ssh -n -q $ABSMENUBACKUPSERVER "rsync -tav -e \"ssh -q\" --delete $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/last_backup/$ABSAPPTYPE $ABSMENUREPOSITORYSERVER:$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future"
    fi
}

ABSMENU_CURRENTDATEANDTIME=$(date +%Y_%b%d_%H%M%S)
ABSMENU_CURRENTBACKUPLOCATION=$ABSMENU_CURRENT_ENV_LICCODE"_"$ABSMENU_CURRENT_ENV_VERSION"_"$ABSMENU_CURRENTDATEANDTIME

export ABSMESSAGE="Restore $ABSMENUBACKUPSERVER:$ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/last_backup to $ABSMENUREPOSITORYSERVER:$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future"
absmenu_log

# check if repository future locations for environment exists. If not then create 
if [ "$ABS_ROOTSERVER" == "$ABSMENUBACKUPSERVER" ] 
then
    [ ! -d "$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future" ] && mkdir -p $ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future
else
    ssh -q $ABSMENUREPOSITORYSERVER "if [ ! -d "$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future" ]; then mkdir -p $ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future; fi;"
fi
                                            
# restore app, rep and glassfish server instance binaries from last backup to repository/Future
restore_abs_component application
restore_abs_component report
restore_abs_component web

