Aychin's Oracle RDBMS Blog

Only for Advanced level Professionals

Shell script to make OCR and VoteDisk backups, for Oracle RAC

This shell script will help You to sleep well and be sure that You will always have OCR and Voting Disk files backups. As we know, by default OCR file is automatically backed up, but only on one of the nodes, and there is no Voting Disk file backups at all. What this shell script will do?

  • It will identify if the current node is OCR backup node and distribute the OCR backup to all other nodes in the Cluster, including week.ocr and day.ocr
  • If current node is not backup owner, then OCR export will be tried locally
  • OCR exports will keep last 11 copies, it means, that You will always have 11 last versions. You can keep track of them from ocr.exp.rep file
  • It will also makes the backup of the Voting Disk file and distribute it on all other nodes in the Cluster
  • Puts the result of the “ocrconfig -showbackup” command to the backuplist.txt

The ocrbackup.sh scipt must be copied on each cluster node and scheduled to run recursively on some time interval. In my case, I scheduled it in the cron, to run every 1 hour. The directory in which backups will be placed is identified by the variable $OCRBACKUPLOC in the script body, by default it is set to $ORACLE_BASE/ocrbackup, sure You can change it to any value You want, but be sure that this directory is exists on every node. There also must be established ssh connectivity between nodes to execute remote commands, check it by executing:

-bash$ ssh node3 date
Fri Jul 22 10:00:49 AZST 2011

It must return date and not ask for the password, if it will ask You for the password then refer to the Oracle Clusterware installation guide to step by step instructions. This script is programmed to run under oracle user for security reasons. That is why You will need the right to execute unix command sudo to run “ocrconfig -export“.

For example, lets assume that You have 3 Node Cluster: node1, node2, node3. Copy the script to each node, desirable to the similar directories, or You can put the script to the shared storage, some cluster file system. Let’s assume that this directory is /d01/scripts. Then we need to create directories to store backups, it must be created on each node and must be similar. Let’s assume that it is $ORACLE_BASE/ocrbackup directory created on each node. Now we need to schedule the script to run on each of the node, if You will use cron then execute under oracle user unix command “crontab -e” and put next line to the cron:

0 * * * * . /home/oracle/.bash_profile; . /d01/scripts/ocrbackup.sh >> /d01/scripts/ocrbackup.log

It will schedule the script to run every hour, do it on each of the nodes, it also redirects the output to the ocrbackup.log file. First time You need to execute this script manually from each node, to be sure that everything works fine, some checks in the script will inform You if something wrong. After first manual execution on each of the nodes, You will have the next output, assume that node2 is OCR backup owner:

On node1:

-node1$ sh ocrbackup.sh
This node is not the OCR backup owner, only OCR export was tried!
ocrbackup.sh script done at Fri Jul 22 10:11:21 AZST 2011

On node2:

-node2$ sh ocrbackup.sh
20000+0 records in
20000+0 records out
10240000 bytes (10 MB) copied, 0.210988 seconds, 48.5 MB/s
backup00.ocr                                                                                                             100%   13MB  13.0MB/s   00:01
backup01.ocr                                                                                                             100%   13MB  13.0MB/s   00:00
backup02.ocr                                                                                                             100%   13MB  13.0MB/s   00:01
day_.ocr                                                                                                                 100%   13MB  13.0MB/s   00:00
day.ocr                                                                                                                  100%   13MB  13.0MB/s   00:00
week_.ocr                                                                                                                100%   13MB  13.0MB/s   00:01
week.ocr                                                                                                                 100%   13MB  13.0MB/s   00:00
VoteDisk.bkp                                                                                                             100%   10MB   9.8MB/s   00:00
backuplist.txt                                                                                                           100%  405     0.4KB/s   00:00
backup00.ocr                                                                                                             100%   13MB  13.0MB/s   00:00
backup01.ocr                                                                                                             100%   13MB  13.0MB/s   00:01
backup02.ocr                                                                                                             100%   13MB  13.0MB/s   00:00
day_.ocr                                                                                                                 100%   13MB  13.0MB/s   00:00
day.ocr                                                                                                                  100%   13MB  13.0MB/s   00:01
week_.ocr                                                                                                                100%   13MB  13.0MB/s   00:00
week.ocr                                                                                                                 100%   13MB  13.0MB/s   00:00
VoteDisk.bkp                                                                                                             100%   10MB   9.8MB/s   00:01
backuplist.txt                                                                                                           100%  405     0.4KB/s   00:00
ocrbackup.sh script done at Fri Jul 22 10:12:21 AZST 2011

On node3:

-node3$ sh ocrbackup.sh
This node is not the OCR backup owner, only OCR export was tried!
ocrbackup.sh script done at Fri Jul 22 10:13:21 AZST 2011

Now let’s check $ORACLE_BASE/ocrbackup on node1:

-node1$ ls -ltr
-rw-r--r-- 1 oracle dba    129467 Jul 22 10:40 ocr.exp.rep
-rw-r--r-- 1 root   root   315925 Jul 22 10:40 ocr.exp.0
-rw-r--r-- 1 oracle dba  13582336 Jul 22 10:42 backup01.ocr
-rw-r--r-- 1 oracle dba  13582336 Jul 22 10:42 backup00.ocr
-rw-r--r-- 1 oracle dba  13582336 Jul 22 10:42 day.ocr
-rw-r--r-- 1 oracle dba  13582336 Jul 22 10:42 day_.ocr
-rw-r--r-- 1 oracle dba  13582336 Jul 22 10:42 backup02.ocr
-rw-r--r-- 1 oracle dba  13582336 Jul 22 10:42 week.ocr
-rw-r--r-- 1 oracle dba  13582336 Jul 22 10:42 week_.ocr
-rw-r--r-- 1 oracle dba  10240000 Jul 22 10:42 VoteDisk.bkp
-rw-r--r-- 1 oracle dba       405 Jul 22 10:42 backuplist.txt

We can find here, OCR backups and Vote Disk backup that was come from OCR backup owner node, from backuplist.txt we can see that it is node2. There is also OCR export file ocr.exp.0 that was made locally, as You can see the owner of the file is root, because export command was executed by root using sudo. There is also the ocr.exp.rep file, it is used for keeping the track of OCR export files, after first execution this file was created automatically, and the sequence was started from 0:

-node1$ more ocr.exp.rep
-1 #Don't edit this file! But You can remove this file to start from sequence 0.
0     /d01/ocrbackup/ocr.exp.0     Fri Jul 22 10:47:48 AZST 2011     node1

The first line, that starts from -1, is control line. Read the comment “Don’t edit this file! But You can remove this file to start from sequence 0”, don’t edit this file, also You can remove this file to start the sequence from 0. If will execute ocrbackup.sh second time on the node1, then next line will be added to the file and exp file with sequence 1 will be created:

-node1$ ls -ltr ocr.exp.*
-rw-r--r-- 1 root   root 315925 Jul 22 10:47 ocr.exp.0
-rw-r--r-- 1 oracle dba     333 Jul 22 11:01 ocr.exp.rep
-rw-r--r-- 1 root   root 315925 Jul 22 11:01 ocr.exp.1

-node1$ more ocr.exp.rep
-1 #Don't edit this file! But You can remove this file to start from sequence 0.
0     /d01/ocrbackup/ocr.exp.0     Fri Jul 22 10:47:48 AZST 2011     node1
1     /d01/ocrbackup/ocr.exp.1     Fri Jul 22 11:00:11 AZST 2011     node1

After each execution there will be new file with new sequence until it reach sequence 10, then ocr.exp.0 file will be rewritten, the cycle repeats.

This is the ocrbackup.sh script, it is tested on Linux:

#!/bin/bash

# ------------------------------------------------------------------------------
# FUNCTION
#   Copy OCR backups and Voting Disk file to the ocr backup dir on local node and
#   all available cluster nodes
# NOTES
#   Make sure that directories on remote hosts exists
# CREATED
#   Aychin Gasimov 09/2010 aychin.gasimov@gmail.com
# MODIFIED
#   ...
# ------------------------------------------------------------------------------

CP=/bin/cp
SCP=/usr/bin/scp
DD=/bin/dd
AWK=/bin/awk
TL=/usr/bin/tail
MKDIR=/bin/mkdir
GREP=/bin/grep
HEAD=/usr/bin/head
CUT=/bin/cut
ID=/usr/bin/id
LS=/bin/ls
RM=/bin/rm
SUDO=/usr/bin/sudo

if [ -z $ORACLE_HOME ]; then 
  echo "Please set ORACLE_HOME environment variable!" 
  exit 1
fi
if [ -z $ORACLE_BASE ]; then 
  echo "Please set ORACLE_BASE environment variable!" 
  exit 1
fi

OUID=`$ID|$AWK -F\( '{print $2}'|$AWK -F\) '{print $1}'`
OOWNER=`$LS -l $ORACLE_HOME/bin/oracle | $AWK {'print $3'}`

if [ ${OUID} != ${OOWNER} ]; then 
  echo "You must be logged in as $OOWNER to run $0." 
  echo "Log in as $OOWNER and restart $0 execution, also check ssh connection to remote nodes, execute 'ssh node date' if this command will prompt the password then configure ssh." 
  exit 1
fi

# OCR Backup directory
OCRBACKUPLOC=$ORACLE_BASE/ocrbackup

if [ -f $ORACLE_HOME/oraInst.loc ]; then
  ORAINVLOC=`$HEAD -1 $ORACLE_HOME/oraInst.loc | cut -d'=' -f 2`
  V1=`grep CRS $ORAINVLOC/ContentsXML/inventory.xml | awk {'print $3'}`
  export $V1
else 
  echo "Cant locate oraInst.loc file in the ORACLE_HOME, create it or edit this script to set CRS_HOME manually" exit 1
fi

# CRS_HOME location, comment this line and insert new one with correct CRS home
CRS_HOME=`echo $V1 | $CUT -d'"' -f 2`

OCRC=$CRS_HOME/bin/ocrconfig
OLSN=$CRS_HOME/bin/olsnodes
CRSC=$CRS_HOME/bin/crsctl
OCRBNODE=`$OCRC -showbackup | $AWK {'print $1'} | $HEAD -2 | $TL -1`
OCRBLOC=`$OCRC -showbackup | $AWK {'print $4'} | $HEAD -2 | $TL -1`
CURNODE=`$OLSN -l`
VDFILE=`$CRSC query css votedisk | $HEAD -1 | $AWK {'print $3'}`
NODES=(`$OLSN | $GREP -v $CURNODE`) 

if [ ! -d $OCRBACKUPLOC ]; then
 $MKDIR $OCRBACKUPLOC
fi

$OCRC -showbackup > $OCRBACKUPLOC/backuplist.txt

OCREXPREP=ocr.exp.rep

if [ ! -f $OCRBACKUPLOC/$OCREXPREP ]; then 
  echo "-1 #Don't edit this file! But You can remove this file to start from sequence 0." > $OCRBACKUPLOC/$OCREXPREP
fi 

for j in {0..10}; do
 if [ ! -f $OCRBACKUPLOC/ocr.exp.$j ]; then
         $SUDO $OCRC -export $OCRBACKUPLOC/ocr.exp.$j -s online
         echo "$j     $OCRBACKUPLOC/ocr.exp.$j     `date`     $CURNODE" >> $OCRBACKUPLOC/$OCREXPREP
         break else
   LAST=`$TL -1 $OCRBACKUPLOC/ocr.exp.rep | $AWK {'print $1'}`
   if [ $LAST = 10 ]; then LAST=-1; fi if [ $j = $[LAST+1] ]; then
         $RM -f $OCRBACKUPLOC/ocr.exp.$j
         $SUDO $OCRC -export $OCRBACKUPLOC/ocr.exp.$j -s online
         echo "$j $OCRBACKUPLOC/ocr.exp.$j `date` $CURNODE" >> $OCRBACKUPLOC/$OCREXPREP
         break
   fi
 fi 
done

if [ $CURNODE = $OCRBNODE ]; then
 $CP $OCRBLOC/*.ocr $OCRBACKUPLOC
 $DD if=$VDFILE of=$OCRBACKUPLOC/VoteDisk.bkp

 for i in ${NODES[@]}; do
  $SCP $OCRBLOC/*.ocr ${i}:$OCRBACKUPLOC
  $SCP $OCRBACKUPLOC/VoteDisk.bkp ${i}:$OCRBACKUPLOC
  $SCP $OCRBACKUPLOC/backuplist.txt ${i}:$OCRBACKUPLOC
 done 
else
 echo "This node is not the OCR backup owner, only OCR export was tried!" 
fi

echo "$0 script done at `date`" 

exit 0

P.S.: Please fill free to make any changes, then send them to me and I will update the script on the site, and put Your name under the MODIFIED section in the script header. One of the modifications can be adding variable to store the number of export files to keep, at this moment it is hard coded to 10.


(c) Aychin Gasimov, 07/2011, Azerbaijan Republic


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: