How to Take a Scheduled Weekly Volumes Snapshots

Preface

This guide describe how to take a scheduled weekly volumes snapshots from all or some volumes on STC Jeddah cloud and retaining only N weeks. The following steps was tested on ubuntu 16.04 server that has openstack clients installed.

ssh to the server

Install Openstack clients

sudo apt-get update && sudo apt-get install python-dev python-pip -y
sudo pip install python-openstackclient
mkdir /home/ubuntu/volumes-snapshots
cd /home/ubuntu/volumes-snapshots

Createopenrc.shfile using your favorite editor

nano openrc.sh

Copy the following text to the file and change username, password and project name to yours, then save and exit

export OS_USERNAME='username'
export OS_PASSWORD='password'
export OS_TENANT_NAME='project name'
export OS_AUTH_URL=https://api-jed1-vdc.bluvalt.com/identity/v3
export OS_IDENTITY_API_VERSION=3
export OS_USER_DOMAIN_NAME=jed1
export OS_PROJECT_DOMAIN_NAME=jed1
export OS_VOLUME_API_VERSION=2

List all volumes IDs on the project and save it to volumes-IDs file.

source openrc.sh 
openstack volume list |awk -F '|' '{print $2}'|grep -v ID|grep -v -e '^$'>/home/ubuntu/volumes-snapshots/volumes-IDs

You can exclude any volumes from this list by removing its ID from volumes-IDs file.

Create a file named snapshots-creation. sh

nano snapshots-creation.sh 

Insert the following script to the file then save and exit**

#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export OS_USERNAME='username'
export OS_PASSWORD='password'
export OS_TENANT_NAME='project name'
export OS_AUTH_URL=https://api-jed1-vdc.bluvalt.com/identity/v3
export OS_IDENTITY_API_VERSION=3
export OS_USER_DOMAIN_NAME=jed1
export OS_PROJECT_DOMAIN_NAME=jed1
export OS_VOLUME_API_VERSION=2
for i in $(cat /home/ubuntu/volumes-snapshots/volumes-IDs); do openstack volume snapshot create --force --volume $i `date +%F`-$i-snapshot |grep -m1 id|awk -F '|' '{print $3}'|grep -v -e '^$'; sleep 20s; done >>/home/ubuntu/volumes-snapshots/`date +%F`-snapshots

Create old snapshots cleaning script that will remove any older than N weeks snapshots which was created by first script (snapshots-creation .sh), change username, password and project name to yours, then save it as old-snapshots-cleaning .sh file.

nano old-snapshots-cleaning.sh 
#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export OS_USERNAME='username'
export OS_PASSWORD='password'
export OS_TENANT_NAME='project name'
export OS_AUTH_URL=https://api-jed1-vdc.bluvalt.com/identity/v3
export OS_IDENTITY_API_VERSION=3
export OS_USER_DOMAIN_NAME=jed1
export OS_PROJECT_DOMAIN_NAME=jed1
export OS_VOLUME_API_VERSION=2
for i in $(cat /home/ubuntu/volumes-snapshots/$(date -d 'now -N weeks' +%F)-snapshots); do openstack volume snapshot delete $i; sleep 10s; done

Note Replace N in ‘now -N weeks’ in last command with number of weekss you need to retain the created snapshots.THIS SCRIPT ONLY WORKS WITH WEEKLY SNAPSHOTS.

Cronjob

Add a crontab job to execute the scripts at your convenient time Please don’t forget to calculate the needed quota and request to increase storage quota if needed.

chmod a+x /home/ubuntu/volumes-snapshots/snapshots-creation.sh
chmod a+x /home/ubuntu/volumes-snapshots/old-snapshots-cleaning.sh
sudo crontab -e
30 01 * * 5  /home/ubuntu/volumes-snapshots/snapshots-creation.sh
30 18 * * 5 /home/ubuntu/volumes-snapshots/old-snapshots-cleaning.sh