Skip to content

Latest commit

 

History

History
executable file
·
153 lines (99 loc) · 4.72 KB

README.md

File metadata and controls

executable file
·
153 lines (99 loc) · 4.72 KB

DigitalOcean API: Create and Rotate Volume Snapshot

This Python app is containerised with Docker Compose for a modular and cloud native deployment that fits in any microservice architecture.

It does the following:

  1. Call the DigitalOcean (DO) API to create a snapshot from a volume; and
  2. Rotate the volume snapshot by removing the obsolete one(s).

A detailed walk-through is available here.

Table of Content

Getting Started

Get started in three simple steps:

  1. Download a copy of the app;
  2. Create the environment variables for authentication and modify the crontab if needed; and
  3. Docker Compose or build and run the image manually to start the app, or alternatively run the Python script as a standalone service.

Git Clone

Download a copy of the app with git clone. Be sure to pass the --recurse-submodules argument to initialise and update each submodule in the repository.

$ git clone --recurse-submodules https://github.com/kurtcms/do-api-volume-snapshot /app/do-api-volume-snapshot/

Environment Variables

The app expects the base URL, the API token, the DO volume ID and the number of volume snapshot to keep, as environment variables in a .env file in the same directory.

Be sure to create the .env file.

$ nano /app/do-api-volume-snapshot/.env

And define the variables accordingly.

DO_BASE_URL = 'https://api.digitalocean.com/v2'
DO_TOKEN = '(redacted)'

# ID of the volume
DO_VOLUME_ID = '(redacted)'

# Number of volume snapshot to keep
DO_SNAPSHOT = 1

Crontab

By default the app is scheduled with cron to create and rotate the volume snapshot everyday, with stdout and stderr redirected to the main process for Docker logs.

Modify the crontab if a different schedule is required.

$ nano /app/do-api-volume-snapshot/crontab

Docker Container

Packaged as a container, the app is a standalone, executable package that may be run on Docker Engine. Be sure to have Docker installed.

Docker Compose

With Docker Compose, the app may be provisioned with a single command.

Install Docker and Docker Compose with the Bash script that comes with app.

$ chmod +x /app/do-api-volume-snapshot/docker-compose/docker-compose.sh \
    && /app/do-api-volume-snapshot/docker-compose/docker-compose.sh

Start the containers with Docker Compose.

$ docker-compose -f /app/do-api-volume-snapshot/docker-compose.yml up -d

Stopping the containers is as simple as a single command.

$ docker-compose -f /app/do-api-volume-snapshot/docker-compose.yml down

Build and Run

Otherwise the Docker image can also be built manually.

$ docker build -t do_api_volume_snapshot /app/do-api-volume-snapshot/

Run the image with Docker once it is ready.

$ docker run -it --rm --name do_api_volume_snapshot do_api_volume_snapshot

Standalone Python Script

Alternatively the do_api_volume_snapshot.py script may be deployed as a standalone service.

Dependencies

In which case be sure to install the following required libraries for the do_api_volume_snapshot.py:

  1. Requests
  2. Python-dotenv

Install them with pip3:

$ pip3 install requests python-dotenv

Cron

The script may then be executed with a task scheduler such as cron that runs it everyday for example.

$ (crontab -l; echo "0 0 * * * /usr/bin/python3 /app/do-api-volume-snapshot/do_api_volume_snapshot.py") | crontab -

Docker Logs

A message will be printed on the creation and the deletion of volume snapshot.

Snapshot 2022-10-21-00-00-02 is created at 2022-10-21T00:00:02Z
Snapshot 2022-10-20-00-00-02 is removed at 2022-10-21T00:00:06Z

Reference