#! /bin/sh
# $Header: duplicate_images.sh,v 1.2.24.1 2004/01/15 15:39:09 $
#***************************************************************************
#* $VRTScprght: Copyright 1993 - 2004 VERITAS Software Corporation, All Rights Reserved $
#***************************************************************************
#
# duplicate_images
#
# This script can be used by an adminstrator as a way to duplicate backups
# that were done in the last X hours.  Example in this script is 8 hours.
# It may be used in a crontab entry to have the duplication done and
# results mailed to you once per day.  It also may be called by the
# /usr/openv/netbackup/bin/session_notify script.
#
# caveat:
#   -not tried on all platforms, some may require modifications for
#    mail processing.

# mail_cmd - find a mail binary and send a message
#
# mail_cmd subject user

mail_cmd () {
	mail_subject="$1"
	mail_user="$2"
	mail_command=''

	# Searching in this order appears to find a command that supports
	# the "-s" argument on all of our UNIX server platforms.

	for mail_base in mailx Mail mail
	do
		for mail_dir in /usr/bin /usr/ucb /usr/sbin /bin /sbin
		do
			if [ -x "$mail_dir/$mail_base" ]
			then
				mail_command="$mail_dir/$mail_base"
				break
			fi
		done
		if [ "$mail_command" != '' ]
		then
			break
		fi
	done
	if [ "$mail_command" = '' ]
	then
		echo "No mail command on this platform" >&2
		return 1
	fi
	"$mail_command" -s "$mail_subject" "$mail_user"
}
		

if [ -f /usr/openv/netbackup/version ] ; then
	HARDWARE=`head -1 /usr/openv/netbackup/version | cut -f2 -d" "`
else
	echo "/usr/openv/netbackup/version not found"
	exit 1
fi

/usr/openv/netbackup/bin/admincmd/bpauthorize -test_admin >/dev/null
status=$?
if [ ${status} -ne 0 ] ; then
	echo ""
	echo "You do not have the proper authorization to perform"
	echo "this task.  Please resolve and try again."
	exit ${status}
fi

HOURS=8
OUTFILE=/tmp/duplicate_progress_log
DUPE=/usr/openv/netbackup/bin/admincmd/bpduplicate

/bin/rm -rf $OUTFILE

echo "

NB images for the last $HOURS hours

" >> $OUTFILE

# this command "previews" the images which would be
# duplicated by the following bpduplicate command
#
$DUPE -hoursago $HOURS -PM >> $OUTFILE
echo "
" >> $OUTFILE

# this command (when modified to be correct for your configuration)
# initiates duplication of images which were created within $HOURS ago.
# you must specify the destination storage unit (-dstunit) which is to
# receive the images, and (optionally) a media pool from which to obtain
# media (-dp); if unspecified, the destination pool is NB_duplicates.
#
# $DUPE -dp poolnamehere -dstunit stunitnamehere -hoursago $HOURS -L $OUTFILE

cat $OUTFILE | mail_cmd "NetBackup duplicate status" root

/bin/rm -rf $OUTFILE
