I’m looking for a way to automate snapshots of my Droplets using doctl
. I want to make sure I have regular backups without manually creating snapshots every time.
Is there a way to schedule automatic snapshots with doctl
or through a simple script? Also, how can I manage old snapshots to avoid unnecessary storage costs?
Any guidance or examples would be much appreciated!
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Accepted Answer
Hey! 👋
DigitalOcean offers automated backups which might be enough for your use-case:
For more advanced control you could also look into the SnapShooter service:
If you explicitly want to create snapshots yourself, you can indeed use the doctl
CLI tool!
First, run this command to manually create a snapshot for a Droplet:
doctl compute droplet-action snapshot DROPLET_ID --snapshot-name "auto-backup-$(date +\%Y-\%m-\%d)"
This will create a snapshot with the current date in the name for easy tracking.
For more information, you can check the official documentation:
https://docs.digitalocean.com/reference/doctl/reference/compute/droplet-action/snapshot/
To automate this, create a cron job to run the command daily. Open the crontab editor:
crontab -e
Then, add this line to run the snapshot command every day at midnight:
0 0 * * * doctl compute droplet-action snapshot DROPLET_ID --snapshot-name "auto-backup-$(date +\%Y-\%m-\%d)"
Make sure your doctl
is authenticated (doctl auth init
) and that your environment variables are properly set if using in scripts.
To avoid storage bloat, you can delete snapshots older than, say, 7 days. Here’s a quick script for that:
#!/bin/bash
RETENTION_DAYS=7
SNAPSHOTS=$(doctl compute snapshot list --output json | jq -r '.[] | select(.name | test("auto-backup-")) | "\(.id) \(.created_at)"')
while read -r ID CREATED; do
SNAPSHOT_DATE=$(date -d "$CREATED" +%s)
CUTOFF_DATE=$(date -d "$RETENTION_DAYS days ago" +%s)
if [ "$SNAPSHOT_DATE" -lt "$CUTOFF_DATE" ]; then
doctl compute snapshot delete "$ID" --force
echo "Deleted snapshot $ID created on $CREATED"
fi
done <<< "$SNAPSHOTS"
You can check out the documentation for doctl compute snapshot list
and doctl compute snapshot delete
for more information:
https://docs.digitalocean.com/reference/doctl/reference/compute/snapshot/list/
You can set this script on a weekly cron to clean up old snapshots!
Hope this helps!
- Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.