Automated Patching – Oracle Fusion Middleware 11g

Why?

The reason I am addressing this is that, while working on patches for 11g servers, I was so tired of doing it manually. Staging the patch out to each server, going to each one running the same mundane commands to install (with just the slight variations for each one). I decided I would just create 2 scripts to get this done instead.

Two Scripts?

I had to create two scripts in order to stage the patches from one server to the others. While there might have been a better (more all in one) way of doing this, I felt it was better to keep it plain and simple. The two scripts I wrote are patchOracle.sh (for the actual patching of the server) and stagePatches.sh (as the name says this one is to stage the patches from one server to the others).

Script one (stagePatches.sh)

This script resides on the server where the patches are downloaded from Oracle Support to. You will need to change the variables to suite where you want to grab the patches from (ie. Where you download them to):

## stagePatches.sh
## Variables for this script
patchID=${1}
patchType=${2}
hostListInput=${3}
stageDir="/stage/dir/stage-tmp"
oracleHome="/my/oracle/home"
function stageBSU(){
 echo "SCPing patched to staging directory on hosts . . "
 hostList=`echo ${hostListInput} | sed 's/,/\\n/g'`
 for host in ${hostList}
 do
 scp ${stageDir}/${patchID}.zip ${host}:${oracleHome}/utils/bsu/cache_dir/
 echo "Staged to ${host}"
 done
 echo ". . FINISHED"
}
function stageOPatch(){
 echo "SCPing patched to staging directory on hosts . . "
 hostList=`echo ${hostListInput} | sed 's/,/\\n/g'`
 for host in ${hostList}
 do
 scp ${stageDir}/${patchID}.zip ${host}:~/
 echo "Staged to ${host}"
 done
 echo ". . FINISHED"
}
function help(){
 echo "Help Function:"
 echo "Usage: ${0} patchID patchType hostList"
 echo "Examples:"
 echo "BSU: ${0} IHFB bsu hostname01,hostname02"
 echo "OPatch: ${0} 17802511 opatch hostname01,hostname02"
 exit 0
}

function checkProd(){
 #this is for checking if staging to a prod environment (change the *prod* to a string checking the hostname for a prod indicator, this is obviously different for each environment).
 if [[ ${hostList} = *prod* ]]; then
 echo "You are applying a patch on a production environment!!"
 read -p "Press [Enter] to continue or ctr-C to quit."
 echo ""
 fi
}

case ${patchType} in
 bsu | -bsu )
 checkProd "${hostList}"
 stageBSU "${patchID}" "${hostList}" "${stageDir}"
 ;;

 opatch | -opatch )
 checkProd "${hostList}"
 stageOPatch "${patchID}" "${hostList}" "${stageDir}"
 ;;
 *)

help
exit 0
;;


esac

Script two (patchOracle.sh)

This script resides on the server where the patches are to be installed. You will need to change the variables to suite where you want to grab the patches from (ie. Where you scp’d the patches to):

## patchOracle.sh

## Variables for this script
patchID=${1}
logFile="~/patchOracle.log"
oracleHome="/oracle/home/dir"

function bsuPatching(){
 ## Run BSU on host
 echo "Unzipping patch . . . ."
 cd ${oracleHome}/utils/bsu/cache_dir/
 unzip ${patchID}.zip
 echo "Applying BSU patch on host now"
 cd ${oracleHome}/utils/bsu/
 ./bsu.sh -prod_dir=${oracleHome}/wlserver_10.3 -patchlist=${patchID} -verbose -install
 echo "${patchID} has been applied!!"
}
function osbPatching(){
 ## Run OSB patch on host
 echo "Unzipping patch . . . ."
 cd ~/
 unzip ${patchID}.zip
 cd ~/${patchID}/
 echo "Applying OSB patch on host now"
 ${oracleHome}/Oracle_OSB1/OPatch/opatch apply
 echo "${patchID} has been applied!!"
}
function soaPatching(){
 ## Run SOA patch on host
 echo "Unzipping patch . . . ."
 cd ~/
 unzip ${patchID}.zip
 cd ~/${patchID}/
 echo "Applying SOA patch on host now"
 ${oracleHome}/Oracle_SOA1/OPatch/opatch apply
 echo "${patchID} has been applied!!"
}
# Swap out the *prod* for a better way of telling whether the host is a prod or non production en#vironment
function checkProd(){
 hostname=`hostname`
 if [[ ${hostname} = *prod* ]]; then
 echo "You are applying a patch on a production environment!!"
 read -p "Press [Enter] to continue or ctr-C to quit."
 echo ""
 fi
}
function bsuCheck(){
 ## Run BSU on host
 echo "Checking if patch is installed already . . "
 cd ${oracleHome}/utils/bsu/
 check=`./bsu.sh -report |grep ${patchID}`
 if [[ ${check} ]]; then
 echo "${patchID} is already installed!!"

 exit 1
 fi
 echo "Patch ID: ${patchID} is not installed yet, proceeding . . "
}
function osbCheck(){
 ## Check to see if osb patch is already installed
 echo "Checking if patch is installed already . . "
 check=`${oracleHome}/Oracle_OSB1/OPatch/opatch lsinventory |grep ${patchID}`
 if [[ ${check} ]]; then
 echo "${patchID} is already installed!!"
 exit 1
 fi
 echo "Patch ID: ${patchID} is not installed yet, proceeding . . "
}
function soaCheck(){
 ## Check to see if soa patch is already installed
 echo "Checking if patch is installed already . . "
 check=`${oracleHome}/Oracle_SOA1/OPatch/opatch lsinventory |grep ${patchID}`
 if [[ ${check} ]]; then
 echo "${patchID} is already installed!!"
 exit 1
 fi
 echo "Patch ID: ${patchID} is not installed yet, proceeding . . "
}
function help(){
 echo "Help Function:"
 echo "Usage: ${0} patchID patchType"
 echo "Examples:"
 echo "BSU: ${0} IHFB bsu"
 echo "OSB: ${0} 17802511 osb"
 echo "SOA: ${0} 17802001 soa"
 exit 0
}
function startPatching(){
 echo "####################################" >> ${logFile}
 echo "Patching has been initiated on ${hostname} for patch number ${patchID}" >> ${logFile}
 echo "####################################" >> ${logFile}
}
function finishedPatching(){
 echo "####################################" >> ${logFile}
 echo "Patching has been finished on ${hostname} for patch number ${patchID}" >> ${logFile}
 echo "####################################" >> ${logFile}
}

case ${2} in
 bsu | -bsu )
 if [ $# -ne 2 ]; then
 help
 fi
 checkProd
 startPatching ${patchID}
 bsuCheck ${patchID}
 bsuPatching ${patchID}
 finishedPatching ${patchID}
 ;;
 osb | -osb )
 if [ $# -ne 2 ]; then
 help
 fi
 checkProd
 startPatching ${patchID}
 osbCheck ${patchID}
 osbPatching ${patchID}
 ;;
 soa | -soa )
 if [ $# -ne 2 ]; then
 help
 fi
 checkProd
 startPatching ${patchID}
 soaCheck ${patchID}
 soaPatching ${patchID}
 finishedPatching ${patchID}
 ;;
 *)
 help
 exit 0
 ;;
esac

Leave a Reply