#!/bin/ksh
######################################################################
#                                                                    #
#  Author:  Raymond M. Erdey                                         #
#  Purpose: Makes a backup of SNMP collected data and then purges    #
#           the data in Netview's data collection files.             #
#  Date:    2003/12/23                                               #
#                                                                    #
#  Notes:                                                            #
#           The snmpCollect daemon is stopped during the backup      #
#           and purge process to prevent any issues with it.         #
#                                                                    #
#           Redirect Standard out and Error to a log file of your    #
#           choosing if you are running this script as a cron job.   #
#                                                                    #
#           May need to modify the following variables at the top    #
#           of this script:                                          #
#                        strSNMPCollectPath                          #
#                        strBackupPath                               #
#                                                                    #
######################################################################

typeset -rs strSNMPCollectPath="/usr/OV/databases/snmpCollect"
typeset -rs strBackupPath="/backup/snmpCollect"

typeset -rs strLogFile="${strBackupPath}/BackupLog.txt"

typeset -rs LOGIN="/usr/erac/bin/login.sh"

if [ -x /usr/bin/awk ]
then
	typeset -rs AWK="/usr/bin/awk"
else
	typeset -rs AWK="awk"
fi

if [ -x /usr/bin/compress ]
then
	typeset -rs COMPRESS="/usr/bin/compress"
else
	typeset -rs COMPRESS="/usr/bin/compress"
fi

if [ -x /usr/bin/cp ]
then
	typeset -rs CP="/usr/bin/cp"
else
	typeset -rs CP="cp"
fi

if [ -x /usr/bin/date ]
then
	typeset -rs DATE="/usr/bin/date"
else
	typeset -rs DATE="date"
fi

if [ -x /usr/bin/grep ]
then
	typeset -rs GREP="/usr/bin/grep"
else
	typeset -rs GREP="grep"
fi

if [ -x /usr/bin/ls ]
then
	typeset -rs LS="/usr/bin/ls"
else
	typeset -rs LS="ls"
fi

if [ -x /usr/bin/mkdir ]
then
	typeset -rs MKDIR="/usr/bin/mkdir"
else
	typeset -rs MKDIR="mkdir"
fi

if [ -x /usr/bin/mv ]
then
	typeset -rs MV="/usr/bin/mv"
else
	typeset -rs MV="mv"
fi

if [ -x /usr/OV/bin/ovstart ]
then
	typeset -rs OVSTART="/usr/OV/bin/ovstart"
else
	typeset -rs OVSTART="ovstart"
fi

if [ -x /usr/OV/bin/ovstop ]
then
	typeset -rs OVSTOP="/usr/OV/bin/ovstop"
else
	typeset -rs OVSTOP="ovstop"
fi

if [ -x /usr/bin/ps ]
then
	typeset -rs PS="/usr/bin/ps"
else
	typeset -rs PS="ps"
fi

######################################################
#  Create the backup directory if it does not exist  #
######################################################
if [ ! -d ${strBackupPath} ]
then
	set -x
	${MKDIR} -p ${strBackupPath}
	set +x
fi

#####################################
#  Send all output to the log file  #
#  which resides in the backup      #
#  directory                        #
#####################################
exec 1>${strLogFile} 2>&1

print -u2 "\nBacking up files in \"${strSNMPCollectPath}\" to \"${strBackupPath}\"..."

print "\n\tStart Time: $( ${DATE} )"

${LOGIN} 2>&1 | ${AWK} '{ printf("\t%s\n", $0) }'

typeset -rs strDate="$( ${DATE} '+%Y_%m_%e' )"

strFileList="$( ${LS} ${strSNMPCollectPath} | ${GREP} -v '!' )"

############################################
#  Check to see if snmpCollect is running  #
############################################
print ""
${PS} -afe | ${GREP} -v -E "grep|$0" | ${GREP} "snmpCollect"
iFindsnmpCollectProcess=$?

###########################################################
# Only stop/start snmpCollect if it is currently running  #
###########################################################
if [ ${iFindsnmpCollectProcess} -eq 0 ]
then
	print -n -u2 "\n\tStopping \"snmpCollect\":\n\t\t"
	set -x
	${OVSTOP} snmpCollect
	set +x
fi

for strFile in ${strFileList}
do
	print -u2 ""
	strBackupFile="${strBackupPath}/${strFile}__${strDate}"
	if [ -f ${strBackupFile}.Z ] || [ -f ${strBackupFile} ]
	then
		print -u2 "\t${strBackupFile} has already been backed up"
	else
		print -u2 -n "\t"
		########################################
		#  Copy snmp data collection file and  #
		#  save status of the copy.            #
		########################################
		set -x
		${CP} ${strSNMPCollectPath}/${strFile} ${strBackupFile}
		iRC=$?
		set +x
		#############################################
		#  Truncate Netview's data collection file  #
		#  only if the copy of the data worked.     #
		#############################################
		if [ ${iRC} -eq 0 ]
		then
			print -u2 -n "\t"
			set -x
			>${strSNMPCollectPath}/${strFile}
			set +x
			print -u2 -n "\t"
			set -x
			${COMPRESS} ${strBackupFile}
			set +x
		fi
	fi
done

###########################################################
# Only stop/start snmpCollect if it is currently running  #
###########################################################
if [ ${iFindsnmpCollectProcess} -eq 0 ]
then
	print -u2 "\n\tStarting \"snmpCollect\":\n\t\t/etc/netnmrc"
	/etc/netnmrc 2>&1 | ${AWK} '{ printf("\t\t\t%s\n", $0) }'
fi

print "\n\tEnd Time: $( ${DATE} )"
print -u2 "\nDone.\n"
