#!/bin/sh

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

#
#NOTE:IF your SAP user (in this script sapdb) 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"

RETURN_STATUS=0

CMD_LINE=""

#
# If SAP_SERVER exists then export it to make it available to backint 
#
if [ -n "$SAP_SERVER" ]
then
        CMD_LINE="$CMD_LINE SAP_SERVER=$SAP_SERVER; export SAP_SERVER;"
#if SAPDB/MAXDB user( sapdb in this script) uses C Shell, comment the above line and uncomment next line
#        CMD_LINE="$CMD_LINE setenv SAP_SERVER $SAP_SERVER;"
fi

#
# If SAP_POLICY exists then export it to make it available to backint 
#
if [ -n "$SAP_POLICY" ]
then
        CMD_LINE="$CMD_LINE SAP_POLICY=$SAP_POLICY; export SAP_POLICY;"
#if SAPDB/MAXDB user( sapdb in this script) uses C Shell, comment the above line and uncomment next line
#        CMD_LINE="$CMD_LINE setenv SAP_POLICY $SAP_POLICY;"
fi

#
#
#Example of backup command:
#User name and password (dbm for both), database name (SMALL) and medium name (BACKSMALL)
#need to be replaced with correct information.
#
# Type of backup (Complete Data or Incremental) is dictated by the atributes of backup medium.
# For more information, please see SAPDB (MAXDB) documentation.

if [ $SAP_FULL -eq 1 ]; then
	CMD_LINE="$CMD_LINE dbmcli -u dbm,dbm -d SMALL -uUTL dbm,dbm backup_start BACKData"
elif [ $SAP_CINC -eq 1 ]; then
	CMD_LINE="$CMD_LINE dbmcli -u dbm,dbm -d SMALL -uUTL dbm,dbm backup_start BACKPage"
fi

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

RETURN_STATUS=$?

exit $RETURN_STATUS
