#!/bin/sh
#
# $Id: nsrdmo.sh,v 1.16 1998/05/14 15:45:19 john Exp $ Copyright (c) 1996, Legato Systems, Inc.
#
#
# BusinesSuite Module for Oracle version 2.1.2.
# Copyright (c) 1990-1998, Legato Systems, Inc.
#
# nsrdmo
#
# Modification of this script should be done with care and only after reading
# the administration manual included with this product.
#
# This script should only be run as part of a scheduled savegroup.
#
# IMPORTANT NOTE FOR EBU USERS
# The Enterprise Backup Utility executable and process prefixes and suffixes
# are changed from "obk" to "ebu" at EBU 2.2.  This script has been modified
# both the old and new nomenclature and also to support enhancements introduced
# in EBU 2.2.  This script now accepts, in addition to the regular savegrp
# options, the -H option for ORACLE_HOME, and -S option for ORACLE_SID. If you
# have more than one database you are backing up with EBU 2.2 you can now
# create a separate client resource for each database and call a the nsrdmo
# script with the -H and -S parmameters to specify which database to backup.
# Note that the use of options on the backup command requires NetWorker server 
# and client release 5.0 and above.
#
# IMPORTANT NOTE FOR ORACLE8 USERS:
# This script is not presently supported for running a scheduled savegroup
# (server initiated backups) with the Oracle8 Recovery Manager Utility (RMAN).
# Support for running a scheduled savegroup (server initiated backups) with
# RMAN will be available in future releases of the BusinesSuite Module for
# Oracle.   Note that the BusinesSuite Module for Oracle still supports both
# ad-hoc (client initiated backups) and Oracle Enterprise Manager 1.4 for
# both Oracle7 and Oracle8.
#
# Returns 0 on success; 1 on failure.
#

#
# Configuration variables, modifiable by users.
#

#
# Variable: OBK_HOME
# Default value: none (site specific)
# Description: Specifies where the Enterprise Backup Utility is installed,
# typically $ORACLE_HOME/obackup. This variable must be configured in order
# to locate the obackup executable.
#
# If running EBU 2.0.10, 2.0.12.4, or 2.1. setting this variable is required
# If running EBU 2.2 and you intend to use obackup compatibility mode then
# setting this variable is required.
# If running EBU 2.2 and you do not intend to use obackup compatibility mode
# then do setting this variable is not recommended.
#
#
OBK_HOME=

#
# Variable: EBU_HOME
# Default value: none (site specific)
# Description: Specifies where the Enterprise Backup Utility is installed,
# typically $ORACLE_HOME/obackup. This variable must be configured in order
# to locate the ebu executable.
#
# If running EBU 2.2 setting this variable is required
# If running EBU 2.0.10, 2.0.12.4, or 2.1. this variable is not supported
#
EBU_HOME=

#
# Variable: ORACLE_HOME
# Default value: none (site specific)
# Description: Specifies where the Oracle Server installation is located.
# This variable can be set here to provide a default value if it is not
# specified with the -H option to nsrdmo on the backup command.  Setting
# the -H option will override this setting.
#
# If running EBU 2.2 setting this variable is either set here or with the
# -H option to this script on the backup command is required
#
# If running EBU 2.0.10, 2.0.12.4, or 2.1. setting this variable is generally
# not required.
#
ORACLE_HOME=

#
# Variable: ORACLE_SID
# Default value: none (site specific)
# Description: Specifies the SID of the database being backed up.
# This variable can be set here to provide a default value if it is not
# specified with the -S option to nsrdmo on the backup command.  Setting
# the -S option will override this setting.
#
# If running EBU 2.2 setting this variable is either set here or with the
# -S option to this script on the backup command is required
#
# If running EBU 2.0.10, 2.0.12.4, or 2.1. this variable is generally not
# recommended.
#
ORACLE_SID=


#
# Variable: NLS_LANG
# Default value: AMERICAN_AMERICA.US7ASCII
# Description: The Oracle7 Enterprise Backup Utility requires that this
# parameter is set if you are running against a database that does not
# use this character set by default.
#
# If setting this variable you may also need to set ORACLE_HOME in this
# script to allow obackup to locate the message catalog.
#
# Setting this variable is not generally required.
#
#NLS_LANG=AMERICAN_AMERICA.US7ASCII


#
# Variable: PRECMD
# Default value: NONE
# Description: A command to run before backup is started. This variable
# can be used to run a command before oracle backups is started. 
#
# The pre command must return a non zero exit code if it fails.  It is
# the resposibility of the script writer to ensure that the script 
# returns the proper exit code.  Please refer chapter 5 of the BusinesSuite
# Module for Oracle Administrator's Guide UNIX Version for templates and
# examples.
#
# Setting this variable is optional.
#
PRECMD=

#
# Variable: POSTCMD
# Default value: NONE
# Description: A command to run after backup is completed. This variable
# can be used to run a command after oracle backups have completed. 
#
# The post command must return a non zero exit code if it fails.  It is
# the resposibility of the script writer to ensure that the script 
# returns the proper exit code.  Please refer chapter 5 of the BusinesSuite
# Module for Oracle Administrator's Guide UNIX Version for templates and
# examples. 
#
# Setting this variable is optional.
#
POSTCMD=

#
# Variable: NSR_DATA_VOLUME_POOL
# Default value: none
# Description: If this variable is set to name a media pool, then database
# backups will be directed to the named media pool.  By default all BMO
# backups will be directed to the 'Default' pool.  You can set this
# variable here to provide an alternate default value if a specific pool
# is not specified by savegrp
#
# This variable is either set here or the value can be passed on the -b
# option to savegrp
#
# Setting this variable is optional.
#
NSR_DATA_VOLUME_POOL=

#
# Variable: NSR_NO_BUSY_ERRORS
# Default value: FALSE
# Description: If this variable is set to a value of TRUE then DMO will
# will fail immediately if it encounters a busy server while initializing
# the session.
# If this variable is not set DMO will fail immediately
#
# Setting this variable is optional.
#
NSR_NO_BUSY_ERRORS=FALSE

#
# Variable: NSR_CHECKSUM
# Default value: FALSE
# Description: This environment variable specifies checksumming
# is performed on the data.
# If this variable is set DMO will use checksumming.
#
# Setting this variable is optional.
#
NSR_CHECKSUM=FALSE

#
# Variable: NSR_ENCRYPTION
# Default value: FALSE
# Description: This environment variable specifies encryption
# is performed on the data.
# If this variable is not set DMO will not use encryption by default.
#
# Setting this variable is optional.
#
NSR_ENCRYPTION=FALSE

#
# Variable: NSR_COMPRESSION
# Default value: FALSE
# Description: This environment variable specifies compression
# is performed on the data.
# If this variable is not set DMO will not use compression by default.
#
# Setting this variable is optional.
#
NSR_COMPRESSION=FALSE


#
#
# Variables: BRD_TOT_TIME, BRD_SAMP_TIME, BRD_ERR_TIME, BRD_RETRIES 
# Description: The noted environment variables allow the user to 
# configure the EBU Instance Manager (brd).  The default settings for these
# variables as specified by Oracle are not compatible with scheduled saves
# from NetWorker where obackup starts a new brd process.  If brd is started
# using the default values for these variables then savegrp will appear to
# appear to hang before backing up the indexes.  The following settings
# will prevent the described behaviour by causing brd to exit seconds after
# the backup is completed. 
#
# resist the urge to modify these variables.
#

#
# Variable: BRD_TOT_TIME
# Default value: 300
# Oracle Default value: 172800
# Description: Once the Instance Manager (brd) becomes idle, it will wait for
# BRD_TOT_TIME seconds for any activity by obackup before it exits.
# Caution: setting this value too low may cause brd to exit before obackup
# can establish a connection to the Backup Catalog. If this happens obackup
# will fail with the following error:
# OBK-2464: Internal error.  Instance Manager process was not started
#
BRD_TOT_TIME=300

#
# Variable: BRD_SAMP_TIME
# Default value: 1
# Oracle Default value: 300
# Description: The Instance Manager (brd) checks the Backup Catalog for any
# active jobs in the interval of BRD_SAMP_TIME seconds.
#
BRD_SAMP_TIME=1

#
# Variable: BRD_ERR_TIME
# Default value: 1
# Oracle Default value: 900
# Description: If the Instance Manager (brd) detects an error it waits
# BRD_ERR_TIME before retrying the catalog.
#
BRD_ERR_TIME=1

#
# Variable: BRD_RETRIES
# Default value: 1
# Oracle Default value: 3
# Description: The Instance Manager (brd) looks for active jobs BRD_RETRIES
# number of times before going to sleep.
#
BRD_RETRIES=1


# Variable: PATH
# Default value: all platforms
# Description: Set up PATH environment variable.  This must be configured
# to include path to the NetWorker program "mminfo" and /usr/bin.
# This variable must be configured to locate the correct executables
#
# HP-UX 10.x
#PATH=/opt/networker/bin:/usr/bin
# Solaris
#PATH=/bin:/usr/sbin:/usr/bin
# AIX
#PATH=/usr/bin
# Digital UNIX
#PATH=/usr/opt/networker/bin
# All Platforms
PATH=/bin:/usr/sbin:/usr/bin:/opt/networker/bin:/usr/networker/bin:/usr/opt/networker/bin

# For HP-UX only
# Variable: SHLIB_PATH
# Default value: /usr/lib
# obackup requires this variable to be set in order to locate the libobk.sl
# shared library.
#
# This variable should be configured on HP-UX systems.
#
SHLIB_PATH=/usr/lib

#
# Variable: NSR_DEBUG_FILE
# Default value: /tmp/bmo.dbg
# Description: To enable debugging output for DMO set the following to 
# an appropriate path and file name.  
#
# Setting this variable is for debugging purposes only 
#
NSR_DEBUG_FILE=/tmp/bmo.dbg


#
# export_environment_variables()
# Make sure that approriate environment variables are exported.
# 
#
export_environment_variables()
{

# The following variables are requried by EBU 2.2
export EBU_HOME
export ORACLE_HOME
export ORACLE_SID

# The following variable is required by versions other than EBU 2.2
# export NLS_LANG only if it is required.
export OBK_HOME
#export NLS_LANG

# Optional BMO variables  
export NSR_COMPRESSION
export NSR_ENCRYPTION
export NSR_CHECKSUM

# The following variables should always be exported even if not set.
export NSR_SERVER
export NSR_SAVESET_EXPIRATION
export NSR_DATA_VOLUME_POOL
export NSR_GROUP

# The following variables control EBU Instance Manager operation
export BRD_TOT_TIME
export BRD_SAMP_TIME
export BRD_ERR_TIME
export BRD_RETRIES

# The following variable is for debugging only.
#export NSR_DEBUG_FILE 

export PATH
export SHLIB_PATH

}


# ---------------------------------------------------------------------
# WARNING
# User modifications below this point are not recommended or supported.
# ---------------------------------------------------------------------

#
# Shell and environment variables not configured by user.
#
getdate="date '+%a %b %d %H:%M:%S %Y'"
echo=echo		# echo command
Verbose=0		# Are we generating verbose output?
Pid=0			# Which process to kill when we are cancelled

#
# usage(): display correct usage for nsrdmo
#
usage()
{
	${echo} \
"This command should only be executed by savegroup."
	${echo} \
"usage: $0 [<options>]
    nsrdmo Options:
	[ -H oracle_home ] [-S oracle_sid ]
    savgrp Options:
	[ -BEiLnqvx ] [ -s server ] [ -N name ] [ -e expiration ]
	[  -f dirfile ] [ -b pool ] [ -F file ] [ -g group ]
	[ -l level ] [ -t date ] [ -m masquerade ] [ -W width ] [ path ... ]

For an explanation of all options, see the save(8) man page.
"
	exit 1
}


#
# Run the pre-processing command.  This can be used to shut down
# a database.
#
run_precmd()
{
	if [ "${PRECMD}" = "" ]; then
		return 0
	fi

	vecho
	vecho "Executing pre-processing command '${PRECMD}' at `date`"
	vecho

	${PRECMD}
	return_status=$?

	vecho
	vecho "Pre-processing command '${PRECMD}' finished at `date`"
	vecho

	return $return_status
}

#
# Run the post-processing command.  This can be used to start a database.
#
#
run_postcmd()
{
	if [ "${POSTCMD}" = "" ]; then
		return 0
	fi

	vecho
	vecho "Executing post-processing command '${POSTCMD}' at `date`"
	vecho

	${POSTCMD}
	return_status=$?

	vecho
	vecho "Post-processing command '${POSTCMD}' finished at `date`"
	vecho

	return $return_status
}

#
# Process arguments supplied by savegrp.  Must handle any arguments save(8)
# accepts.
#
# Sample list of arguments
# save -s empire -g Default -n -LL -m empire -l full -W78 -N /usr /usr
#
processargs()
{
while [ $# -gt 0 ]; do
	case "$1" in
	-H )	# Oracle home
		ORACLE_HOME=$2
		shift
		shift
		;;
	-S )	# Oracle SID
		ORACLE_SID=$2
		shift
		shift
		;;
	-s )	# Specify server
		NSR_SERVER=$2
		shift
		shift
		;;
	-B )	# Force connecting directory information
		Bflag=y
		shift
		;;
	-E )	# Estimate save set size
		Eflag=y
		shift
		;;
	-i )	# Ignore any directives
		iflag=y
		shift
		;;
	-LL )	# Force local backup and cause extra line to be printed
		LLflag=y
		shift
		;;
	-n)	# No save
		nflag=y
		shift
		;;
	-q )	# Quiet
		qflag=y
		shift
		;;
	-v )	# Verbose
		Verbose=1
		shift
		;;
	-N )	# Save set name 
		Saveset_name=$2
		shift
		shift
		;;
	-e )	# Specify alternate expiration time
		NSR_SAVESET_EXPIRATION=$2
		shift
		shift
		;;
	-f )	# Specify directives
		Directives=$2
		shift
		shift
		;;
	-b )	# Specify pool
		NSR_DATA_VOLUME_POOL=$2
		shift
		shift
		;;
	-F )	# Specify file for modification time comparison (not used)
		Directives=$2
		shift
		shift
		;;
	-g )	# Specify group
		NSR_GROUP=$2
		shift
		shift
		;;
	-l )	# Specify level
		Level=$2
		shift
		shift
		;;
	-t )	# Specify time for comparison
		Comparison_time=$2
		shift
		shift
		;;
	-m )	# Masquerade as another program
		Masquerade=$2
		shift
		shift
		;;
	-W )	# Specify width of output
		Output_width=$2
		shift
		shift
		;;
	*)	# Name of the obackup script
		obk_commands=$1
		shift
		;;
	esac
done
}

#
# backup_oracle: run obackup to backup the oracle database
# The particulars of the backup are contained in the obackup 
# command script which is passed to obackup
# We pass the -s[ilent] option to obackup to stop output to stderr.
# nsrobkstart must be on the PATH.
#
backup_oracle()
{

	nsrobkstart -s $obk_commands &
 	Pid=$!
 	wait $Pid
	obackup_status=$?

	if [ $obackup_status != 0 ]; then
		echo "obackup returned status of "$obackup_status
	fi
}

#
# Echo the arguments only if in verbose mode.
#
vecho()
{
	if [ $Verbose -gt 0 ]; then
		echo $*
	fi
}

#
# Display output exactly like save does to keep savegroup happy. 
# displays size information for database backups.  
#
display_summary()
{
	Hostname=`uname -n`
       first_part=`echo "$Hostname: Oracle Backup Status       level=full,        "`
       second_part=`mminfo -s $NSR_SERVER -q"savetime>=$Start_time,savetime<=$End_time" -r "name,totalsize(1),nfiles(1)" | \
awk '\
BEGIN { total_kb = 0; total_mb = 0; total_files = 0; files_str = "file" } \
{ \
        if ( $3 == "KB" ) { \
               total_kb += $2 \
       } else { \
               total_mb += $2 \
       } \
       total_files += $4 \
} \
END { \
       if (total_files > 1) {  \
               files_str = "files"  \
       } else {  \
               files_str = "file" \
       } \
        if (total_kb > 1000) { \
               total_mb += (total_kb / 1000) \
       } \
        if (total_mb > 0) {  \
printf "%d.%d MB 00:00:00  %d %s\n", total_mb, total_kb % 1000, total_files, files_str \
       } else {  \
printf "%d KB 00:00:00  %d %s\n", total_kb, total_files, files_str \
       } \
}`

	echo "$first_part$second_part"

	return 0

}

#
# Handle cancel signals sent by savegrp when user stops the group.
# obackup likes to be cancelled with a SIGHUP
#
handle_signal()
{
	if [ $Pid != 0 ]; then
		kill -1 $Pid
	fi
	exit 1
}

#
# The main portion of this shell.
#
# Process arguments, export environment
# Execute a pre-command (if any)
# Execute obackup backup
# Execute a post-command
# Display save output

processargs $*

#
# Make sure we respond to savegrp cancellations.
#
trap handle_signal 2 15

#
# Export all environment variables.
#
export_environment_variables

#
# Handle pre-processing
#
run_precmd
precmd_status=$?
if [ $precmd_status != 0 ]; then
	echo "Pre-processing command failed.  Ending session."
	exit 1
else
	vecho "Pre-processing command succeeded."
fi

#
# Handle database backup
#
Start_time=`eval $getdate`
backup_oracle
End_time=`eval $getdate`

#
# handle post-processing
#
run_postcmd
postcmd_status=$?
if [ $postcmd_status != 0 ]; then
	echo "Post-processing command failed.  Ending session."
	if [ $obackup_status != 0 ]; then
		exit $obackup_status
	else
		exit 1
	fi
else
	vecho "Post-processing command succeeded."
	if [ $obackup_status != 0 ]; then
		exit $obackup_status
	fi
fi

#
# Display summary for savegroup
# Only displays if pre-command, obackup, and post-command were all
# successful.
#
display_summary

exit 0
