#!/bin/sh

#bcpyrght
#***************************************************************************
# $Copyright: Copyright (c) 2022 Veritas Technologies LLC. All rights reserved $
#***************************************************************************
#ecpyrght

#
#NOTE:IF your SAP user (in this script orasap) runs in C shell, environmental 
#variables can not be exported. In that case, you should modify this script to 
#work in your environment. For example:
#   SAP_SERVER=$SAP_SERVER; export SAP_SERVER; (Correct for Bourne and Korn shells)
#   can change into
#   setenv SAP_SERVER $SAP_SERVER; (Correct for C shell)
#

#
#These environment variables are created by NetBackup (bphdb)
#

echo "SAP_SCHEDULED = $SAP_SCHEDULED"
echo "SAP_USER_INITIATED = $SAP_USER_INITIATED"
echo "SAP_SERVER = $SAP_SERVER"
echo "SAP_POLICY = $SAP_POLICY"
echo "SAP_SCHED = $SAP_SCHED"
echo "SAP_SNC_SCHED = $SAP_SNC_SCHED"

RETURN_STATUS=0

# SAP_ENV - Holds environmental variables.
SAP_ENV=""

#
# If SAP_SERVER exists then export it to make it available to backint. 
#
if [ -n "$SAP_SERVER" ]
then
	SAP_ENV="$SAP_ENV SAP_SERVER=$SAP_SERVER; export SAP_SERVER;"
#if Oracle DBA acount(orasap user) uses C Shell,comment the above line and uncomment the next line
#	SAP_ENV="$SAP_ENV setenv SAP_SERVER $SAP_SERVER;"
fi

#
# If SAP_POLICY exists then export it to make it available to backint.
#
if [ -n "$SAP_POLICY" ]
then
	SAP_ENV="$SAP_ENV SAP_POLICY=$SAP_POLICY; export SAP_POLICY;"
#if Oracle DBA account(orasap user) uses C Shell, comment the above line and uncomment the next line
#	SAP_ENV="$SAP_ENV setenv SAP_POLICY $SAP_POLICY;"
fi

#
# If SAP_SCHED exists then export it to make it available to backint.
#
if [ -n "$SAP_SCHED" ]
then
        SAP_ENV="$SAP_ENV SAP_SCHED=$SAP_SCHED; export SAP_SCHED;"
#if Oracle DBA account(orasap user) uses C Shell, comment the above line and uncomment the next line
#       SAP_ENV="$SAP_ENV setenv SAP_SCHED $SAP_SCHED;"
fi

#
# If SAP_SNC_SCHED exists then export it to make it available to backint.
#
if [ -n "$SAP_SNC_SCHED" ]
then
        SAP_ENV="$SAP_ENV SAP_SNC_SCHED=$SAP_SNC_SCHED; export SAP_SNC_SCHED;"
#if Oracle DBA account(orasap user) uses C Shell, comment the above line and uncomment the next line
#       SAP_ENV="$SAP_ENV setenv SAP_SNC_SCHED $SAP_SNC_SCHED;"
fi

#
# Full online backup with dynamic BEGIN/END BACKUP switch 
#

CMD_LINE="$SAP_ENV brbackup -c -d util_file_online -t online -m all"

#  
# The username on the "su" command needs to be replaced with the correct user
# name.
# 
echo "Execute $CMD_LINE"
su - orasap -c "$CMD_LINE"

RETURN_STATUS=$?

#
# save and delete archive logs
#


if [ $RETURN_STATUS -eq 0 ]
then

	timeStamp=`date '+%m%d%y%H%M%S'` ;
	br_out_file="/tmp/brarchive_out.${timeStamp}" ;
	CMD_LINE="$SAP_ENV brarchive -c -d util_file -sd > $br_out_file"

#
# The username on the "su" command needs to be replaced with the correct user 
# name.
# 
	echo "Execute $CMD_LINE"
	su - orasap -c "$CMD_LINE"

	RETURN_STATUS=$?


# If there were no redo logs to backup, brarchive will return 1. But if 1 is
# returned to bphdb, job will appear as failed in the activity monitor. To
# prevent NetBackup failure, check return code from brarchive. If the return code
# is 1, check for message code BR0013W in the output of brarchive. If this message
# is present, then there were no redo logs present to backup and its not a failure.
# So in that case return 0 to bphdb.

        if [ $RETURN_STATUS = 1 ]; then
                su - orasap -c "grep 'BR0*13W' $br_out_file > /dev/null"
                if [ $? = 0 ]; then
                        RETURN_STATUS=0
                fi
        fi
	su - orasap -c "cat $br_out_file;rm $br_out_file"
fi
exit $RETURN_STATUS
