Skip to main content

BorgAPI

A helpful wrapper for borgbackup to be able to easily use it in python scripts.

This is not supported use case by the borg developers. They only intend for it's use via a CLI. Keeping parity with borg is the main goal of this api.

Installation

pip install borgapi

Requires:

  • borgbackup: 1.1.16
  • python-dotenv: 0.17.1

Usage

import borgapi

api = borgapi.BorgAPI(defaults={}, options={})

# Initalize new repository
api.init("/foo/bar", make_parent_dirs=True)

# Create backup 
stdout, stderr = api.create("/foo/bar::backup", "/home", "/mnt/baz", json=True)
print(stdout["name"]) # backup
print(stdout["repository"]["location"]) # /foo/bar

BorgAPI Init arguments

class BorgAPI(
    defaults: dict = None,
    options: dict = None,
    log_level: str = "info",
    log_json: bool = False
)
  • defaults: dictionary that has command names as keys and value that is a dict of command specific optional arguments
{
    "init": {
        "encryption": "repokey-blake2",
        "make_parent_dirs": True,
    },
    "create": {
        "json": True,
    },
}
  • options: dictionary that contain the optional arguments (common, exclusion, filesystem, and archive) used for every command (when valid). Options that aren't valid for a command will get filterd out. For example, strip_components will be passed into the extract command but not the diff command.
{
    "debug": True,
    "log_json": True,
    "exclue_from": "baz/spam.txt",
    "strip_components": 2,
    "sort": True,
    "json_lines": True,
}
  • log_level: defualt log level, can be overriden for a specific comand by passing in another level as and keyword argument
  • log_json: log lines written by logger are formatted as json lines, passed into the logging setup

Setting Environment Variables

You are able to manage the environment variables used by borg to be able to use different settings for different repositories.

There are 3 ways you can set the variables:

  1. filename: Path to a file that contains the variables and their values. See the python-dotenv README for more information.
  2. dictionary: Dictionary that contains the variable names as keys with their corresponding values set.
  3. **kwargs: Argument names are the variable names and the values are what will be set.
api.set_environ(filename="foo/bar/.env")
api.set_environ(dictionary={"FOO":"BAR", "SPAM":False})
api.set_environ(FOO="BAR", SPAM=False)

Only one value will be used if multiple set, filename has highest precedence, followed by dictionary, and fallback to **kwargs.

If no values are given for any of the three things (ie. calling with no arguments), then the default behavior for load_dotenv from python-dotenv will be used, which is searching for a ".env" file somewhere above in the current file path.

Environment Variables used by borgbackup.

Removing Environment Variables

If you want to unset a variable so it doesn't get used for another command you can use the unset_environ method. It'll remove any variables passed in from the current environment. If no variables are passed in, it'll remove the variables set from the last call to set_environ.

# Enironment = {}
api.set_environ(dictionary={"FOO":"BAR", "SPAM":False})
# Enironment = {"FOO": "BAR", "SPAM": "False"}
api.unset_environ("FOO")
# Enironment = {"SPAM": "False"}
api.set_environ(BAZ="HAM")
# Enironment = {"SPAM": "False", "BAZ": "HAM"}
api.unset_environ("OTHER")
# Enironment = {"SPAM": "False", "BAZ": "HAM"}
api.unset_environ()
# Enironment = {"SPAM": "False"}

Borg Commands

When using a borg command any of the arguments can be set as keyword arguments. The argument names are the long option names with dashes turned into underscores. So the --storage-quota argument in init gets turned into the keyword argument storage_quota.

api.init(
    repository="/foor/bar",
    encryption="repokey",
    append_only=True,
    storage_quota="5G",
    make_parent_dirs=True,
    debug=True,
    log_json=True,
)

diff_args = {
    sort: True,
    json_lines: True,
    debug: True,
    exclude_from: "./exclude_patterns.txt",
}

api.diff(
    "/foo/bar::tuesday",
    "friday",
    "/foo/bar",
    "/baz",
    **diff_args,
)

Available Borg Commands

  • init
  • create
  • extract
  • check
  • rename
  • list
  • diff
  • delete
  • prune
  • info
  • mount
  • umount
  • key_change_passphrase (key change-passphrase)
  • key_export (key export)
  • key_import (key import)
  • upgrade
  • export_tar
  • serve
  • config
  • with-lock
  • break-lock
  • benchmark crud

Unavailable Borg Commands

  • recreate
    • Since this is an experimental feature there are no current plans to implament this.

Command Quirks

Things that were changed from the way the default borg commands work to make things a bit more manageable.

  • init
    • encryption is an optional argument that defaults to repokey
  • config
    • borg config can only change one key at a time
    • changes is a list of (NAME, VALUE) tuples so multiple changes can be made at once to the same repository

Capturing Output

borg commands display information different depending on what is asked for. For example, create with the --list option writes the file list to the logger. When the --log-json common flag is included it writes it to stderr. The --stats option writes to the logger, like the --list option does, but when --json is used, which outputs the stats info as a json object, it gets written to stdout.

Currently the api returns a tuple, (stderr, stdout). If either json or log_json is set, it'll try to convert the tuple output to json. If it is unable and there is output that is captured it'll return the plaintext value. If no output is captured, it returns None.

This will be researched more so that specific commands will return relevant and clear values when called.

Roadmap

  • Make compatible with same version of Python that Borg uses (currently 3.5)
  • Start work on Borg's beta branch chagnes and keeping up with those

Links

Contributing

Help is greatly appreciated. First check if there are any issues open that relate to what you want to help with. Also feel free to make a pull request with changes / fixes you make.

License

MIT License

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

borgapi-0.3.1.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

borgapi-0.3.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file borgapi-0.3.1.tar.gz.

File metadata

  • Download URL: borgapi-0.3.1.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.6.13

File hashes

Hashes for borgapi-0.3.1.tar.gz
Algorithm Hash digest
SHA256 23d58c00a3c5bfed6bc8b739a06ae05987373fcb8f6a60b4cdb24d2cd066dbee
MD5 635820119772c9a5d20bbeb38cced09f
BLAKE2b-256 094b5108e31ae61425386400e77b8936fa0d5f3d3db94cbaf607f277d527cd4d

See more details on using hashes here.

File details

Details for the file borgapi-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: borgapi-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.6.13

File hashes

Hashes for borgapi-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e19b96e83e502afaa393b4e5d005fc0c26ed61758dae4cee39d3222a2eaf2236
MD5 774490983c5bd1cfdf0d27ab68030276
BLAKE2b-256 e4fb45ccd127c70a91e54aac7b05e1d65121409c8d6c57f53f0bda1da5fd22a9

See more details on using hashes here.

Release history Release notifications | RSS feed

0.7.1

2 files

0.7.0

2 files

0.6.1

2 files

0.6.0

2 files

0.5.1

2 files

0.5.0

2 files

0.4.0

2 files

This release

0.3.1 This release

2 files

0.3.0

2 files

0.2.1

2 files

0.2.0

2 files

0.1.3.dev4

2 files

0.1.3.dev3

2 files

0.1.1

2 files

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