Report this

What is the reason for this report?

How Can I Schedule Automatic Snapshots for My Droplets Using doctl?

Posted on January 10, 2025

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!

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.
0

Accepted Answer

Hey! 👋

DigitalOcean offers automated backups which might be enough for your use-case:

https://www.digitalocean.com/products/backups

For more advanced control you could also look into the SnapShooter service:

https://docs.digitalocean.com/products/snapshooter/

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.