#! /bin/sh
# $Header: pltemplates.sh,v 1.8.10.1 2004/01/15 15:39:10 $
#
#***************************************************************************
#* $VRTScprght: Copyright 1993 - 2004 VERITAS Software Corporation, All Rights Reserved $
#***************************************************************************
#
# pltemplates
#
# This script can be used by an adminstrator to create example policies
# in the NetBackup database which may be used as templates for the
# creation of new policies and schedules using bpadm or xbpadm.
#
# policy: template_normal
#  This policy is defined to have standard clients and to not use
#   compression on the clients.
#  Four schedules are defined:
#   fullbkps - performs full backups during evening hours with lengthened
#              hours on the weekend.  backups are scheduled weekly, and
#              are retained for one month.
#   incrbkps - performs incremental backups during evening hours with
#              lengthened hours on the weekend.  backups are scheduled daily,
#              and are retained for three weeks.
#   userarch - permits user archiving of files during workdays.
#              NOTE that the retention period for these archives is set
#              to one year rather than infinite.  you may want to change this.
#   userbkps - permits user backups of files during workdays.
#              these backups are retained for one month.
#  Although the user schedules do not overlap the fulls and incrementals,
#  the potential exists that a user directed request could start prior to
#  the window close on Friday and hamper incremental or full backups until
#  the user directed request completes.
#
# policy: template_weekend
#  This policy is defined to have standard clients and to not use
#   compression on the clients. The schedules are disjoint, permitting
#   full backups ONLY on the weekends and incrementals ONLY during the week.
#  Two schedules are defined:
#   wkndfull - performs full backups once a week only during weekend hours.
#              the backup window for this schedule spans the weekend.
#              the frequency of 3 days is needed (rather than 7 days) due
#              to the window spanning days.
#   incrs - performs incremental backups during evening hours only on
#           Mondays through Thursdays.
#
# some caveats:
#   -all command line interfaces are subject to change without notice
#   -the created policies have hard-coded names and ARE DELETED
#    prior to their re-creation
#   -no clients are added to the policies
#   -no error checking is done

trap trapped 2

trapped () {

	echo "Exiting pltemplates after ^C"
	exit 1
}

DEVNULL=/dev/null
TEMP=/tmp/clients.$$

POLICYNEW=/usr/openv/netbackup/bin/admincmd/bppolicynew
INCLUDE=/usr/openv/netbackup/bin/admincmd/bpplinclude
DELETE=/usr/openv/netbackup/bin/admincmd/bppldelete
SCHD=/usr/openv/netbackup/bin/admincmd/bpplsched
INFO=/usr/openv/netbackup/bin/admincmd/bpplinfo
WIN=/usr/openv/netbackup/bin/admincmd/bpplschedwin
LIST=/usr/openv/netbackup/bin/admincmd/bppllist
CLIENTS=/usr/openv/netbackup/bin/admincmd/bpplclients
FILELIST_LOTUSNOTES=/usr/openv/netbackup/bin/goodies/lotusnotes.filelist
FILELIST_EXCHANGE=/usr/openv/netbackup/bin/goodies/ms_exchange.filelist
FILELIST_EXCHANGE_MAILBOX=/usr/openv/netbackup/bin/goodies/ms_exchange_mailbox.filelist
FILELIST_EXCHANGE_2000=/usr/openv/netbackup/bin/goodies/ms_exchange_2000.filelist
FILELIST_EXCHANGE_SRS=/usr/openv/netbackup/bin/goodies/ms_exchange_srs.filelist
FILELIST_EXCHANGE_PUB=/usr/openv/netbackup/bin/goodies/ms_exchange_pub.filelist
FILELIST_EXCHANGE_KMS=/usr/openv/netbackup/bin/goodies/ms_exchange_kms.filelist
FILELIST_WIN2K=/usr/openv/netbackup/bin/goodies/windows2000.filelist
FILELIST_SHAREPOINT_SERVER=/usr/openv/netbackup/bin/goodies/ms_sharepoint_server.filelist
FILELIST_SHAREPOINT_WS=/usr/openv/netbackup/bin/goodies/ms_sharepoint_ws.filelist
FILELIST_ORACLE_RMAN=/usr/openv/netbackup/bin/goodies/oracle_rman.filelist
FILELIST_ORACLE_XML_EXPORT=/usr/openv/netbackup/bin/goodies/oracle_xml_export.filelist
FILELIST_DB2=/usr/openv/netbackup/bin/goodies/db2.filelist

CNAME1=template_normal
CNAME2=template_weekend
# CNAME3 - CNAME8 used to be for split mirror
CNAME3=MS_Exchange_SRS
CNAME4=MS_Exchange_Public_Folders
CNAME5=MS_Exchange_KMS
CNAME6=MS_SharePoint_Server
CNAME7=MS_SharePoint_Workspaces
CNAME9=LotusNotes
CNAME10=MS_Exchange
CNAME11=Windows2000
CNAME12=MS_Exchange_5.x
CNAME13=MS_Exchange_Mailbox
CNAME14=MS_Exchange_2000
CNAME15=Oracle_RMAN
CNAME16=Oracle_XML_Export
CNAME17=DB2
PNAME=DataStore
UNAME=`uname -n`
HOSTNAME=$UNAME

nb_flag=0
ln_flag=0
ex_flag=0
nt_flag=0
ds_flag=0
sp_flag=0
or_flag=0
db2_flag=0

# Process options
while true
do
	if [ "$#" -eq 0 -o $ds_flag = 1 ] ; then
		break;
	fi

	case "$1"
	in
		"netbackup")
			nb_flag=1 ;;
		"lotus_notes")
			ln_flag=1 ;;
		"ms_exchange")
			ex_flag=1 ;;
		"ms_sharepoint")
			sp_flag=1 ;;
		"windows2000")
			nt_flag=1 ;;
		"datastore")
			ds_flag=1 ;;
		"oracle")
			or_flag=1 ;;
		"db2")
			db2_flag=1 ;;
		*)
			echo "unknown option: $1"
			exit 1
			;;
	esac
	#get next option
	shift
done

/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

if [ $ln_flag = 1 ] ; then
	$DELETE $CNAME9 -template 2>>$DEVNULL
	echo "Creating Directive Set for $CNAME9"
	$POLICYNEW $CNAME9 -template 2>>$DEVNULL
	$INFO $CNAME9 -template -set -clienttype Lotus-Notes -inactive 2>>$DEVNULL
	$INCLUDE $CNAME9 -template -add `cat $FILELIST_LOTUSNOTES` 2>>$DEVNULL
fi

if [ $ex_flag = 1 ] ; then
	$DELETE $CNAME10 -template 2>>$DEVNULL

	# Exchange 5.5
	$DELETE $CNAME12 -template 2>>$DEVNULL
	echo "Creating Directive Set for $CNAME12"
	$POLICYNEW $CNAME12 -template 2>>$DEVNULL
	$INFO $CNAME12 -template -set -clienttype MS-Exchange-Server -inactive 2>>$DEVNULL
	$INCLUDE $CNAME12 -template -add "`cat $FILELIST_EXCHANGE`" 2>>$DEVNULL

	# Exchange Mailbox
	$DELETE $CNAME13 -template 2>>$DEVNULL
	echo "Creating Directive Set for $CNAME13"
	$POLICYNEW $CNAME13 -template 2>>$DEVNULL
	$INFO $CNAME13 -template -set -clienttype MS-Exchange-Server -inactive 2>>$DEVNULL
	$INCLUDE $CNAME13 -template -add "`cat $FILELIST_EXCHANGE_MAILBOX`" 2>>$DEVNULL

	# Exchange 2000
	$DELETE $CNAME14 -template 2>>$DEVNULL
	echo "Creating Directive Set for $CNAME14"
	$POLICYNEW $CNAME14 -template 2>>$DEVNULL
	$INFO $CNAME14 -template -set -clienttype MS-Exchange-Server -inactive 2>>$DEVNULL
	$INCLUDE $CNAME14 -template -add "`cat $FILELIST_EXCHANGE_2000`" 2>>$DEVNULL

	# Exchange Site Replication Service
	$DELETE $CNAME3 -template 2>>$DEVNULL
	echo "Creating Directive Set for $CNAME3"
	$POLICYNEW $CNAME3 -template 2>>$DEVNULL
	$INFO $CNAME3 -template -set -clienttype MS-Exchange-Server -inactive 2>>$DEVNULL
	$INCLUDE $CNAME3 -template -add "`cat $FILELIST_EXCHANGE_SRS`" 2>>$DEVNULL

	# Exchange Public Folder
	$DELETE $CNAME4 -template 2>>$DEVNULL
	echo "Creating Directive Set for $CNAME4"
	$POLICYNEW $CNAME4 -template 2>>$DEVNULL
	$INFO $CNAME4 -template -set -clienttype MS-Exchange-Server -inactive 2>>$DEVNULL
	$INCLUDE $CNAME4 -template -add "`cat $FILELIST_EXCHANGE_PUB`" 2>>$DEVNULL

	# Exchange Key Management Service
	$DELETE $CNAME5 -template 2>>$DEVNULL
	echo "Creating Directive Set for $CNAME5"
	$POLICYNEW $CNAME5 -template 2>>$DEVNULL
	$INFO $CNAME5 -template -set -clienttype MS-Exchange-Server -inactive 2>>$DEVNULL
	$INCLUDE $CNAME5 -template -add "`cat $FILELIST_EXCHANGE_KMS`" 2>>$DEVNULL
fi

if [ $sp_flag = 1 ] ; then

	# SharePoint Portal Server
	$DELETE $CNAME6 -template 2>>$DEVNULL
	echo "Creating Directive Set for $CNAME6"
	$POLICYNEW $CNAME6 -template 2>>$DEVNULL
	$INFO $CNAME6 -template -set -clienttype MS-SharePoint -inactive 2>>$DEVNULL
	$INCLUDE $CNAME6 -template -add "`cat $FILELIST_SHAREPOINT_SERVER`" 2>>$DEVNULL

	# SharePoint Portal Workspaces
	$DELETE $CNAME7 -template 2>>$DEVNULL
	echo "Creating Directive Set for $CNAME7"
	$POLICYNEW $CNAME7 -template 2>>$DEVNULL
	$INFO $CNAME7 -template -set -clienttype MS-SharePoint -inactive 2>>$DEVNULL
	$INCLUDE $CNAME7 -template -add "`cat $FILELIST_SHAREPOINT_WS`" 2>>$DEVNULL
fi

if [ $or_flag = 1 ] ; then
	$DELETE $CNAME15 -template 2>>$DEVNULL
	echo "Creating Template Set for $CNAME15"
	$POLICYNEW $CNAME15 -template 2>>$DEVNULL
	$INFO $CNAME15 -template -set -clienttype Oracle -inactive 2>>$DEVNULL
	$INCLUDE $CNAME15 -template -add "`cat $FILELIST_ORACLE_RMAN`" 2>>$DEVNULL
fi

if [ $or_flag = 1 ] ; then
	$DELETE $CNAME16 -template 2>>$DEVNULL
	echo "Creating Template Set for $CNAME16"
	$POLICYNEW $CNAME16 -template 2>>$DEVNULL
	$INFO $CNAME16 -template -set -clienttype Oracle -inactive 2>>$DEVNULL
	$INCLUDE $CNAME16 -template -add "`cat $FILELIST_ORACLE_XML_EXPORT`" 2>>$DEVNULL
fi

if [ $db2_flag = 1 ] ; then
	$DELETE $CNAME17 -template 2>>$DEVNULL
	echo "Creating Template Set for $CNAME17"
	$POLICYNEW $CNAME17 -template 2>>$DEVNULL
	$INFO $CNAME17 -template -set -clienttype DB2 -inactive 2>>$DEVNULL
	$INCLUDE $CNAME17 -template -add "`cat $FILELIST_DB2`" 2>>$DEVNULL
fi

if [ $nt_flag = 1 ] ; then
	$DELETE $CNAME11 -template 2>>$DEVNULL
	echo "Creating Directive Set for $CNAME11"
	$POLICYNEW $CNAME11 -template 2>>$DEVNULL
	$INFO $CNAME11 -template -set -clienttype MS-Windows-NT -inactive 2>>$DEVNULL
	$INCLUDE $CNAME11 -template -add "`cat $FILELIST_WIN2K`" 2>>$DEVNULL
fi

if [ $nb_flag = 1 ] ; then
	$DELETE $CNAME1 2>>$DEVNULL
	$DELETE $CNAME2 2>>$DEVNULL

	echo " ... creating policy $CNAME1"
	$POLICYNEW $CNAME1 2>>$DEVNULL
	$INFO $CNAME1 -set 2>>$DEVNULL
	$INCLUDE $CNAME1 -add / /home /usr 2>>$DEVNULL
	echo " ... creating schedules for policy $CNAME1"
	$SCHD $CNAME1 -add fullbkps -type FULL -retlevel 3 -freq 604800 -window 72000 36000 2>>$DEVNULL
	$WIN $CNAME1 fullbkps -0 57600 50400 -5 57600 50400 -6 57600 50400 2>>$DEVNULL
	$SCHD $CNAME1 -add incrbkps -type INCR -retlevel 2 -freq 86400 -window 72000 36000 2>>$DEVNULL
	$WIN $CNAME1 incrbkps -0 57600 50400 -5 57600 50400 -6 57600 50400 2>>$DEVNULL
	$SCHD $CNAME1 -add userbkps -type UBAK -retlevel 3 -window 28800 28800 2>>$DEVNULL
	$WIN $CNAME1 userbkps -close 0 -close 6 2>>$DEVNULL
	$SCHD $CNAME1 -add userarch -type UARC -retlevel 8 -window 28800 28800 2>>$DEVNULL
	$WIN $CNAME1 userarch -close 0 -close 6 2>>$DEVNULL

	echo " ... creating policy $CNAME2"
	$POLICYNEW $CNAME2 2>>$DEVNULL
	$INFO $CNAME2 -set 2>>$DEVNULL
	$INCLUDE $CNAME2 -add / /home /usr 2>>$DEVNULL
	echo " ... creating schedules for policy $CNAME2"
	$SCHD $CNAME2 -add wkndfull -type FULL -retlevel 3 -freq 259200 2>>$DEVNULL
	$WIN $CNAME2 wkndfull -5 57600 216000 2>>$DEVNULL
	$SCHD $CNAME2 -add incrs -type INCR -retlevel 2 -freq 86400 -window 72000 36000 2>>$DEVNULL
	$WIN $CNAME2 incrs -0 57600 50400 -5 57600 50400 -6 57600 50400 2>>$DEVNULL
	$WIN $CNAME2 incrs -close 0 -close 5 -close 6 2>>$DEVNULL

	echo "Done creating example policies and schedules."
fi

if [ $ds_flag = 1 ]; then 
  if [ $# -eq 1 ]; then
    CNAME=$1
  fi
  if [ $# -eq 2 ]; then
    CNAME=$1
    # Get the client name. 
    HOSTNAME=$2
  fi
  if [ $# -eq 0 ]; then
    CNAME=DSPolicy
  fi

  # Check if the policy exists. If it does, exit
  $LIST >> $TEMP
  if [ -s $TEMP ] ; then
    while read TEMPNAME
    do
      if [ $TEMPNAME = $CNAME ] ; then
        echo $TEMPNAME >> /tmp/classinfo.$$
        break;
      fi
    done < $TEMP 
    rm $TEMP
  fi
  if [ -s /tmp/classinfo.$$ ] ; then
    echo " " Policy $CNAME exists
    rm /tmp/classinfo.$$
    exit
  fi

  #Get NBU server
  if [ -f /usr/openv/netbackup/bp.conf ] ; then
      read temp1 temp2 SERVER < /usr/openv/netbackup/bp.conf
  fi

  echo " " ...creating policy $CNAME
  $POLICYNEW $CNAME 2>>$DEVNULL

  $INFO $CNAME -modify -M $SERVER -pool $PNAME -active -follownfs 0 -classjobs 1 -ct DataStore 2>>$DEVNULL

  echo " " ...creating schedule for policy $CNAME
  $SCHD $CNAME -add dssched -type UBAK -retlevel 9 -window 0 86400 2>>$DEVNULL

  # Add the client for the new policy
  $CLIENTS >> $TEMP
  if [ -s $TEMP ] ; then
    while read HARDWARE OS HNAME
    do
      if [ $HNAME = $HOSTNAME ] ; then
        echo $HARDWARE $OS > /tmp/clientinfo.$$
        break 2
      fi
    done < $TEMP
    rm $TEMP
  fi
  if [ -s /tmp/clientinfo.$$ ] ; then
    read HARDWARE OS < /tmp/clientinfo.$$
    rm /tmp/clientinfo.$$
  fi

  $CLIENTS $CNAME -add $HOSTNAME $HARDWARE $OS
fi
