Practical Autonomous DB with OCI CLI — Cross Region & Local Switchover

Data Voyage by Jigar Doshi
4 min readMar 24, 2023

--

Script Autonomous DB switchover using OCI CLI. Improve DR response and drill turn around time. Automate routine tasks.

In my previous Practical ADB posts, I have described

  1. Creating Primary and Cross Region Standby
  2. Generating Wallet and connection strings
  3. Start, Stop, Restart, Delete and Scale database

In this article, I will demonstrate how easy is it perform both cross-region and local switchover using OCI CLI. Scripting switchover operations with CLI saves you time during actual DR scenario and lends itself to further integration with related application switchover steps. Instead of relying on a human to perform it manually a script can be called within a DR framework such as OCI full stack disaster recovery , custom cron jobs or any other tool.

All the scripts in this series take the approach of getting OCID’s dynamically based on 2 simple input parameters, namely Compartment name and DB name. This makes it easy for anyone to use. Very often I notice users struggle to locate/identify correct OCID and manually copy it to execute CLI. Getting OCID programmatically makes it easy to manage and operate

Let’s get started by first saving compartment, primary & remote standby (peer DB) OCID’s.

# Replace <INSERT_COMPARTMENT_NAME> with actual compartment name (case sensitive)
# Assumes unique compartment name in tenancy. Adjust for duplicate compartment names
export compname=<INSERT_COMPARTMENT_NAME>
export Compocid=`oci iam compartment list --compartment-id-in-subtree true --all |jq -r ".data[] | select(.name == \"${compname}\") | .id"`
# Replace <INSERT_DB_NAME> with actual DB name.
export DBname=<INSERT_DB_NAME>

# Save DB OCID in variable based on name(case sensitive)
export ATPocid=`oci db autonomous-database list --compartment-id $Compocid --query "data[?contains(\"display-name\",'$DBname')]"|jq -r ".[].id"`
#This assumes only cross region DG setup. To be adjusted for both local and cross region DG.
# Get DataGuard Peer OCID
export Peerdbocid=`oci db autonomous-database get --autonomous-database-id $ATPocid|jq -r '.data."peer-db-ids"[0]'`
#Get Peer Region from Peer OCID
export PeerRegion=`echo $Peerdbocid|cut -d . -f 4`

Scenario 1- Cross region switchover and monitor progress.

#Switchover DB 
echo "Initiating Switchover - Status update can take few minutes"
export WorkReqOCID=`oci db autonomous-database switchover --autonomous-database-id $ATPocid --peer-db-id $Peerdbocid |jq -r '."opc-work-request-id"'`

# Loop to check switchover status. Exit loop when switchover is successful.

while true
do
export status=`oci work-requests work-request get --work-request-id $WorkReqOCID|jq -r ".data.status"`
echo "Switchover request is $status"
#exit the loop once switchover is completed
if [ $status == "SUCCEEDED" ]
then
#Sleep 1 time before checking DB recreation status
sleep 20
break
fi
echo "Sleeping 60 seconds"
echo "Ctrl-C to exit"
sleep 60
done

Original primary ADB is automatically recreated as standby after the switchover to new primary (old standby) ADB. This is somewhat different than traditional DB switchover process.

To monitor the standby recreation request status, execute the below code block. Keep in mind this can take anywhere from few minutes to hours depending on the size of DB.

#check ADB DG recreation status
#This will only work if there is only 1 active DG recreation request.
#Future Todo - Modify to exclude any old request
# Get Active ADB DG recreation work request ID
export ADBGRecreate_workrequest_id=`oci work-requests work-request list --compartment-id $Compocid --resource-id $Peerdbocid --region $PeerRegion --query "data[?status=='ACCEPTED'&&\"operation-type\"=='Recreate Autonomous Data Guard standby']"|jq -r '.[].id'`

# Monitor ADB DG recreation status
while true
do
export percent_complete_status=`oci work-requests work-request get --work-request-id $ADBGRecreate_workrequest_id --region $PeerRegion |jq -r '.[]."percent-complete"'`
echo "Autonomous Data Guard Standby Recreation in $PeerRegion is $percent_complete_status % completed"
if [ $percent_complete_status == "100" ]
then
break
fi
echo "Sleeping 60 seconds"
echo "Ctrl-C to exit"
sleep 60
done

Scenario 2- Local switchover & monitor progress

#Switchover DB 
echo "Initiating Switchover - Status update can take few minutes"
export WorkReqOCID=`oci db autonomous-database switchover --autonomous-database-id $ATPocid |jq -r '."opc-work-request-id"'`

# Loop to check switchover status. Exit loop when switchover is successful.

while true
do
export status=`oci work-requests work-request get --work-request-id $WorkReqOCID|jq -r ".data.status"`
echo "Switchover request is $status"
#exit the loop once switchover is completed
if [ $status == "SUCCEEDED" ]
then
echo "Switchover Succeeded for $DBname"
break
fi
echo "Sleeping 20 seconds"
echo "Ctrl-C to exit"
sleep 20
done

Note 1— In case of local standby switchover there is no additional step of recreating standby after switchover, hence local switchover is faster than cross region switchover.

Note 2 — If the local standby is Backup based instead of Autonomous DG (ADG) the switchover duration will be longer.

If you have been following the series, you will realize that CLI makes lot of complex tasks easy to script and simplifies day to day activities.

Thank you for reading so far and hope this was useful. If you like to see any other ADB operation using CLI, leave a note or say hello in comments.

Refer switchover_local_adb.sh and switchover_crossregion_adb.sh in my repo for complete script.

Tip — To verify the execution flow and logic, execute in following format

sh -x switchover_local_adb.sh MYcompartment MYDBname
sh -x switchover_crossregion_adb.sh MYcompartment MYDBname

--

--

Data Voyage by Jigar Doshi

Master Principal Cloud Architect @ Oracle Singapore. Data enthusiast. Sharing my adventures in world of data