Stop and start your Autonomous Databases automatically and save 70% CPU cost

How many Databases are you running in test and development environments? On-premise, you buy the licenses, the hardware, pay for support, and pay the operation cost! 24×7!

BUT! In most cases, nobody is using these databases at weekends, at night, and in different stages of the project.

In the Cloud, you pay only for what you really use! So why not moving all these databases to the Cloud, shut them down when not needed, and save hard cash?

On top of that, Oracle Autonomous Database does all the maintenance work for you, including patching and upgrade, so you focus on other tasks that bring even more value to your business.

Calculation

7 * 24 = 168 hours per week. 

Weekend: 48 hours. 

5 working days from 6p.m. to 8a.m.: 5 * 14 = 70 hours.

(48+70)/168=0.70 –> 70%

How to do it automatically

First, you need a machine that is running all the time. You can use one of the two free Virtual Machines included in the Alawys Free offer, and run a cron job on it that executes shell scripts to stop and start the databases at desired time.

Preparation

To access your OCI resources, including your Autonomous Databases, you need the following:

  1. Create and upload API Signing Key to your tenancy, and get the fingerprint
  2. Path to Private Key you generated in step 1, e.g.: /home/opc/.oci/oci_api_key.pem
  3. Your tenancy OCID
  4. Your user OCID

Option 1: Using REST API calls

Step 1: download the following script https://docs.cloud.oracle.com/iaas/Content/Resources/Assets/signing_sample_bash.txt and save it on your VM in /home/opc/restapi/oci-curl.sh and change the following four values you got in the preparation

No alt text provided for this image

Step 2: create a json file /home/opc/restapi/empty.json with actually no content, only the opening { and closing } braces

cat empty.json
No alt text provided for this image

Step 3: create a shell script /home/opc/restapi/stop.sh with the following content and make it executable:

#!/bin/bash
. ./oci-curl.sh
DB_OCID=ocid1.autonomousdatabase.oc1.eu-frankfurt-1.xxxxxxxxx
oci-curl database.eu-frankfurt-1.oraclecloud.com POST ./empty.json /20160918/autonomousDatabases/$DB_OCID/actions/stop

chmod +x /home/opc/restapi/stop.sh

The DB_OCID is the OCID of your Autonomous Database you want to stop.

Step 4: schedule running the script at desired time, e.g. every day at 6p.m. using a cron job

crontab -e
0 18 * * * /home/opc/restapi/stop.sh

Step 5: to start the Autonomous Database, repeat steps 3 and 4 and replace the word “stop” with “start”.

Option 2: Using OCI CLI

OCI CLI is a small footprint tool that you can use to complete tasks on your OCI resources. OCI CLI might be more user frendly than creating JSON if you are doing more complicated tasks.

Step 1install the OCI CLI tool. It takes only few minutes.

Step 2: create a shell script /home/opc/ocicli/stop.sh with the following content and make it executable:

DB_OCID=ocid1.autonomousdatabase.oc1.eu-frankfurt-1.xxxxxxxxx
oci db autonomous-database stop --autonomous-database-id $DB_OCID

chmod +x /home/opc/ocicli/stop.sh

Step 3: schedule running the script at desired time, e.g. every day at 6p.m. using a cron job

crontab -e
0 18 * * * /home/opc/ocicli/stop.sh

Step 4: to start the Autonomous Database, repeat steps 2 and 3 and replace the word “stop” with “start”.

Make your scripts dynamic

If your are managing many databases at the same time, adding or removing any more frequently, then you wouldn’t wont to edit your scripts manually each time a database is created or deleted. In this case, query the OCIDs of your existing databases at run time:

oci db autonomous-database list --compartment-id $COMP_OCID

Extract the OCIDs and create a while looping to stop or start them all.

In case you have any more important databases you want to keep out of the scheduled stop and start, then create a whitelist.txt file with corresponding OCIDs and execlude them in the while loop. Another “nicer” option would be by assigning TAGs to these databases and use the TAGs to execlude them from stoping automatically.

Save even more!

If you want to save even more, just shut down the databases at the end of the business day, and DON’T start them automatically the next day. In this case, databases not needed at all for longer time, e.g. because nobody is testing in the next 3 weeks, stay down all the time and save even more cost!

If the database is needed, the first employee at the office start it manually in the morning before getting the first coffee. Or even better, while waiting for the coffee at the coffee machine using Siri.

Using Siri to run shell commands

Step 1: install the Shortcuts APP in your mobile phone

Step 2: create a shortcut named “Start Autonomous Database” and configure it as follows:

No alt text provided for this image

Host: the IP address of your VM

Port: 22

User: opc

Authentication: SSH Key

SSH Key: click on “Generate Key” and add it to the /home/opc/.ssh/authorized_keys file on your VM.

Input: enter the path to your shell script that starts the Autonomous Database.

Step 3: while you are waiting for your coffee, start Siri and say “Start Autonomous Database” 🙂

Would you like to get notified when the next post is published?