#!/bin/sh
#***************************************************************************
# $Copyright: Copyright (c) 2022 Veritas Technologies LLC. All rights reserved $
#***************************************************************************
#
# ---------------------------------------------------------------------------
#                          data_archiver_export.sh
# ---------------------------------------------------------------------------
#  This script uses bporaexp to export Oracle table data in XML format using
#  a NetBackup policy.  It is assumed that this script will be executed
#  by user root. In order for the bporaexp utility to work properly we 
#  switch user (su -) to a user account that has the correct environment
#  for running a utility that accesses Oracle.
#  The archive will be owned by this operating system user account
#  which must then be used to perform an import.
#  If this script runs under this user account instead of as root
#  you should remove the switch user logic.
#
# ---------------------------------------------------------------------------
#
# ---------------------------------------------------------------------------
# Put output in <this file name>.out. Change as desired.
# Note: output directory requires write permission.
# ---------------------------------------------------------------------------

OUTF=${0}.out

# ---------------------------------------------------------------------------
# You may want to delete the output file so that backup information does
# not accumulate.  If not, delete the following lines.
# ---------------------------------------------------------------------------

if [ -f "$OUTF" ]
then
	rm -f "$OUTF"
fi

{ # output block

echo "`date` ----------------Beginning of Script------------"
echo "Script name: $0"

# ---------------------------------------------------------------------------
# Replace /db/oracle/product/10.2, below, with the Oracle home path.
# ---------------------------------------------------------------------------

ORACLE_HOME=/db/oracle/product/10.2
export ORACLE_HOME

# ---------------------------------------------------------------------------
# Replace ORA102, below, with the Oracle SID of the target database.
# ---------------------------------------------------------------------------

ORACLE_SID=ORA102
export ORACLE_SID

# ---------------------------------------------------------------------------
# Replace ora102, below, with the Oracle DBA user id (account).
# ---------------------------------------------------------------------------

ORACLE_USER=ora102

# ---------------------------------------------------------------------------
# Replace ${ORACLE_HOME}/scripts with the NetBackup Oracle script path.
# Since subsequent installs or deinstalls will remove this file, you will
# want to move this script from its installed location before making updates.
# ---------------------------------------------------------------------------

NB_ORA_SCRIPTS=${ORACLE_HOME}/scripts

# ---------------------------------------------------------------------------
# Replace ${NB_ORA_SCRIPTS}/bporaexp_tables.param with the location of the
# XML export parameter file.
# ---------------------------------------------------------------------------

NB_ORA_EXP_PARAMS=${NB_ORA_SCRIPTS}/bporaexp_tables.param

# ---------------------------------------------------------------------------
# Specify the location of the XML export utility.
# ---------------------------------------------------------------------------

BPORAEXP=/usr/openv/netbackup/bin/bporaexp

# ---------------------------------------------------------------------------
# Print out the value of the variables set by this script.
# ---------------------------------------------------------------------------

echo  
echo   "ORACLE_SID: $ORACLE_SID"
echo   "ORACLE_USER: $ORACLE_USER"
echo   "ORACLE_HOME: $ORACLE_HOME"
echo   "NB_ORA_SCRIPTS: $NB_ORA_SCRIPTS"
echo   "NB_ORA_EXP_PARAMS: $NB_ORA_EXP_PARAMS"
echo   "BPORAEXP: $BPORAEXP"

echo 

# ---------------------------------------------------------------------------
# Print out the value of the variables set by bphdb.
# Settings in bporaexp_tables.param will override
# NB_ORA_SERV and NB_ORA_POLICY values.
# ---------------------------------------------------------------------------

echo   "NB_ORA_SERV: $NB_ORA_SERV"
echo   "NB_ORA_POLICY: $NB_ORA_POLICY"
echo

# ---------------------------------------------------------------------------
# Call bporaexp
# ---------------------------------------------------------------------------

echo 
echo "% ${BPORAEXP} parfile=${NB_ORA_EXP_PARAMS}"
echo 

su - $ORACLE_USER -c "
ORACLE_SID=$ORACLE_SID
export ORACLE_SID
ORACLE_HOME=$ORACLE_HOME
export ORACLE_HOME
NB_ORA_SERV=$NB_ORA_SERV
export NB_ORA_SERV
NB_ORA_POLICY=$NB_ORA_POLICY
export NB_ORA_POLICY
${BPORAEXP} parfile=${NB_ORA_EXP_PARAMS}
"
RETURN_STATUS=$?

echo 
echo "`date` ----------------End of Script------------------"
echo 
echo "exit $RETURN_STATUS"
exit $RETURN_STATUS

} >> $OUTF
