Dear All,
Good Day!!!
Today, I would like to share a recent customer requirement involving the cleanup of old OCI volume backups, including both boot volume and block volume backups that were created manually during maintenance activities.
Since these manually created backups are not under any retention policy, they must be identified and removed manually to avoid unnecessary storage consumption and associated costs.
The customer had the following requirements:
- Delete all volume backups that are older than 7 days.
- Exclude any backups that contain the keyword DO_NOT_DELETE in their name.
To address this requirement, I developed the following script. The script can be further customized and enhanced based on specific operational or business requirements. This script can be executed either from OCI Cloud Shell or from any compute instance where the OCI CLI is installed and properly configured.
Boot volume backup cleanup program
##ENV SETTINGexport COMPARTMENT_OCID=<compartment OCID>export DATE1=<RETENTION_DATE> ##format YYYY-MM-DDTHH:MI:SSZexport EXCLUDE_STRING=<string to exclude> ##for example do_not_delete##INDENTIFY BACKUPSoci bv boot-volume-backup list --compartment-id "$COMPARTMENT_OCID" --all \ | jq -r --arg date "$DATE1" '.data[] | select(.["time-created"] < $date) | .id + " "+ ."display-name" + " "+ ."lifecycle-state"' |grep -v $EXCLUDE_STRING | grep -v TERMINATED \ | awk '{print $1}' > boot_backup_ids.txt##DELETION OF IDENTIFIED BACKUPSfor backup_id in $(cat boot_backup_ids.txt); do if [ -n "$backup_id" ]; then echo "Deleting boot volume backup: $backup_id" oci bv boot-volume-backup delete --boot-volume-backup-id "$backup_id" --force fidone
Block volume backup cleanup program
##ENV SETTINGexport COMPARTMENT_OCID=<compartment OCID>export DATE1=<RETENTION_DATE> --format YYYY-MM-DDTHH:MI:SSZexport EXCLUDE_STRING=<string to exclude> --for example do_not_delete##INDENTIFY BACKUPSoci bv backup list --compartment-id "$COMPARTMENT_OCID" --all \ | jq -r --arg date "$DATE1" '.data[] | select(.["time-created"] < $date) | .id + " "+ ."display-name" + " "+ ."lifecycle-state"' |grep -v $EXCLUDE_STRING | grep -v TERMINATED \| awk '{print $1}' > block_backup_ids.txt##DELETION OF IDENTIFIED BACKUPSfor backup_id in $(cat block_backup_ids.txt); do if [ -n "$backup_id" ]; then echo "Deleting block volume backup: $backup_id" oci bv backup delete --volume-backup-id "$backup_id" --force fidone
It is strongly recommended to thoroughly test the script in a test or lower environment before deploying or executing it in a production environment.
I hope you find this post useful and that it helps streamline your backup cleanup activities!!
Let me know for any questions and any further information in comments or LinkedIn.

Leave a comment