Skip to main content

CoBRA - Comprehensive Backing up and Restoration Archiver

Cobra is a tool for creating, managing and restoring backups. It is designed to cover docker powered applications as well as it allows backing up of regular file system folders.

Cobra cli

How to use

pip install cobra-archiver

CLI

After that cobra command will be available from the command line.

To get the cli description please issue cobra --help or e.g. cobra backup --help to get help on certain command.

This will backup all the docker volumes as well as /want/this/dir/backed/up directory, but skip-this-volume and-this-one.

cobra backup build --push --dir /want/this/dir/backed/up \
    --creds /path/to/google-service-acc-key.json --folder-id google-drive-folder-id \
    --exclude skip-this-volume and-this-one

This restores latest backup from the given remote folder.

cobra backup pull --latest --restore \
    --creds /path/to/google-service-acc-key.json --folder-id google-drive-folder-id

Remote storage

For now Google Drive only supported. If you find this project useful you can contribute to enhance it. Or at least you can post a feature request.

  1. To have this work the Google Service Account is necessary. The service account id (email) looks like <the-name-you-choose>@hip-heading-376120.iam.gserviceaccount.com.
  2. Under the service account you've created add the key pair and download it in .json format.
  3. Now create the folder within your Google Drive you wish to push the backups in.
  4. Share this folder with the service account (email) from step 1.

Hooks

They are listed below.

HOOKS = ('before_build', 'after_build', 'before_push', 'after_push', 
         'before_pull', 'after_pull', 'before_restore', 'after_restore')

One can either issue cobra hooks init that populates hook files to the default directory. Or put the hook files with the names e.g. before_build.py or before_build.sh. For shell script chmod +x before_build.sh is necessary.

Cobra searches for .py file first if found imports it and execute hook function as.

hook(hook_name=hook_name, hooks_dir=hooks_dir, backup_dir=backup_dir, 
     filename=backup_filename, docker=docker_client)
  • hook_name is the one from the list above
  • hooks_dir the directory where hooks reside
  • backup_dir the local backup directory where backup is stored
  • filename the backup file name
  • docker DockerClient object

If .py file is not found. The default hook is called that continue searching for .sh file. If latter found it's called via subprocess.check_call(). With the same params except docker.

By default cobra copies and packs the content of a volume. To backup database with tools like mongodump or pg_dump one may use before_build hook and --exclude volume-name from the processing. before_build hook may look like this in such a case.

#!/usr/bin/env bash

# Stop any containers that mangle database while dumping to have consistent dump
docker stop my-excellent-app

MONGO_DUMP_DIR=/tmp/mongodump
mkdir -p "${MONGO_DUMP_DIR}"
mongodump --archive="${MONGO_DUMP_DIR}/mongo-dump-by-hook.tar.gz" --db=test --gzip mongodb://mongo-container-name:27017

# Then start them again
docker start my-excellent-app

Errors that are propagated from hooks stop farther processing. To see more details please inspect e2e test sources.

Default locations

To find out paths used by cobra one can issue following. On my system I have this output.

$ cobra dirs
/home/q/.local/share/cobra/backup
/home/q/.cache/cobra
/home/q/.local/share/cobra/hooks

Python

Minimum python version is 3.7.

from cobra.api import Api
from cobra.hooks import Hooks
from docker import DockerClient

api = Api(gateway=DockerClient(), hooks=Hooks())
api.backup_build()

Method parameters are described in cli help cobra backup --help e.g.

Security notice

This code is subject to command injection vulnerabilty. There are no such a checks. The caller should provide all checks on his own.

Run tests

git clone https://github.com/yell0w4x/cobra.git
cd cobra
./run-tests --unit

The above runs unit tests. To execute end-to-end tests run is as follows. Note that docker must reside in the system. To install it on Ubuntu use wget -qO- https://get.docker.com | sudo bash. On Manjaro (Arch) issue sudo pacman -S docker.

./run-tests --e2e --folder-id goolge-drive-folder-id --key path/to/google-service-account-key.json

or

GOOGLE_DRIVE_FOLDER_ID=goolge-drive-folder-id GOOGLE_SERVICE_ACC_KEY=path/to/key.json ./run-tests --e2e

The tests are based on pytest. All the extra arguments are passed to pytest. E.g. to have verbose output use -v or -vv. To show stdout -s. To run certain tests use -k test_name and etc. For details see the pytest docs.

./run-tests --help
Run cobra unit and e2e tests.

Usage:
    ./run-tests [OPTIONS] [EXTRA_ARGS]

All the EXTRA_ARGS are passed to pytest

Options:
    --help                Show help message
    --unit                Run unit tests
    --e2e                 Run e2e tests
    --skip-build          Skip building dist files
    --folder-id FOLDER_ID Google drive folder id to use as remote storage for e2e tests. 
                          If not given read from GOOGLE_DRIVE_FOLDER_ID environment variable.
    --key KEY_FN          Path to google service account key file in json format
                          If not given read from GOOGLE_SERVICE_ACC_KEY environment variable.

Metadata

Release files for cobra-archiver 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cobra-archiver 0.0.5
File Size Uploaded Provenance Actions
cobra-archiver-0.0.5.tar.gz 25.2 kB Without provenance Details for cobra-archiver-0.0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cobra-archiver 0.0.5
File Interpreter ABI Platform Actions
cobra_archiver-0.0.5-py3-none-any.whl Python 3 none any Details for cobra_archiver-0.0.5-py3-none-any.whl

Total release size: 51.3 kB

Release files / cobra-archiver-0.0.5.tar.gz

Download URL cobra-archiver-0.0.5.tar.gz
Size 25.2 kB
Tags Source
SHA-256 checksum
How to use checksums
87d37212013583c80e778d2b6e357205fa13ca7ffc6bd18c6b642a54f98a2241
BLAKE2b-256 checksum
How to use checksums
ee13d4201cc2660e11bfbff687827061d1831087afe43d4360e7ef79c1b5fddc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/6.6.0 pkginfo/1.9.6 requests/2.30.0 requests-toolbelt/1.0.0 tqdm/4.65.0 CPython/3.10.9

Release files / cobra_archiver-0.0.5-py3-none-any.whl

Download URL cobra_archiver-0.0.5-py3-none-any.whl
Size 26.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b584139af6485fbd784a22223e56d9b8e495660ac33a53e00a9ef558d9ab05b7
BLAKE2b-256 checksum
How to use checksums
65044595b62ad05ce82f3bcf4edb9dbdfd5af68c418152ece8f7e49bf0bb8203
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/6.6.0 pkginfo/1.9.6 requests/2.30.0 requests-toolbelt/1.0.0 tqdm/4.65.0 CPython/3.10.9

Security for cobra-archiver 0.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page