#!/bin/sh
# sybase_mydb_backup $Revision$
#bcpyrght
#***************************************************************************
# $Copyright: Copyright (c) 2022 Veritas Technologies LLC. All rights reserved $
#***************************************************************************
#ecpyrght

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

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

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

#***************************************************************************
# Replace syb_files below with your actual name of a NetBackup policy of 
# type Standard to be used to backup the directory with Sybase script files
#***************************************************************************
SYB_FILES_POLICY=syb_files

#***************************************************************************
# Replace /usr/sybase12/scripts below with your actual path of the Sybase files 
# associated with the instance, but that are not the database or logs files 
#***************************************************************************
SYB_FILES_DIR=/usr/sybase12/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`"
echo ""

SYBASE=${SYBASE}; export SYBASE

#***************************************************************************
# Display the environment.
#
# If initiated by an Automatic schedule on the master server: 
# * The master and media servers provide these variables to this host.
# * The bphdb process sets them into the environment for use by this script.
# * The values are from the perspective of the master server.
#
# If this script is initiated by some other process on this host:
# * These variables will typically be unset.
# * But could be present, if set in the environment of the initiating process.
#***************************************************************************
echo "These environment variables are set:" 
echo ""
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}"
echo ""

RETURN_STATUS=0

#***************************************************************************
# Replace "transaction_dump" below if initiated by an Automatic schedule on 
# the master server.
# * Specify the name of the Automatic schedule used for a transaction dump.
# * The backup is a Sybase database dump if initiated by an automatic 
#   schedule with any other name.
#***************************************************************************
if [ "${SYBACKUP_SCHEDULED}" = "1" ]; then
	if [ "${SYBACKUP_SCHED}" = "transaction_dump" ] ; then
		DUMP_TYPE=TRANSACTION
	else
		DUMP_TYPE=DATABASE
	fi
fi

#***************************************************************************
# Remove the '#' below if not initiated by an Automatic schedule on the 
# master server and a transaction dump is desired.  Otherwise execution 
# from the local host will perform a database dump.
#***************************************************************************
if [ "${SYBACKUP_SCHEDULED}" != "1" ]; then
	DUMP_TYPE=DATABASE
#	DUMP_TYPE=TRANSACTION
fi

#***************************************************************************
# A temporary filename into which the Sybase dump instructions will be placed.
# * Must be writable by this script.
# * Defaults to the same directory as this script.
#***************************************************************************
SYB_INPUT_FILE="${0}.isql.${DATABASE_NAME}.${DUMP_TYPE}"

#***************************************************************************
# Constructing the Sybase dump command.
#
# * Must specify the dump type for the Sybase instance backup.
# * Must specify the database name for the Sybase instance.
# * Must specify "sybackup::" to use the NetBackup for Sybase agent.
# * May optionally include "sybackup::<values>" if needed.
# 
# The default behavior of the NetBackup for Sybase agent (libsybackup) is:
# 
# * Use the first SERVER from the client bp.conf file.
# * Use the CLIENT_NAME from the client bp.conf file.
# * Let the master server select a Sybase policy for the client.
# * Let the master server select an Application schedule in the policy.
# * The master server will select the first ones that meet the criteria.
#
# Note: The Sybase backup process, that loads the agent library, may be either
#       a child of the isql process (and this script) or a child of a Sybase
#       service to which isql connects (depending on the Sybase version).
#       If these variables are set in the environment of that process, the
#       NetBackup for Sybase agent will use them instead of the defaults
#       noted above: $SYBACKUP_SERVER, $SYBACKUP_POLICY, 
#                    $SYBACKUP_CLIENT, $SYBACKUP_SCHED.
#
# This script can optionally override the configuration above by setting 
# specific values in the "sybackup::" string.  These values are passed through 
# the Sybase instance, to the NetBackup for Sybase agent.  This is necessary
# only when the configured values are not sufficient for the operational
# needs of the site.
#
# -SERV   Must be a hostname that is network routable to the master server.
#         Only needed if backing up to a second master server.
# -CLIENT Must match a client in a Sybase Policy.
#         Must be network routable from NetBackup servers to this Sybase host.
#         Only needed if using a virtual name instead of CLIENT_NAME.
# -POL    Must be of type Sybase, and active, and include the client.
#         Only needed if the master has 2+ Sybase policies for this client.
# -SCHED  Must be of type Application Backup, and must have an open window.
#         Only needed if the policy has 2+ Application schedules.
#         Unless deleted, every Sybase policy has an Application schedule
#         named Default-Application-Backup.
# -PMT    Optional, overides the system default image file permission
#         which are typically 660 (rw-rw----). Value must be one of:
#         USER, GROUP, ANY for 600, 660, 664 respectively. 
#         If space is limited per the following Note about 127 characters: 
#         Then 1, 2, & 3 can be used for USER, GROUP, & ANY respectively.
#         If space won't allow for these 7 extra characters, " -PMT 3",
#         the following two are equivalent & interchangeable:
#         "-POL policyname -PMT ANY" & "-PUP policyname" .
#         PUP being "POL with Universal backup file Permissions"
#
# Note: Sybase limits the "sybackup::<values>" string to 127 characters.
#
# Note: Passing -SERV, -CLIENT, -POL, or -SCHED values overrides the
#       environment variables, configuration keywords, and defaults.
#
# If this script is initiated by an Automatic schedule on the master server:
#
# * bphdb provides these environment variables for use by the script:
#   ${SYBACKUP_SERVER}    = Hostname by which the master server knows itself.
#                           Do not use for -SERV if not routable from client.
#   ${SYBACKUP_CLIENT}    = Client name in the Sybase policy.
#   ${SYBACKUP_POLICY}    = Sybase policy name.
#   ${SYBACKUP_SCHED}     = Automatic schedule name from the Sybase policy.
#   ${SYBACKUP_SCHEDULED} = 1
# * The first three could be used for -SERV, -CLIENT, and -POL respectively.
# * Do not use the $SYBACKUP_SCHED value (an Automatic schedule name)
#   for -SCHED, because the -SCHED value must be an Application schedule name.
#   Script logic can use the value of $SYBACKUP_SCHED to pick an Application
#   schedule name.
# * Use the default values when possible.
#
# If this script is initiated by some process on this host:
#
# * Specify any non-default values as needed.
# * Use the default values when possible.
#***************************************************************************

#***************************************************************************
# The first dump command below uses the NetBackup for Sybase agent with the 
# default configuration and/or environment variables.
#
# Remove the '#' from the second dump command below, 
# to allow this script to pass specific values to NetBackup for Sybase.  Be 
# sure to replace or delete -SERV/-CLIENT/-POL/-SCHED per the discussion above.
#***************************************************************************

  echo dump ${DUMP_TYPE} ${DATABASE_NAME} to \"sybackup::\" > ${SYB_INPUT_FILE}

# echo dump ${DUMP_TYPE} ${DATABASE_NAME} to \"sybackup::-SERV ${SYBACKUP_SERVER} -CLIENT ${SYBACKUP_CLIENT} -POL ${SYBACKUP_POLICY} -SCHED Default-Application-Backup\" > ${SYB_INPUT_FILE}

#**************************************************************************
# Remove the '#' from the beginning of the line below if you are going to
# use multiple stripes for the backup. 
# 
# Delete or replace -SERV/-CLIENT/-POL/-SCHED to match the dump command above.
#
# Repeat this line for each stripe.
# **************************************************************************

# echo stripe on \"sybackup::-SERV ${SYBACKUP_SERVER} -CLIENT ${SYBACKUP_CLIENT} -POL ${SYBACKUP_POLICY} -SCHED Default-Application-Backup\" >> ${SYB_INPUT_FILE}

# **************************************************************************
# Last input to isql.  It causes the dump and stripe commands to execute.
# **************************************************************************

echo go >> ${SYB_INPUT_FILE}

#***************************************************************************
# Log the isql command and input. 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} < ${SYB_INPUT_FILE}"
echo ""
echo "isql input file contains:"
cat ${SYB_INPUT_FILE}
echo ""

#***************************************************************************
# Replace "sa" with your Sybase server Administrator's login name.
# Replace "manager" with your Sybase server Administrator's password.
#***************************************************************************

${SYBASE}${OCS_QUAL}/bin/isql -Usa -Pmanager -I${SYBASE}/interfaces -S${SYBSERVER} < ${SYB_INPUT_FILE}
RETURN_STATUS=$?

#***************************************************************************
# Delete the temporary isql input file.
# The contents have already been displayed and consumed above.
#***************************************************************************

/bin/rm -f ${SYB_INPUT_FILE} 

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

	echo ""
	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 ""
echo "Finished `date`"

echo "exit ${RETURN_STATUS}"
echo ""

exit ${RETURN_STATUS}
