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:
- Call the DigitalOcean (DO) API to create a snapshot from a volume; and
- Rotate the volume snapshot by removing the obsolete one(s).
A detailed walk-through is available here.
Get started in three simple steps:
- Download a copy of the app;
- Create the environment variables for authentication and modify the crontab if needed; and
- Docker Compose or build and run the image manually to start the app, or alternatively run the Python script as a standalone service.
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/
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
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
Packaged as a container, the app is a standalone, executable package that may be run on Docker Engine. Be sure to have Docker installed.
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
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
Alternatively the do_api_volume_snapshot.py
script may be deployed as a standalone service.
In which case be sure to install the following required libraries for the do_api_volume_snapshot.py
:
Install them with pip3
:
$ pip3 install requests python-dotenv
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 -
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