As I’ve been learning more about running a web server, Linux, and Bash scripting, I’ve been looking for all sort of ways to automate my digital life. As of late, I’ve been disappointed with Digital Ocean‘s backup plans for Droplets, which is just a backup once per week, of which, you cannot schedule yourself. They have snapshots which can be created ad hoc, but that’s not a way to live life.
I found out that Digital Ocean has it’s own command line interface (CLI) which allows you to access your Digital Ocean account and droplets remotely. After learning about this, I immediately wanted to leverage this with the following goals:
- Clean up unnecessary files on the server to reduce snapshot size. Digital Ocean charges for space used for snapshots.
- Shutdown the server
- Take a snapshot. Taking a snapshot when the server is off is safer and reduces the chance of corrupt files.
- Reboot the server
- Once the server is back on, delete the oldest snapshot if there are more than 10. This counts Digital Ocean’s backups as snapshots too.
This would keep my server lean and have a two backups a week for a maximum of 5 weeks if you consider their backup plan. The script I’ve created doesn’t perform #1, as I have a separate script that performs that duty and I believe that individuals should choose how to keep their server maintained and lean.
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 maintaining a public repository
I’m proud of this script, although I know there’s more that can be done to improve 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 Digital Ocean droplet, 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.
As an exmaple, I have a separate Ubuntu Virtual Machine 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: https://github.com/digitalocean/doctl/blob/master/README.md. There is also this super awesome community-written guide: https://www.digitalocean.com/community/tutorials/how-to-use-doctl-the-official-digitalocean-command-line-client
Obtain the Digital Ocean API Key
This is the first step as it will be required to connect your script with your Digital Ocean account.
- In your dashboard in Digital Ocean, 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.
The DOCTL Remote Snapshot Script
Next, you’ll want to run the following command:
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:
#Append an additional note at end of snapshot name. Currently, it’s set to “_cron_snapshot”
Config file explaination
DROPLETID is your Droplet’s ID. If you do not know your droplet’s ID, log into your Digital Ocean account , click on the droplet, and the URL of your droplet will contain your Droplet’s ID after the /droplets/ directory, like so: https://cloud.digitalocean.com/droplets/XXXXXXXXX/graphs?i=78109b&period=hour. The “XXXXXXXXX” in the URL string is your droplet’s ID.
RECIPIENTEMAIL is your email address.
DROPLETNAME is your own name for your droplet. You can assign any value to this. It will appear at the beginning of your snapshot’s name and is for organizational purposes only.
SNAPNAMEAPPEND allows you to append additional descriptors to the name of your snapshots. I prefer to inculde “_cron_snapshot” so that I know snapshots from this process came from my cronjob.
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 begin snapshot which is the longest part of the process. According to Digital Ocean “Snapshots can take up to 1 minute per GB of data used by your Droplet.” Once the snapshot is complete, the droplet is powered on again. The last 10 images will be kept, and other wills be deleted.
Please note that is images and not just snapshots. Images include both snapshots and backups from the entire account, not just the droplet. I do have plans in the future to have it only delete from a separate droplet and allow the user to identify how many images to keep.
After this is complete, a notification is sent to the user’s email supplied in the config file.
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.
You can use Crontab Generator to generate the cronjob command for you.
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. Feel free to Fork it, contribute, and comment on Github.