Overview

Digital Ocean is a fantastic hosting provider for simple SSD cloud servers built with developers in mind. Since I develop pretty much everything in Python these days, I like to keep as much of my automation and other tasks in python as well. Recently, I found a nice little python wrapper for the Digital Ocean API that makes communicating with your droplets super easy.

The documentation is lacking, although it seems to follow the syntax of the official Digital Ocean API quite well. You can do many, many different things through the API such as creating/destroying droplets, re-sizing droplets, surveying images, etc. The task I use it for most often is automating snapshots. A snapshot can be used as a backup, or for spinning up an identical server elsewhere. For information on the difference between backups and snapshots, check out this article.

Automate Snapshots through the Digital Ocean API

  1. Install the python-digitalocean library

    pip install -U python-digitalocean
    
  2. Get API and Client Keys

    This can be found in the developer section of your Digital Ocean settings. Simply click Generate New Key

  3. Get Droplet ID

    I am really not sure of the easiest way to find the droplet ID; however, I usually grab it through the API as you will see below

  4. Take Snapshot

    4.1. Define Constants:

    api_key = 'some_key'
    client_id = 'some_client_id'
    droplet_id = 'some_id'
    

    4.2: Import Some Things

    import digitalocean
    

    4.3: Create Instance of Droplet Class and Load

    droplet = digitalocean.Droplet(id = droplet_id, client_id = client_id, api_key = api_key)
    droplet.load()
    

    4.4: Power off the droplet

    droplet.power_off()
    

    4.5: Take a Snapshot!

    droplet.take_snapshot("Name of your Snapshot!")
    

    4.6: Power the Droplet back on

    droplet.power_on()
    

Tada! You should now be able to successfully integrate this into a script to automate your snapshot-ing!