X

The latest cloud infrastructure announcements, technical solutions, and enterprise cloud insights.

How to backup block volume using OCI CLI and why would I want it?

Introduction

Block volume backup allow you to make a point in time snapshot of the data on a block volume. It can be backed up anytime whether it is attached or detached from an instance. Backing up block volume can be achieved easily using the OCI web console. You can select from 3 oracle defined backup policies which are gold, silver and bronze with different backup type, backup schedules and retention period. Should you find the configuration not appropriate, you may define a custom backup policy with your own configuration of backup type, backup schedule and retention period.

If you take a closer look to the configuration as shown in the following screenshot, when adding schedule, you can choose the schedule type as either, Daily, Weekly, Monthly or Yearly.

And for daily schedule, if you scroll down the hour of the day, as per the screenshot shown below, you can only select at most once everyday.

What if you want to backup every 2 or 3 hourly. There is no way (at least as of now) that you can achieve it in the web console. So here is the workaround that I would show you.

Backing Up Block Volume using OCI CLI

For information on how to install OCI CLI, you may refer to here.

To create a backup of a block volume, find out the block volume id. It is the OCID field in the block volume details. 

To run a backup using OCI CLI, run the following command. This will by default run an incremental backup. If you want a full backup, just add -type full at the end of the command.

oci bv backup create –volume-id <Block Volume OCID>

 

How to manage backup retention for manual backup

For backup created using OCI CLI, you will need to maintain the retention of the backup manually, which means that you will need to manually delete the backup, or else the backup will be stored indefinitely.

I will show an example of housekeeping the backup using bash script with OCI CLI. Please take this as a reference and test thoroughly before you adopt it.

The script will required a command line tool called "jq" to parse the output of OCI CLI.

#!/bin/bash
# Set retention days
Retention=30

set -e

compartmentList=$(oci bv backup list --compartment-id "ocid1.compartment.oc1..aaaaaaaau337m2maguz3zrssc4ie3jfkyfxalbpmgu2wglo26l7dg3nxmyaa")

for i in $(echo "$compartmentList" | jq '.data | keys | .[]')
do
    ID=$(echo $compartmentList | jq -r ".data[$i].\"id\"")
    SizeGB=$(echo $compartmentList | jq -r ".data[$i].\"size-in-gbs\"")
    SourceType=$(echo $compartmentList | jq -r ".data[$i].\"source-type\"")
    BackupType=$(echo $compartmentList | jq -r ".data[$i].\"type\"")
    TimeCreated=$(echo $compartmentList | jq -r ".data[$i].\"time-created\"")
    CurrentDate=`date`
    DayDiff=$((`echo "$(($(date -d "$CurrentDate" '+%s') - $(date -d "$TimeCreated" '+%s')))"`/86400))


    echo "ID: $ID"
    echo "Size in GB: $SizeGB"
    echo "Source Type: $SourceType"
    echo "Backup Type: $BackupType"
    echo "Time Created: $TimeCreated"
    echo "Day Difference: $DayDiff"

    # Enable the following block only when you want to delete block volume backup that is greater than $retention
    if [ $DayDiff -gt $Retention ]; then
        oci bv backup delete --volume-backup-id $ID --force
        echo $ID deleted!!!
    fi
    # Enable the following block only when you want to delete block volume backup that is greater than $retention

    echo "----------------------"

done
 

 

Summary

Backing up block volume could be done either in OCI web console or OCI CLI. However, if you have a requirement to backup a block volume multiple times within a day, OCI CLI should be the tool for you. 

 

Learn More

If you want to know more about how to setup OCI CLI to perform manual backup of block volume in OIC, you may follow links as below.

Click here to learn more about OCI CLI.

Click here to learn more about backup policies in OCI console.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha