#!/bin/sh

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

# Purpose: This is a sample script that uses the "db2_all" command to perform
#          a backup in a MPP environment.  It needs to be modified before it 
#          will work in your environment.  The following are same lines that 
#          need to be modified: 
#              - USER="db2inst1"
#              - DB2_CMD="db2 BACKUP DATABASE sample ......... 
#              - DB2_ALL='db2_all "||\`<<+0< '
#          In order for this sample script to work two seperate policyes needs 
#          to be define.  The first policy should contain the following: 
#             -  Clients: one client name that defines the catalog node 
#             -  Schedules: define one "Automatic Backup" schedule
#                           delete the "Backup Policy" schedule
#             -  Files: the path name to a modified version of this sample  
#                       scripted.
#          The second policy should contain the following: 
#             -  Clients: all the client name in the node list  
#             -  Schedules: define one "Backup Policy" schedule
#             -  Files: none 
#
# Logic Flow:  1. Backup the catalog node
#                 a. su to db2 user account
#                 b. set node to 0 through export command 
#                 c. call "db2 backup database" command  
#              2. Wait until catalog node backup completes successfully  
#              3. Start backup of all other nodes in parallel.  
#                 a. su to db2 user account 
#                 b. call db2_all to backup all nodes except catalog node (0). 
#                 c. tell db2_all to exit after all backup commans are started. 
#
# Environment Var:  The following is a list of environment variable that are 
#                   set by bphdb (scheduler) or xbp.  These variable can be 
#                   used to coniditionalize this script.  The variable are 
#                   local to this script and are not pass on to "db2 backup" 
#                   or "db2 restore".  
#
#                      DB2_POLICY - policy name 
#                      DB2_SCHED - schedule name
#                      DB2_SERVER - server name
#                      DB2_USER_INITIATED = boolean, true if initiated by xbp
#                      DB2_SCHEDULED - boolean, true if initiated by scheduler


# ----------------------------------------------------------------------------
# NOTE:  Set the USER variable to a valid db2 user name.  Since this script 
#        is executed for root, we need to set the USER variable to a valid 
#        user name that can perform DB2 backup/restore commands.  This 
#        variable is used on the "su" command. 
# ----------------------------------------------------------------------------
USER="db2inst1"

NB_DIR="/usr/openv/netbackup"
LOG_DIR=$NB_DIR"/logs/bphdb"

# ----------------------------------------------------------------------------
# NOTE: Define DB2_CMD to the call "db2 backup" correctly.  The correct 
#       database name and other needed parameter should be define in the 
#       DB2_CMD variable.
#
# Change nbdb2.lib below to the correct NetBackup library name for your host:
#	Solaris-32 = nbdb2.so
#	Solaris-64 = nbdb2.so64
#	AIX-32 = nbdb2.sl
#	AIX-64 = nbdb2.sl64
# ----------------------------------------------------------------------------
DB2_CMD="db2 BACKUP DATABASE sample ONLINE LOAD "$NB_DIR"/bin/nbdb2.lib"

# ----------------------------------------------------------------------------
# NOTE: Define DB2_ALL to call the "db2_all" command with the correct options. 
#       It is currently defined to run the remote backup commands on all the 
#       node except node 0 as parallel daemon i.e. in the background with stdin 
#       stdout and stderr all close.  This option use the fews amount of   
#       resource, but make debugging more difficult if error occur in the  
#       initialization process.  To help with debugging stdout on the 
#       "db2 backup" command is redirected to the bphdb log directory. 
#       Removing ( \` ) in the DB2_ALL variable will cause all processes 
#       to run in parallel and will report errors back to the 
#      "Automatic Backup" schedule, but will use more processes.
# ----------------------------------------------------------------------------
DB2_ALL='db2_all "||\`<<-0< '

EXP_NODE="export DB2NODE=0;"
EXP_DIR="export RAHBUFDIR="$LOG_DIR";"
EXP_NAM="export RAHBUFNAME=db2_rahout;"
TIME=`date +"%m%d%y"`
LOG_OUT=$LOG_DIR'/db2_stdout.'$TIME
CRE_LOG=';chmod 666 '$LOG_OUT
RETURN_STATUS=0

#
# Start the catalog node backup. 
# 

CMD_LINE=$EXP_NODE$DB2_CMD
echo "Execute $CMD_LINE"
su - $USER -c "$CMD_LINE"
RETURN_STATUS=$?

# 
# If the catalog node backup is succussfully start all other backups in 
# in parallel by using db2_all
# 

if [ $RETURN_STATUS -eq 0 ]
then

  DB2_CMD=$DB2_CMD' >> '$LOG_OUT
  CMD_LINE=$EXP_DIR$EXP_NAM$DB2_ALL$DB2_CMD$CRE_LOG'"'
  echo "Execute $CMD_LINE"
  su - $USER -c "$CMD_LINE"

  RETURN_STATUS=$?

fi

exit $RETURN_STATUS
