Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

First, it is worth reminding that it it advisable that the creation of snapshots and backups is done in a different machine that the one backing up. Consequently, the cron  job should also be run in a different machine. To run a cron  job, ensure that cron is installed in the machine, e.g.


Code Block
 sudo apt update && sudo apt install cron 


in Ubuntu distributions, and start the cron services with

...

.Note that this job will necessarily fail, since the cron service does not use the environment varriables variables with the OpenStack credentials that are needed to use the OpenStack CLI. Hence, a better solution is to write a shell script that will load the appropriate environment variables and run any operations. Thus, the same example with the correct OS credentials will look like

Code Block
0 12 * * Sat run_backup_job.sh 2>&1 | logger -t backups

which will run with the script run_backup_job.sh  and log all the output. The script run_backup_job.sh is then

Code Block
#! /usr/bin/env bash

for var in $(env | sed -n 's/^\(OS.*\)=.*/\1/p'); do unset "$var"; done
export OS_AUTH_URL=https://keystone.cloudferro.com:5000/v3
export OS_INTERFACE=public
export OS_IDENTITY_API_VERSION=3
export OS_USERNAME="<username>"
export OS_REGION_NAME="WAW3-1"
export OS_PROJECT_ID="<project_id>"
export OS_PROJECT_NAME="<project_name>"
export OS_PROJECT_DOMAIN_ID="<domain_id>"
export OS_PASSWORD="<password>"
export OS_CLIENT_ID=openstack
export OS_CLIENT_SECRET=08eb53c3-3cac-4a01-8406-d72f6a70948b
export OS_PROTOCOL=openid
export OS_IDENTITY_PROVIDER=eumetsat_provider
export OS_AUTH_TYPE=v3oidcpassword
export OS_DISCOVERY_ENDPOINT="https://identity.cloudferro.com/auth/realms/Eumetsat-elasticity/.well-known/openid-configuration"

echo "Stopping server"
openstack server stop <server> TestVM
echo "Backing up server"
sleep 5
openstack server image create --name <image_name> <server>TestVM_snapshot --wait TestVM
echo "Restarting server"
openstack server start <server> TestVM

which, in addition to creating the snapshot, will stop the server beforehand, and then restart it, waiting for the server to be stopped and image to be created in between.

To run the backups only once, without repetition, it is possible to set up a conditional statement on the script, such as

Code Block
...
<set up env>
...

COMMAND=run_backup_job.sh
DONE="${COMMAND}.alreadyrun"
   
if [[ -f $DONE ]]; then
  exit 1
fi
touch "$DONE"

...
<run some commands>
...

Alternatively, the same result can be obtained using the at command instead of cron , by running

Code Block
./run_backup_job.sh | at <some_future_time>

Lastly, when scheduling backups, it is recommended to set up a retention limit, so that no more than a certain number of backups are kept at a given time. One can incorporate this check, and the corresponding deletion of older backups, in the cron  script, with

Code Block
...
<do some backup>
...

retention_count=5
backups=(`openstack image list --property image_type=snapshot --property instance_uuid=<instance_id> --sort updated_at -c ID -f value`)
nbackups=${#backups[@]}
echo "Found $nbackups backups of instance"
if [[ $nbackups -gt $retention_count ]]
then
    let "todelete=$nbackups-$retention_count"
    echo "Backup number over the retention count, will delete the oldest $todelete backups"
    for ((i=0;i<todelete;i++)); do
        echo "Deleting image ${backups[$i]}"
        openstack image delete ${backups[$i]}
    done;
else
	echo "Backup number equal or under retention count, doing nothing"
fi

Note: some of the syntax above is specific to bash . Add SHELL=/usr/bin/env bash to your crontab  to ensure cron runs using bash , or change the above script for its equivalent in sh .WIP

Backup automation
Anchor
BackupAutomation
BackupAutomation

...