A script for Relocating and Restoring Database Services
Due to complex requirements for service relocate and restore as part of patching, I decided to script the process.
Requirements: ```code Critical services should only be relocated once as part of patching. After patching, non-critical services will need to be relocated to other instance. Critical services are defined as svc_21-29.
## Initial Environment and Service Configuration Existing configuration: ```code [oracle@racnode-dc2-1 patch]$ . /media/patch/hawk.env The Oracle base has been set to /u01/app/oracle ORACLE_UNQNAME=hawk ORACLE_SID=hawk1 ORACLE_BASE=/u01/app/oracle ORACLE_HOME=/u01/app/oracle/12.2.0.1/db1 Oracle Instance alive for sid "hawk1" [oracle@racnode-dc2-1 patch]$ srvctl status database -d $ORACLE_UNQNAME -v Instance hawk1 is running on node racnode-dc2-1 with online services svc_11,svc_12,svc_13,svc_14,svc_15,svc_16,svc_17,svc_18,svc_19. Instance status: Open. Instance hawk2 is running on node racnode-dc2-2 with online services svc_21,svc_22,svc_23,svc_24,svc_25,svc_26,svc_27,svc_28,svc_29. Instance status: Open.
Capturing Baseline Service Affinity
Save existing services affinity:
[oracle@racnode-dc2-1 patch]$ ./save_service.sh $ORACLE_UNQNAME ##### Save services affinity: /tmp/service_hawk1.conf Instance hawk1 is running on node racnode-dc2-1 with online services svc_11,svc_12,svc_13,svc_14,svc_15,svc_16,svc_17,svc_18,svc_19. Instance status: Open. ##### Save services affinity: /tmp/service_hawk2.conf Instance hawk2 is running on node racnode-dc2-2 with online services svc_21,svc_22,svc_23,svc_24,svc_25,svc_26,svc_27,svc_28,svc_29. Instance status: Open. -r-------- 1 oracle oinstall 156 Jun 3 17:40 /tmp/service_hawk1.conf -r-------- 1 oracle oinstall 156 Jun 3 17:40 /tmp/service_hawk2.conf
Note: /tmp/service_hawk*.conf is read-only to prevent accidental overwrite.
Relocating Services for Instance-Level Patching
Patching starts from instance1 and relocate all services to instance2.
[oracle@racnode-dc2-1 patch]$ ./relocate_service.sh $ORACLE_UNQNAME 1 2 ******************************** ***** SERVICES CURRENT LOCATION: ******************************** Instance hawk1 is running on node racnode-dc2-1 with online services svc_11,svc_12,svc_13,svc_14,svc_15,svc_16,svc_17,svc_18,svc_19. Instance status: Open. Instance hawk2 is running on node racnode-dc2-2 with online services svc_21,svc_22,svc_23,svc_24,svc_25,svc_26,svc_27,svc_28,svc_29. Instance status: Open. ****************************** ***** CMD RELOCATE SERVICES TO: hawk2 ****************************** + srvctl relocate service -d hawk -service svc_11 -oldinst hawk1 -newinst hawk2 ... Instance hawk1 is running on node racnode-dc2-1. Instance status: Open. Instance hawk2 is running on node racnode-dc2-2 with online services svc_11,svc_12,svc_13,svc_14,svc_15,svc_16,svc_17,svc_18,svc_19,svc_21,svc_22,svc_23,svc_24,svc_25,svc_26,svc_27,svc_28,svc_29. Instance status: Open.
Patching completed from instance1. Patching starts from instance2 and relocate all services to instance1.
[oracle@racnode-dc2-1 patch]$ ./relocate_service.sh $ORACLE_UNQNAME 2 1 ******************************** ***** SERVICES CURRENT LOCATION: ******************************** ... ****************************** ***** CMD RELOCATE SERVICES TO: hawk1 ****************************** + srvctl relocate service -d hawk -service svc_11 -oldinst hawk2 -newinst hawk1 ... + srvctl relocate service -d hawk -service svc_21 -oldinst hawk2 -newinst hawk1 ... Instance hawk1 is running on node racnode-dc2-1 with online services svc_11,svc_12,svc_13,svc_14,svc_15,svc_16,svc_17,svc_18,svc_19,svc_21,svc_22,svc_23,svc_24,svc_25,svc_26,svc_27,svc_28,svc_29. Instance status: Open. Instance hawk2 is running on node racnode-dc2-2. Instance status: Open.
Finalizing Service Distribution and Restoring Affinity
Patching is completed and all services are running from instance1. Instead of relocating services back to its original instance, non-critical services are relocated to the other instance. Essentially, svc_11-19 is relocated 3x while svc_21-29 is relocated 1x.
Non-Critical services were running from instance1 per /tmp/service_hawk1.conf when save_service.sh was run. Non-Critical services are currently running from instance1 and will need to be restored to instance2.
[oracle@racnode-dc2-1 patch]$ ./restore_service.sh $ORACLE_UNQNAME 1 2 /tmp/service_hawk1.conf ******************************** ***** SERVICES CURRENT LOCATION: ******************************** Instance hawk1 is running on node racnode-dc2-1 with online services svc_11,svc_12,svc_13,svc_14,svc_15,svc_16,svc_17,svc_18,svc_19,svc_21,svc_22,svc_23,svc_24,svc_25,svc_26,svc_27,svc_28,svc_29. Instance status: Open. Instance hawk2 is running on node racnode-dc2-2. Instance status: Open. ************************** ***** SAVED CONFIGURATION: /tmp/service_hawk1.conf ************************** Instance hawk1 is running on node racnode-dc2-1 with online services svc_11,svc_12,svc_13,svc_14,svc_15,svc_16,svc_17,svc_18,svc_19. Instance status: Open. ************************************* ***** CMD - RESTORE SERVICES TO: hawk2 ************************************* + srvctl relocate service -d hawk -service svc_11 -oldinst hawk1 -newinst hawk2 ... Instance hawk1 is running on node racnode-dc2-1 with online services svc_21,svc_22,svc_23,svc_24,svc_25,svc_26,svc_27,svc_28,svc_29. Instance status: Open. Instance hawk2 is running on node racnode-dc2-2 with online services svc_11,svc_12,svc_13,svc_14,svc_15,svc_16,svc_17,svc_18,svc_19. Instance status: Open.
Automation Scripts: Save, Relocate, and Restore
Hopefully, patching is not always this complex and if it is, maybe the scripts will be useful.
save_service.sh
#!/bin/sh -e # MDinh : Mar 27, 2019 # DN=`dirname $0` BN=`basename $0` DB=${1:?"---> USAGE: $DN/$BN <db_unique_name>"} IFS="," save_service () { OUTF=/tmp/service_$i.conf srvctl status instance -d ${DB} -instance ${i} -v > $OUTF echo echo "##### Save services affinity: $OUTF" chmod 400 $OUTF cat $OUTF echo } inst=$(srvctl config database -db ${DB} |grep "^Database instances" | awk -F' ' '{print $3}') for i in ${inst}; do save_service done ls -lh /tmp/service*.conf exit
relocate_service.sh
#!/bin/sh # relocate_service.sh # MDinh : Jun 02, 2019 # DN=`dirname $0` BN=`basename $0` DB=${1:?"---> USAGE: $DN/$BN <db_unique_name> <oldinst> <newinst>"} OLD=${2:?"---> USAGE: $DN/$BN <db_unique_name> <oldinst> <newinst>"} NEW=${3:?"---> USAGE: $DN/$BN <db_unique_name> <oldinst> <newinst>"} echo echo "********************************" echo "***** SERVICES CURRENT LOCATION:" echo "********************************" srvctl status database -d ${DB} -v echo echo "******************************" echo "***** CMD RELOCATE SERVICES TO: ${DB}${NEW}" echo "******************************" OUTF=/tmp/old_service_${DB}${OLD}.loc srvctl status instance -d ${DB} -instance ${DB}${OLD} -v > $OUTF svc=$(tail -1 $OUTF| awk -F" " '{print $11}'| awk '{$0=substr($0,1,length($0)-1); print $0}') IFS="," for s in ${svc} do set -x srvctl relocate service -d ${DB} -service ${s} -oldinst ${DB}${OLD} -newinst ${DB}${NEW} set +x done srvctl status database -d ${DB} -v exit
restore_service.sh
#!/bin/sh # restore_service.sh # MDinh : Jun 02, 2019 # DN=`dirname $0` BN=`basename $0` DB=${1:?"---> USAGE: $DN/$BN <db_unique_name> <oldinst> <newinst> <conf>"} OLD=${2:?"---> USAGE: $DN/$BN <db_unique_name> <oldinst> <newinst> <conf>"} NEW=${3:?"---> USAGE: $DN/$BN <db_unique_name> <oldinst> <newinst> <conf>"} CONF=${4:?"---> USAGE: $DN/$BN <db_unique_name> <oldinst> <newinst> <conf>"} echo echo "********************************" echo "***** SERVICES CURRENT LOCATION:" echo "********************************" srvctl status database -d ${DB} -v echo echo "**************************" echo "***** SAVED CONFIGURATION: $CONF" echo "**************************" cat $CONF echo echo "*************************************" echo "***** CMD - RESTORE SERVICES TO: ${DB}${NEW}" echo "*************************************" svc=$(tail -1 $CONF| awk -F" " '{print $11}'| awk '{$0=substr($0,1,length($0)-1); print $0}') IFS="," for s in ${svc} do set -x srvctl relocate service -d ${DB} -service ${s} -oldinst ${DB}${OLD} -newinst ${DB}${NEW} set +x done srvctl status database -d ${DB} -v exit
Oracle Database Consulting Services
Ready to optimize your Oracle Database for the future?
Share this
Share this
More resources
Learn more about Pythian by reading the following blogs and articles.
How to Stop All Database Instances Running From ORACLE_HOME
How to Execute 19c runcluvfy.sh With Root and Sudo Method
Testing the 19c Grid Infrastructure by Performing a Dry-Run Upgrade
Ready to unlock value from your data?
With Pythian, you can accomplish your data transformation goals and more.