Zum Inhalt springen

How to create volume snapshots and backups via IaaS-API CLI

Diese Seite ist noch nicht in deiner Sprache verfügbar. Englische Seite aufrufen

This tutorial describe steps you need to create snapshot or backups of volumes.

  • Switch over to where you have installed the STACKIT CLI
  • First use the cli to Authenticate to the API and set the project ID
Terminal window
$ stackit auth login
Successfully logged into STACKIT CLI.
$ stackit config set --project-id xxx
$ PROJECTID=xxx

To start the snapshot creation, you first need to get the ID of the volume.

Terminal window
stackit volume list

This will show you a list with all volumes of your project.

IDNAMESTATUSSERVERAVAILABILITY ZONESIZE (GB)
7c41a663-14f8-499e-a1bd-17c0e5986de8myvolumeAVAILABLEeu01-120
fef8c62d-9d76-4901-955d-4c0aed9cfec9AVAILABLEeu01-164

Currently the snapshot API is not available in the CLI so we have to use the curl subcommand in the cli.

Terminal window
stackit curl -X POST --header "Content-Type: application/json" https://iaas.api.eu01.stackit.cloud/v1/projects/$PROJECTID/snapshots --data '{"name": "mysnapshot", "volumeId": "7c41a663-14f8-499e-a1bd-17c0e5986de8"}'

If everything was successful you can retrieve a list of available snapshots.

Terminal window
stackit curl https://iaas.api.eu01.stackit.cloud/v1/projects/$PROJECTID/snapshots

Now your snapshot is active.

If you have multiple snapshots of a volume you can delete each snapshot on its own.

To start the deletion of a snapshot you need to get the snapshot-id.

Terminal window
stackit curl https://iaas.api.eu01.stackit.cloud/v1/projects/$PROJECTID/snapshots
{
"items": [
{
"createdAt": "2024-12-09T13:35:47Z",
"id": "cab24e9d-a489-4e30-a8a0-38aad761f5f9",
"labels": {},
"name": "mysnapshot",
"size": 20,
"status": "AVAILABLE",
"updatedAt": "2024-12-09T13:35:47Z",
"volumeId": "7c41a663-14f8-499e-a1bd-17c0e5986de8"
}
]
}

This will return a list of all your snapshots, now copy the ID of the snapshot you want to delete.

With this ID you can now execute the deletion of the snapshot.

Terminal window
stackit curl -X DELETE https://iaas.api.eu01.stackit.cloud/v1/projects/$PROJECTID/snapshots/cab24e9d-a489-4e30-a8a0-38aad761f5f9

This will not return anything if the operation was successful (If you want to check that the snapshot is deleted, you can execute the command to list snapshots again).

To start the backup creation you first need to get the ID of the volume.

Terminal window
stackit volume list

This will show you a list with all volumes of your projects, now copy the volume-id of the volume you want to create a backup from.

Terminal window
stackit curl --header "Content-Type: application/json" -X POST https://iaas.api.eu01.stackit.cloud/v1/projects/$PROJECTID/backups --data '{"source": {"id": "7c41a663-14f8-499e-a1bd-17c0e5986de8", "type": "volume"}}'

For more information you can use:

Terminal window
stackit curl https://iaas.api.eu01.stackit.cloud/v1/projects/$PROJECTID/backups
{
"createdAt": "2024-12-09T14:11:51Z",
"id": "3cc18bef-1426-4cf0-9b28-063d564f49ea",
"labels": {},
"name": "",
"size": 20,
"status": "CREATING",
"updatedAt": "2024-12-09T14:11:51Z",
"volumeId": "7c41a663-14f8-499e-a1bd-17c0e5986de8"
}

After the backup is created the status changes to available.

To delete a Backup you need to get it’s ID.

Terminal window
stackit curl https://iaas.api.eu01.stackit.cloud/v1/projects/$PROJECTID/backups/

Now copy the ID of the backup you want to delete from the list.

With the ID you can now begin to delete the backup.

Terminal window
stackit curl -X DELETE https://iaas.api.eu01.stackit.cloud/v1/projects/$PROJECTID/backups/3cc18bef-1426-4cf0-9b28-063d564f49ea

This will not give any response if everything was completed successfully (you can use the command to list backups to check it).

Add a cron job to automate Backups (Linux)

Section titled “Add a cron job to automate Backups (Linux)”

We recommend using a service-account with a long-lived token for running automated API calls. This can easily achieved with the the stackit auth activate-service-account command.

The cron-deamon is service on linux systems to schedule the execution of scripts e.g. this backup creation. To get started you need to create a bashscript that creates the backup of your volumes. This can look like this:

Codeblock for publicly exported bash variables

#!/bin/bash
### This script triggers the creation of a volume backup
### Set the your Project ID here
PROJECTID="xxx"
### Replace the volume-id with your own
### you can enter multiple volume-ids here seperated by space
VOLUMES=("8643cd4d-023f-491c-843a-8xxxxxxxxxx5")
for volume in ${VOLUMES[@]}
do
stackit curl --header "Content-Type: application/json" -X POST https://iaas.api.eu01.stackit.cloud/v1/projects/$PROJECTID/backups --data "{\"source\": {\"id\": \"$volume\", \"type\": \"volume\"}}'
done

After you created a bash script to create a backup you need add it to your cronjobs, to do so open the crontab.

Terminal window
crontab -e

Now you need to add these lines at the end of the document (including the empty newline at the end) to start the schedule (if you dont now what numbers you need to enter, you can try it out here)

Terminal window
0 0 * * * ~/backupvolume.sh #replace this path with the path to your script

With this line you will schedule the backup every midnight.

To restore a volume you first need to create a new volume and provde the source-type of “snapshot” or “backup” with the corresponding id.

Terminal window
$ stackit volume create --availability-zone eu01-1 --source-type snapshot --source-id xxxx --name restored-volume
Creating volume
Created volume for project "demo".
Volume ID: cb4d5b6c-5281-40c4-9297-57400c1995a0

After the Volume is created you can start with the recovery.

Now you can attach the volume to your VMs.

Terminal window
stackit server volume attach cb4d5b6c-5281-40c4-9297-57400c1995a0 --server-id <Server-ID>

Finally, you can detach the old volume and delete it.