Saturday, August 2, 2025

Auto Shutdown and Restart of Oracle DB Systems in OCI Using Functions

 

๐Ÿ”น Introduction

Oracle Cloud Infrastructure (OCI) Database Systems incur compute costs even when idle. If you're running non-production or development environments, one simple cost-saving strategy is to automate their shutdown at night and restart in the morning.

In this blog, I’ll walk you through setting up an automated stop/start schedule for your DB Systems using OCI Functions (or OCI CLI if preferred). This is a practical cost control technique every Oracle DBA should know.

๐Ÿ”น Use Case

✔️ You have a DB System (Bare Metal or VM) running in OCI
✔️ It is used only during business hours
✔️ You want to stop it at 7 PM and start it at 7 AM automatically
✔️ You want a serverless and secure way to do this

๐Ÿ”น Option 1: OCI CLI + Cron (Simpler for DBAs)

If you're using a Linux jump server or Bastion host:

✅ Create a Shell Script to Stop DB

#!/bin/bash DB_OCID="<your_db_system_ocid>" oci db system stop --db-system-id $DB_OCID --wait-for-state STOPPED

✅ Create a Script to Start DB

#!/bin/bash DB_OCID="<your_db_system_ocid>" oci db system start --db-system-id $DB_OCID --wait-for-state AVAILABLE

✅ Add to Crontab

Edit crontab:

crontab -e

Add:

0 19 * * * /home/opc/stop_db.sh 0 7 * * * /home/opc/start_db.sh

That's it. Your DB system now auto-starts and stops every day!

๐Ÿ”น Option 2: OCI Functions + Event Schedule (Serverless Approach)

This is a more cloud-native, scalable approach.

✅ Step 1: Create OCI Function Application

  • Go to Developer ServicesFunctions

  • Create an Application in your preferred compartment and subnet

✅ Step 2: Create a Python Function

Use this Python function to stop/start a DB system:

import io import json import oci def handler(ctx, data: io.BytesIO = None): signer = oci.auth.signers.get_resource_principals_signer() db_client = oci.database.DatabaseClient(config={}, signer=signer) body = json.loads(data.getvalue()) action = body.get("action") # 'start' or 'stop' db_system_id = body.get("db_system_id") if action == "stop": response = db_client.stop_db_system(db_system_id) elif action == "start": response = db_client.start_db_system(db_system_id) else: return json.dumps({"status": "invalid action"}) return json.dumps({"status": "submitted", "action": action})

✅ Step 3: Deploy and Test Function

Use fn CLI to deploy the function and test it with:

fn invoke myapp stop_db --content '{"action": "stop", "db_system_id": "<ocid>"}'

✅ Step 4: Create Scheduled Rule

  • Go to Observability & ManagementEvents

  • Create a Scheduled Rule

    • Time: Daily at 07:00 and 19:00

    • Action: Invoke Function with payload (start/stop + OCID)

๐Ÿ’ก Vignesh’s Tip

If your DB system is critical, always check its current lifecycle state before issuing start/stop commands. Add checks like:


STATE=$(oci db system get --db-system-id $DB_OCID --query 'data."lifecycle-state"' --raw-output)

This avoids redundant API calls or errors.

๐Ÿ”น Benefits

  • ⏱️ Zero manual intervention

  • ๐Ÿ’ฐ Significant cost savings for idle environments

  • ๐Ÿ”’ Secure and auditable (Functions run with resource principals)

  • ⚙️ Easy to scale across multiple DB systems

๐Ÿ”น Conclusion

Automating the start and stop of Oracle DB Systems using OCI CLI or Functions is a smart way to save costs without compromising control. Whether you're a DBA, cloud engineer, or DevOps team, this approach helps keep your cloud bill in check while maintaining full flexibility.

No comments:

Post a Comment

Oracle OS Management Hub in OCI – A Complete Overview

  Oracle OS Management Hub in OCI – A Complete Overview In any enterprise IT landscape, managing operating systems across hundreds of compu...