#!/bin/ksh
# Datensicherung Redologs fuer Dino1 
#
# Autor: F. Baumfalk, NETplus GmbH 
#
# Aenderungen:
# 29022000 sm 	Logs werden nach Datum gesichert. Logs aelter als 14 Tage
#		werden geloescht.
#
############################################################################
####
function switch_logfile
{
  echo connect internal > /tmp/temp.sql
  echo alter system switch logfile >> /tmp/temp.sql
  su - oracle -c "ORACLE_SID=DINO; export ORACLE_SID; svrmgrl
command=@/tmp/temp.sql"
  return ?$
   
}

############################################################################
####
function save_redo
{
 datum=`date '+%y%m%d%H%M%S'`

 if [ $1 = "REDO1" ]
 then
	NB_CLASS=oracle_redo1
        NB_LIST=$LIST_FILE1
	NB_LOG=/var/backup/save_redo1_dino_${datum}.log
 else
	NB_CLASS=oracle_redo2
        NB_LIST=$LIST_FILE2
	NB_LOG=/var/backup/save_redo2_dino_${datum}.log
 fi

 if [ -f $NB_LIST ]
 then

	 echo "Starting backup Type : $NB_CLASS"
	 cat $NB_LIST
 
	 /usr/openv/netbackup/bin/bpbackup -w -c $NB_CLASS -s oracle_redo -L
$NB_LOG -f $NB_LIST
	 RET=$?
	 if [ $RET -ne 0 ]
	 then
	 echo "Save Redo failed" | mailx -s "BACKUP: Dino : Save Redo
failed" SAP.Mail@ewe.de
	 else
		if [ $1 = "REDO2" ]
		then
        		if [ $? -eq 0 ]
			then
			   ERG=`cat $LIST_FILE2`
			   for i in $ERG
           		   do
				echo Checking $i
              			/usr/openv/netbackup/bin/bplist $i >
/dev/null
				if [ $? -eq 0 ]
				then
              				echo Deleting $i
					rm $i
				fi  
           		   done
	   		   rm $LIST_FILE2
			fi  
                fi
		#  keine OK-Mails mehr
	        # echo "Save Redo success" | mailx -s "BACKUP: Dino : Save
Redo success" SAP.Mail@ewe.de
	 fi
  else
	echo Input file missing
	RET=1
  fi
  return $RET
}
############################################################################
######################
# Environment setzen 
# pfad zu archive logs
ARCH_PATH=/archive/DINO

# Archivelog Format
ARCH_FORMAT=DINO*

# Zaehler
COUNTER=0

# Listfiles
LIST_FILE1=/opt/oracle/DINO/sbin/redo1.lst
LIST_FILE2=/opt/oracle/DINO/sbin/redo2.lst

# hilfsvariable
ERG="" 

# alte sicherung fehlgeschlagen ?
if [ -f $LIST_FILE2 ]
then
	echo "Last second save not succesfull trying it one more time"
	save_redo REDO2
else
	# altes listfile noch da ?, dann wiederholen
	if [ -f $LIST_FILE1 ]
	then
		echo "Last save not succesfull trying it one more time"
	else
		# switch logfile erzwingen
                switch_logfile
		# alle logs bis auf das juengste ermitteln


		ERG=`ls $ARCH_PATH/$ARCH_FORMAT| sort -r`
		COUNTER=0
		for i in $ERG
		do
    			if [ $COUNTER -ne 0 ]
			then
				echo $i >> $LIST_FILE1
			fi   
			let COUNTER=$COUNTER+1
		done               
		if [ $COUNTER -eq 0 ] || [ $COUNTER -eq 1 ]
		then
			echo Only 1 Redolog found, ignoring it
			exit 0
		else 
			let COUNTER=$COUNTER-1
			echo Found $COUNTER logs for processing
		fi
	fi
	save_redo REDO1
	if [ $? -eq 0 ]
	then
		mv $LIST_FILE1 $LIST_FILE2
		echo Saving second time
		save_redo REDO2
		if [ $? -ne 0 ]
		then
		   echo error during second save
                fi
	else
		echo error during save
        fi
fi

# Alte Log-Dateien loeschen
find /var/backup -name 'save_redo*log' -mtime +14 -print -exec rm{} \;


