#!/bin/sh
# sybase_mydb_backup $Revision$
#bcpyrght
#***************************************************************************
#* $Copyright: Copyright (c) 2017 Veritas Technologies LLC. All rights reserved VT25-0977-2658-84-51-3 $ *
#***************************************************************************
#ecpyrght

#***************************************************************************
# Replace /usr/sybase12 below with the actual Sybase home directory
#***************************************************************************
SYBASE=/sybase/CRP

#***************************************************************************
# Replace SYBASE12 below with the actual name of the SQL Server
#***************************************************************************
SYBSERVER=CRP

#***************************************************************************
# Replace SYB_DB below with the actual name of your Sybase database
#***************************************************************************
DATABASE_NAME=CRP

#***************************************************************************
# Replace syb_files below with your actual name of the NetBackup
# server Policy to be used to backup the directory with Sybase script files
#***************************************************************************
SYB_FILES_POLICY=SAP_Sybase_BKP_DR

#***************************************************************************
# Replace /usr/sybase12/scripts below with your actual path of the Sybase files
#***************************************************************************
SYB_FILES_DIR=/usr/openv/netbackup/ext/db_ext/sybase/scripts

#***************************************************************************
# Determine the SYBASE_ASE and SYBASE_OCS env variables for Sybase 12.x
#***************************************************************************
OCS_QUAL=
if [ -f "${SYBASE}"/SYBASE.csh ] ; then
        SYBASE_ASE=`grep "setenv SYBASE_ASE" "${SYBASE}"/SYBASE.csh | cut -f3 -d" " | sort -u | head -1`
        SYBASE_OCS=`grep "setenv SYBASE_OCS" "${SYBASE}"/SYBASE.csh | cut -f3 -d" " | sort -u | head -1`
        export SYBASE_ASE
        export SYBASE_OCS
        OCS_QUAL=/$SYBASE_OCS
elif [ -f "${SYBASE}"/SYBASE.sh ] ; then
        SYBASE_ASE=`grep "SYBASE_ASE=" "${SYBASE}"/SYBASE.sh | cut -f2 -d"=" | sort -u | head -1`
        SYBASE_OCS=`grep "SYBASE_OCS=" "${SYBASE}"/SYBASE.sh | cut -f2 -d"=" | sort -u | head -1`
        export SYBASE_ASE
        export SYBASE_OCS
        OCS_QUAL=/$SYBASE_OCS
fi

echo "Started `date`"
SYBASE=$SYBASE; export SYBASE
# These environment variables are set by NetBackup
echo "SYBACKUP_SERVER = $SYBACKUP_SERVER"
echo "SYBACKUP_POLICY = $SYBACKUP_POLICY"
echo "SYBACKUP_SCHED = $SYBACKUP_SCHED"
echo "SYBACKUP_CLIENT = $SYBACKUP_CLIENT"
echo "SYBACKUP_SCHEDULED = $SYBACKUP_SCHEDULED"
echo "SYBACKUP_USER_INITIATED = $SYBACKUP_USER_INITIATED"

RETURN_STATUS=0

#***************************************************************************
# Replace "database_dump" below with your actual NetBackup schedule name
# which is used for a full backup of the Sybase database.
#***************************************************************************
if [ "${SYBACKUP_SCHED}" = "Full" ]
then
############# NetBackup has started a "database_dump" backup ##############
DUMP_TYPE=DATABASE

else
############# NetBackup has started a "transaction log" backup ##############
DUMP_TYPE=TRANSACTION
fi

#***************************************************************************
# Replace "Default-Application-Backup" below with your actual NetBackup
# Application Backup type schedule name for the Sybase database.
#***************************************************************************
        echo dump $DUMP_TYPE $DATABASE_NAME to \"sybackup::-SERV $SYBACKUP_SERVER -CLIENT $SYBACKUP_CLIENT -POL $SYBACKUP_POLICY -SCHED Default-Application-Backup\" > ./syb_${DATABASE_NAME}_dump

#**************************************************************************
# Remove the '#' from the beginning of the line below if you are going to
# use multiple stripes for the backup. Repeat this line for each stripe.
# Replace "Default-Application-Backup" below with your actual NetBackup
# Application Backup type schedule name for the Sybase database.
# **************************************************************************
# echo stripe on \"sybackup::-SERV $SYBACKUP_SERVER -CLIENT $SYBACKUP_CLIENT -POL $SYBACKUP_POLICY -SCHED Default-Application-Backup\" >> ./syb_${DATABASE_NAME}_dump

echo go >> ./syb_${DATABASE_NAME}_dump

#***************************************************************************
# Log the isql command. This line may appear in NetBackup log files.
# Do NOT replace "xxxxxxxx" with a user id or password.
#***************************************************************************
        echo "$SYBASE$OCS_QUAL/bin/isql -Uxxxxxxxx -Pxxxxxxxx -I$SYBASE/interfaces -S$SYBSERVER -X < ./syb_${DATABASE_NAME}_dump"

#***************************************************************************
# Replace "sa" with your Sybase server Administrator's login name.
# Replace "manager" with your Sybase server Administrator's password.
#***************************************************************************
        $SYBASE$OCS_QUAL/bin/isql -Usapsa '-PIdbi0crm' -I$SYBASE/interfaces -S$SYBSERVER -X < ./syb_${DATABASE_NAME}_dump
        su - sybcrp -c "$CMD_LINE"
        RETURN_STATUS=$?

if [ "${DUMP_TYPE}" = "DATABASE" ]
then
#       Initiate a backup of any file related to the Sybase database, such as script files.

        echo "bpbackup -c $SYB_FILES_POLICY $SYB_FILES_DIR"
        /usr/openv/netbackup/bin/bpbackup -c $SYB_FILES_POLICY $SYB_FILES_DIR
        BPBACKUP_STATUS=$?

        if [ "$BPBACKUP_STATUS" -ne 0 ]
        then
                echo ""
                echo "bpbackup of $SYB_FILES_DIR returned $BPBACKUP_STATUS"
        fi
fi


echo "Finished `date`"

echo "exit $RETURN_STATUS"
echo ""

exit $RETURN_STATUS
