DigitalOcean snapshots are a blessing if you’re clumsy like me. They’ve allowed to me to recover from my mistakes, and even a hacking situation.
However, I’ve been disappointed with one aspect of DigitalOcean. Their backup plans for Droplet only create one backup per week and you cannot schedule yourself. They have snapshots which can be created ad hoc through their dashboard, but that’s not a way to live life.
I discovered DigitalOcean has it’s own command line interface (CLI) called DOCTL which allows you to access your DigitalOcean account and droplets remotely on a Linux machine.
After learning about this, I immediately wanted to leverage this with the following goals:
- Shutdown the server
- Take a snapshot as that is safer and reduces the chance of corrupted files
- Reboot the server
- Once the server is back on and live, delete the oldest snapshot if there are more than a certain amount.
This would keep my server lean and have a two backups a week for a maximum of 4 weeks if you consider their backup plan.
Table of Contents
Introducing DOCTL Remote Snapshot
The DOCTL Remote Snapshot script I’ve created is among several firsts for me:
- Learning Git and using Github
- Using DOCTL
- Publishing and maintain a public repository
I’m proud of this script, and I’ll be continuing to improve upon it. With no further ado:
Installation and Authentication
Install DOCTL on a separate Linux installation
Since our script will require us to shut down the droplet to prevent any corruption with our DigitalOcean snapshots, we’ll need a separate machine to make the remote calls and schedule the script via cron. If you run this on the same Droplet, it will shut itself off and that’s it. That is because with the server off, the rest of the script cannot be executed.
As an exmaple, I have a separate Ubuntu Virtual Machine (VM) running on my FreeNAS server that I setup specifically for cronjobs to execute scripts for remote services such as this script.
If you’re installing a fresh Ubuntu 18.04 LTS Server install, you can opt to have DOCTL installed alongside the server from the get-go. Otherwise, you’ll need to follow the GitHub documentation to install it. There is also this super awesome community-written guide.
Obtain the DigitalOcean API Key
This is the first step as it will be required to connect your script with your DigitalOcean account.
- In your dashboard in DigitalOcean, visit the API page: https://cloud.digitalocean.com/settings/api/tokens?i=74b08a
- Generate new token
- Enter the name
- Copy the new token
Authenticate Your Account
On your remote server, run the following command:
sudo doctl auth init
Then you’ll be prompted to enter your key from the first step. Once that is complete, you’re ready to use DOCTL on your server.
The DOCTL Remote Snapshot Script
Next, you’ll want to run the following command in the directory where you’d like this script to be executed.
git clone https://github.com/aaronmweiss/DOCTL-Remote-Snapshot.git
This will extract the GitHub repository to a directory titled DOCTL-Remote-Snapshot.
Configure your Snapshot
You’ll then want to edit the dodroplet.config file and supply the following:
Variable Name
Variable Explaination
dropletid=
numretain
recipient_email
snap_name_append
With the configuration file ready to go, you’re now ready to remotely execute DigitalOcean snapshots.
How to Execute the Script
Now it’s time to the run the script. To be the on the safe side, let’s be sure the script can be executed:
sudo chmod +x auto_snapshot.bash
Now it’s time to run the script:
sudo bash auto_snapshot.bash
Give this some time to run. Once the droplet is powered off, it will create the snapshot which is the longest part of the process. According to DigitalOcean “Snapshots can take up to 1 minute per GB of data used by your Droplet.” Although, I’ve found it might take much longer.
Once the snapshot is complete, the droplet is powered on again. The any snapshots beyond the value in numretain
will be deleted. You can use the -r
flag to bypass any snapshot deletions.
After this is complete, a notification is sent to the user’s email supplied in the dodroplet.config file.
Cronjob
This works best as a cronjob. You can do this by running sudo crontab -e and entering something similar to:
* 1 * * 3 /bin/bash /home/$user/autoscripts/auto_snapshot.bash
Where $user
is your username on your Linux machine. Although, you might want to use this in another location on your Linux installation.
You can use Crontab Generator to generate the cronjob command for you.
Conclusion
That’s it. This is my first published Bash script and GitHub repository. I’m extremely proud of this script, although it’s rather simple. It accomplishes a need that I had that was not readily available elsewhere.
It is my hope that you’re able to use this to automate your DigitalOcean snapshots and ensure your droplets are safe so you can continue to build up on your projects. Feel free to fork it, contribute, and comment on Github.
This article was updated on March, 10th, 2020