by @mcknco

Automatic VPS Backups Using Vultr API

TL;DR: Grab the PHP script.

Although my demands are very limited at the moment, up until now I’ve had a flawless experience hosting my VPS at Vultr. It’s an incredible mix of speed, reliability and a great price. Since I’m a total noob, I’m hosting everything I’m doing on a $2.5 droplet.

Snapshots

Vultr provides a snapshot function via their dashboard. A quick snapshot allows you to create a perfect backup of your instance’s filesystem while it’s still running, so there’s no need to interrupt any services which rely on your VPS.

But the official automatic backup system is a bit restrictive in its functionality — just two snapshots, either daily or weekly. So you don’t have a lot of control over the intervals, number of backups, etc. But luckily, they’ve provided us the tools to roll our own.

Enter Vultr API

Vultr has implemented a terrific simple little REST API for their entire dashboard. So pretty much anything you can do via the web interface you can also do programmatically, via the API. This allows us to write a little script that calls the API in order to create VPS snapshots according to our own schedule and needs.

For example, my script creates 3 daily, 3 weekly, and 3 monthly snapshots of my VPS instance for a total of 9 backups, allowing me a good range of histories for different use-cases: abrupt hardware catastrophe, undetected hacker intrusion, who knows.

How to use Vultr API

Obtain your API credentials via the dashboard. For security it’s a good idea to limit API calls to just your home IP and/or the IP of your Vultr droplet (since I use it to request snapshots of itself).

Vultr dashboard API screen

The API documentation is good, simple and self-explanatory.

The basic API call you’ll need is to server/list to get the subid (unique identifier) of the droplet. In my script I find the correct subid by comparing the returned list of droplet IP addresses against the IP reported on the droplet itself via hostname ‑I.

Once you’ve confirmed the droplet’s subid you can grab a list of current snapshots in your account using snapshot/list. The resulting list should then be filtered for existing snapshots of the relevant instance. In my case I rotate out the oldest snapshots using snapshot/destroy and then create a fresh new snapshot using snapshot/create. Very simple and all around awesome!

Snapshot heaven

At the time of writing (late 2017), Vultr has a very generous policy of 10 free snapshots. Creating and destroying snapshots using the API is also free of charge. So this actually works out cheaper than using the official automated backup system. And by cheap I mean free. More flexible and cost-free, what’s not to like?

The script — literally the first PHP I’ve ever written — is available on GitHub as a gist. Definitely send me your thoughts.

December 1, 2017, @mcknco

Want updates as I navigate my way through the emotional ups and downs of online solopreneurship? Get on my private email list. A few mails a year, max. No spam!

Have feedback?
Thanks for your message! We'll get back to you shortly.
A suggestion? Or a bug? Say hi!