How to automate volume snapshots on Digital Ocean & save money
This isn’t going to be a super long story — just a quick and useful guide for automating volume snapshots on DigitalOcean.
The Problem
Unlike some other cloud providers, DigitalOcean doesn’t offer a built-in feature for automatically scheduling snapshots of your volumes. For those unfamiliar with the terminology or for AWS enthusiasts:
- Volumes are network-based block storage devices that provide extra space for your Droplets. Think of them as detachable hard drives for your virtual machines. You can resize or move them between Droplets as needed.
- Droplets are DigitalOcean’s virtual machines, analogous to EC2 instances on AWS. Volumes are typically used when your primary storage doesn’t suffice, for example, when storing profile images, videos, or other large files.
Why No Automated Snapshots?
I’m guessing that DigitalOcean leaves snapshot scheduling out of its core feature set because the need varies greatly. You might need snapshots hourly, daily, or not at all. It’s your call.
While manual snapshots are always an option, relying on human intervention is far from ideal. There are third-party services that can automate the process, but they come with trade-offs:
- Starting at $19/month
- Limited to daily snapshots for up to three resources
- And a few other nice-to-have features
But why pay for something when you can automate it yourself for free?
The DIY Solution
DigitalOcean provides a robust set of RESTful APIs for managing block storage, which allows you to:
- List all your volumes
- List snapshots for each volume
- Create snapshots on demand
- Delete old snapshots to control storage costs
By leveraging these APIs, I built a simple Node.js cron job that:
- Creates a snapshot of a volume every hour
- Deletes excess snapshots to prevent unnecessary costs (because every snapshot adds up)
Example Code
The full source code for this solution is available here: https://github.com/kumarldh/do-volume-backup-job
It’s straightforward to set up and comes with minimal dependencies. The job does exactly what you need:
- Hourly snapshot creation
- Snapshot cleanup to manage expenses
Licensing
The code is free to use with just a couple of conditions:
- Feel free to modify and use it however you want
- Just credit me and please, don’t sue me
Final Thoughts
Automating DigitalOcean snapshots can save time, reduce risks, and avoid unnecessary expenses. Give it a try — and if it works well for you, I’d love to hear your feedback or suggestions for improvement!
Happy coding!