Backups and Snapshots
Digital Ocean provides us with the ability to set up a backup of our droplets, but it can only be done weekly via their site. Handily they also enable us to create snapshots of a droplet at any time we want. This guide will explain how to use the Digital Ocean API to create droplet snapshots.
Setting up snapshots with the Digital Ocean API
First we will need a security token. So go to the “API” section and click “Generate New Token”, if you have not done so already. Now you’ve got a token, lets start coding!
Be aware that we’ll need to power off the droplet in order to create our snapshot. Make sure that your server will start everything that you need after each restart.
Additionally we’ll need to get a list of existing snapshots, check their dates and delete any old snapshots.
Look at how this can be done in the code below. We use CURL, but of course you can use any library suitable to your needs.
Then you just need to set up a cron job that will run this script.
Example for crontab: 0 0 * * * /usr/bin/php /<path to script>/backup_droplet.php