• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Aaron Weiss

  • Home
  • Blog
  • About
  • Contact

bash

Automated DigitalOcean Snapshots with DOCTL

December 22, 2019 by Aaron Weiss

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:

  1. Shutdown the server
  2. Take a snapshot as that is safer and reduces the chance of corrupted files
  3. Reboot the server
  4. 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
  • Installation and Authentication
    • Install DOCTL on a separate Linux installation
    • Obtain the DigitalOcean API Key
    • Authenticate Your Account
  • The DOCTL Remote Snapshot Script
    • Configure your Snapshot
    • How to Execute the Script
    • Cronjob
  • Conclusion

Introducing DOCTL Remote Snapshot

The DOCTL Remote Snapshot script I’ve created is among several firsts for me:

  1. Learning Git and using Github
  2. Using DOCTL
  3. 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.

  1. In your dashboard in DigitalOcean, visit the API page: https://cloud.digitalocean.com/settings/api/tokens?i=74b08a
  2. Generate new token

    DigitalOcean Generate API Key
    Click here to enlarge
  3. Enter the name

    Name API Key
    Click here to enlarge
  4. Copy the new token

    DigitalOcean API Key
    Click here to enlarge

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=
Your Droplet’s ID. If you do not know your droplet’s ID, log into your DigitalOcean 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.
numretain
Enter the number of snapshots you’d like to keep as a positive integer.
recipient_email
The email address you would like to recieve completion notifications.
snap_name_append
Optionally add additional information to the end of the snapshot’s name.

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

Filed Under: Projects, Website Administration Tagged With: bash, Digital Ocean, digitalocean, DOCTL, git, github, linux, snapshots, web server

Primary Sidebar

Recent Posts

  • TrueNAS Virtual Machine with Ubuntu 18.04 Halting with Signal 11
  • Proxmox ZFS Disk Replacement and Drive Expansion
  • How To Create ZFS Backups in Proxmox
  • Multiple UPS on the same NUT-Server
  • Learning Graylog for Fun and Profit

Categories

  • Film & Television
  • Google Analytics
  • Guitar
  • Lifestyle
  • Projects
  • Proxmox
  • Search Engine Optimization
  • Technology
  • TrueNAS
  • Uncategorized
  • Website Administration
  • WordPress
  • Home
  • Blog
  • About Aaron Weiss
  • Contact
  • Privacy Policy
  • Affiliate Disclosure

© Aaron Weiss. Built with WordPress using the Genesis Framework. Hosted on a2 Hosting and DigitalOcean. Aaron Weiss's LinkedIn Profile