Advertisement

Thursday, November 22, 2018

Oracle Database: RAC - OCR and OLR Backup Scripts (18c / 12c / 11g)

In this blog I present to you OCR and OLR backup scripts for RAC.
OCR and OLR backups must be maintained manually as well and if possible on a shared mount. 

There are 2 scripts below one which backs up OCR and other OLR.
The key thing common in the scripts is to define Oracle Home, Backup location and configure retention. 
If you open the script is is pretty straightforward that way. 
You can take these scripts and use it in your environment.

OCR Backup
#!/bin/bash
#

TIMESTAMP=`date +"%d.%m.%Y_%H:%M:%S"`
BACKUP_LOC=/data/OCR



echo "Activity time "${TIMESTAMP}

if [ -d $BACKUP_LOC ]; then

echo "Backup destination $BACKUP_LOC does exist on this server.........:"

else

mkdir -p $BACKUP_LOC

#chown -R root:root $BACKUP_LOC

#chmod 755 $BACKUP_LOC

BACKUP_LOC=$BACKUP_LOC

fi


get_ocr()
{
#-------------------------------------------------------------------------------------------------
#Get OCR Location
#-------------------------------------------------------------------------------------------------
echo "Running get_ocr script to display ocr location :"
for i in `${ORACLE_HOME}/bin/ocrcheck |grep -i "Device/File Name"|grep -v grep|awk '{print $4}'`
do
echo "Ocr Disk location is ......................................:${i}"
done
}
bck_ocr()
{
#--------------------------------------------------------------
#Take Backup of OCR
#--------------------------------------------------------------
echo "Running bck_ocr script to backup the ocr files :"

for i in `${ORACLE_HOME}/bin/ocrcheck |grep -i "Device/File Name"|grep -v grep|awk 'NR==1{print $4}'`
do
echo "OCR backup file will be .........:$BACKUP_LOC/backup_manual_${TIMESTAMP}.ocr"
${ORACLE_HOME}/bin/ocrconfig -export $BACKUP_LOC/backup_manual_${TIMESTAMP}.ocr

if [ $? -eq 0 ]; then
echo "OCR file successfully backed up.............................:"
else
echo " Error: While backing up the OCR file"
fi
done
}
perf_ocr_back()
{
#--------------------------------------------------------------------------------------------------
#Check CRS Status
#--------------------------------------------------------------------------------------------------
crsstatus=`$ORACLE_HOME/bin/crsctl check crs|grep -i "Cluster Ready Services is online"|tr -s '\n'`
echo $crsstatus
if [ "$crsstatus" = "CRS-4537: Cluster Ready Services is online" ];
then
echo " CRS is up and running"
get_ocr
bck_ocr
else
echo " CRS is not avalible it should be up and running for backup."
fi
}

remove_old_backup()
{
#--------------------------------------------------------------------------------------------------
#Remove 90 days old backup
#--------------------------------------------------------------------------------------------------
if [ -n "$BACKUP_LOC" ] ; then
RESULTS=`find $BACKUP_LOC -type f -ctime +90|wc -l`
if [ $RESULTS = 0 ] ; then
echo " There are no old backup files to delete this time."
else
for i in `find $BACKUP_LOC -type f -ctime +90`
do
echo " Remove old backup file..................................................:${i} "
rm -f $i
done
fi
else
echo "Backup destination is not set on this server .............................: "
fi
}

#----------------------------------------------------------------------------------
# Main Function - Shell Execution Starts here.
#----------------------------------------------------------------------------------
ORACLE_HOME=/opt/oracle/product/180/grid
export ORACLE_HOME
PATH=$PATH:$ORACLE_HOME/bin
export PATH

echo " OCR disk Information and backup ocr disk ....................................:"
perf_ocr_back
echo " Running program to remove old backups of ocr disks older than three months..............:"
remove_old_backup




OLR Backup

#!/bin/bash
#

TIMESTAMP=`date +"%d.%m.%Y_%H:%M:%S"`
BACKUP_LOC=/data/OLR # Where backup happens

#--------------------------------------------------------------------------------------------------
#Locate and check BACKUP_LOC exists or not else it will create the location
#--------------------------------------------------------------------------------------------------

echo "Activity time "${TIMESTAMP}

if [ -d $BACKUP_LOC ]; then

echo "Backup destination $BACKUP_LOC does exist on this server.........:"

else

mkdir -p $BACKUP_LOC

BACKUP_LOC=$BACKUP_LOC

fi


get_OLR()
{
#-------------------------------------------------------------------------------------------------
#Get OLR Location
#-------------------------------------------------------------------------------------------------
echo "Running get_OLR script to display OLR location :"
for i in `${ORACLE_HOME}/bin/ocrcheck -local |grep -i "Device/File Name"|grep -v grep|awk '{print$4}'`
do
echo "OLR Disk location is ......................................:${i}"
done
}
bck_OLR()
{
#--------------------------------------------------------------
#Take Backup of OLR
#--------------------------------------------------------------
echo "Running bck_OLR script to backup the OLR files :"

for i in `${ORACLE_HOME}/bin/ocrcheck -local |grep -i "Device/File Name"|grep -v grep|awk '{print$4}'`
do
echo "OLR backup file will be .........:$BACKUP_LOC/backup_manual_${TIMESTAMP}.olr"
${ORACLE_HOME}/bin/ocrconfig -local -export $BACKUP_LOC/backup_manual_${TIMESTAMP}.olr

if [ $? -eq 0 ]; then
echo "OLR file successfully backed up.............................:"
else
echo " Error: While backing up the OLR file"
fi
done
}
perf_OLR_back()
{
#--------------------------------------------------------------------------------------------------
#Check CRS Status
#--------------------------------------------------------------------------------------------------
crsstatus=`$ORACLE_HOME/bin/crsctl check crs|grep -i "Cluster Ready Services is online"|tr -s '\n'`
echo $crsstatus
if [ "$crsstatus" = "CRS-4537: Cluster Ready Services is online" ];
then
echo " CRS is up and running"
get_OLR
bck_OLR
else
echo " CRS is not avalible it should be up and running for backup."
fi
}

remove_old_backup()
{
#--------------------------------------------------------------------------------------------------
#Remove 90 days old backup
#--------------------------------------------------------------------------------------------------
if [ -n "$BACKUP_LOC" ] ; then
RESULTS=`find $BACKUP_LOC -type f -ctime +90|wc -l`
if [ $RESULTS = 0 ] ; then
echo " There are no old backup files to delete this time."
else
for i in `find $BACKUP_LOC -type f -ctime +90`
do
echo " Remove old backup file..................................................:${i} "
rm -f $i
done
fi
else
echo "Backup destination is not set on this server .............................: "
fi
}

#----------------------------------------------------------------------------------
# Main Function - Shell Execution Starts here
#----------------------------------------------------------------------------------
ORACLE_HOME=/opt/oracle/product/180/grid
export ORACLE_HOME
PATH=$PATH:$ORACLE_HOME/bin
export PATH

echo " OLR disk Information and backup OLR disk ....................................:"
perf_OLR_back
echo " Running program to remove old backups of OLR disks older than three months..............:"
remove_old_backup