Question

Can I use the API to create a snapshot of a droplet then add that snapshot to another region ?

Posted January 8, 2020 694 views
API

Hey All,

I want to create a script that will create a snapshot of a droplet, then add that snapshot to another region for redundancy. Is is possible to do this through the DO API?

Thanks

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Yes. The DigitalOcean API supports both taking a snapshot of a Droplet and transferring a copy of it to additional regions.

I’ve linked to the relevant API docs above, but here’s a quick example using doctl, the DigitalOcean CLI:

SNAPSHOT_NAME="snapshot-$(date +%s)"

doctl compute droplet-action snapshot $DROPLET_ID \
  --snapshot-name $SNAPSHOT_NAME \
  --wait

IMAGE_ID=`doctl compute image list-user --no-header --format Name,ID \
 | grep $SNAPSHOT_NAME \
 | tr -s ' ' \
 | cut -d ' ' -f2`

doctl compute image-action transfer $IMAGE_ID \
  --region $NEW_REGION
  1. The first command creates the snapshot of the Droplet. It sets its name to include the unix timestamp of when it was created. Note the use of the --wait flag. This blocks until the snapshot has been completed.
  2. The next command finds the ID of the newly created image and stores it in a variable for use latter. Explaining the bash a bit: First it finds the image with the name we gave it. Next trims the white space. Finally it grabs the image ID.
  3. The final command transfers the image to a new region. You can also use the --wait flag here if you want the script to block until the transfer is complete.
Submit an Answer