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.
Prerequisites
Section titled “Prerequisites”- Switch over to where you have installed the STACKIT CLI
- First use the cli to Authenticate to the API and set the project ID
$ stackit auth loginSuccessfully logged into STACKIT CLI.$ stackit config set --project-id xxx$ PROJECTID=xxxManage your snapshots via API
Section titled “Manage your snapshots via API”Create a new snapshot from a volume
Section titled “Create a new snapshot from a volume”To start the snapshot creation, you first need to get the ID of the volume.
stackit volume listThis will show you a list with all volumes of your project.
| ID | NAME | STATUS | SERVER | AVAILABILITY ZONE | SIZE (GB) |
|---|---|---|---|---|---|
| 7c41a663-14f8-499e-a1bd-17c0e5986de8 | myvolume | AVAILABLE | eu01-1 | 20 | |
| fef8c62d-9d76-4901-955d-4c0aed9cfec9 | AVAILABLE | eu01-1 | 64 |
Currently the snapshot API is not available in the CLI so we have to use the curl subcommand in the cli.
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.
stackit curl https://iaas.api.eu01.stackit.cloud/v1/projects/$PROJECTID/snapshotsNow your snapshot is active.
Delete existing snapshot
Section titled “Delete existing snapshot”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.
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.
stackit curl -X DELETE https://iaas.api.eu01.stackit.cloud/v1/projects/$PROJECTID/snapshots/cab24e9d-a489-4e30-a8a0-38aad761f5f9This 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).
Creation of a Backup via API
Section titled “Creation of a Backup via API”To start the backup creation you first need to get the ID of the volume.
stackit volume listThis 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.
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:
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.
Deletion of Backups via API
Section titled “Deletion of Backups via API”To delete a Backup you need to get it’s ID.
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.
stackit curl -X DELETE https://iaas.api.eu01.stackit.cloud/v1/projects/$PROJECTID/backups/3cc18bef-1426-4cf0-9b28-063d564f49eaThis 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 herePROJECTID="xxx"
### Replace the volume-id with your own### you can enter multiple volume-ids here seperated by spaceVOLUMES=("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\"}}'doneAfter you created a bash script to create a backup you need add it to your cronjobs, to do so open the crontab.
crontab -eNow 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)
0 0 * * * ~/backupvolume.sh #replace this path with the path to your scriptWith this line you will schedule the backup every midnight.
Restore a Volume
Section titled “Restore a Volume”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.
$ stackit volume create --availability-zone eu01-1 --source-type snapshot --source-id xxxx --name restored-volumeCreating volume ✓Created volume for project "demo".Volume ID: cb4d5b6c-5281-40c4-9297-57400c1995a0After the Volume is created you can start with the recovery.
Now you can attach the volume to your VMs.
stackit server volume attach cb4d5b6c-5281-40c4-9297-57400c1995a0 --server-id <Server-ID>Finally, you can detach the old volume and delete it.