ffuentes

Person who happens to code and also loves music and books

Script for snapshots on Vultr with the API v2

06 Sep 2025 » network

Just wanna share a short script for snapshots with sh and cron in my Vultr instance. Long ago I used to create snapshots for my VPS on Vultr using a script created by Thedro Neely so this is just an update of his script made to be used with the API v2:

You also need your token (the $key) to use and you’ll get it from “Account” -> “API” in the Vultr account.

You need the unique identifier of the instance you wanna create an instance from. You’ll get that from the Vultr account in Products. There’s no option to grab it. Instead you need to click on your instance and grab the identifier from the URL (my.vultr.com/subs/?id=some-thing-like-this)

Key={your key goes here}
SnapshotLimit=2
SnapshotCount=$(curl -s -H "Authorization: Bearer $Key" https://api.vultr.com/v2/snapshots | jq '.meta.total'  )
echo $SnapshotCount;
LastSnapshotID=$(curl -s -H "Authorization: Bearer $Key" https://api.vultr.com/v2/snapshots | jq '.snapshots.[0].id')
LastSnapshotID=$(echo "$LastSnapshotID" | tr -d '"')
echo $LastSnapshotID;
DT=$(date '+%d/%m/%Y');

if [ "$SnapshotCount" -ge "$SnapshotLimit" ]
          then
                  curl -X DELETE -H "Authorization: Bearer $Key" "https://api.vultr.com/v2/snapshots/$LastSnapshotID" || exit 1;
fi

curl -X POST -H "Authorization: Bearer $Key" https://api.vultr.com/v2/snapshots --data '{
    "instance_id" : "The UUID identifier of your instance",
        "description" : "A description with a date:  '$DT'"
          }';

Then just add it to your daily crontab and it’ll create your snapshot and delete the oldest one.

Related Posts