Skip to content

Commit

Permalink
[FIX] KeyError: 'archives' (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-mityutko committed Feb 24, 2024
1 parent 61e6de0 commit 32d8592
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 166 deletions.
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
dev:
@poetry lock --verbose --no-update
@poetry install --all-extras --verbose
@poetry show

test:
@poetry run pytest --cov-config=pyproject.toml --cov-report=term --cov src
@poetry run coverage html

format:
@poetry run black .
@poetry run isort .

lint:
@poetry run pylint src
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,43 @@ Dashboard is available in the [repo](/observability/grafana-dashboard.json) or o
Alerting rules can be found [here](observability%2Fprometheus-alert.yaml). By default alert will
be triggered if there is no backup for repository within 25 hours.

## Development

### Local Environment

* Install [Poetry](https://python-poetry.org)
* Use provided `Makefile` to setup environment: `make dev`
* Run tests: `make test`

### Docker
* Build and run
```shell
docker build -t borgmatic:tag .
docker run --name borgmatic borgmatic:tag
```
* Rename or remove existing container, if the same name is already in use
```shell
docker container ls -a
docker container rm container-id
```
* Exec into the container and create config
```shell
docker exec -it borgmatic /bin/sh
borgmatic config generate
# The default config will have entries for both remote and local repo
# Remote repo config should be commented before proceeding
vi /etc/borgmatic/config.yaml
borgmatic init --encryption repokey
```
* Misc
```shell
# Output repo info in JSON format
borgmatic info --json --last 1
# Create backup
borgmatic create
# Run exporter
python3 cli.py run --config /etc/borgmatic/config.yaml
```
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ omit = [
[tool.coverage.report]
fail_under = 70

[tool.coverage.html]
directory = ".html"

[tool.black]
skip_numeric_underscore_normalization = true
exclude = ".*(venv|virtualenv|migrations|node_modules|s3)"
Expand Down
43 changes: 23 additions & 20 deletions src/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,70 +70,73 @@ def set_metric(

def collect(borgmatic_configs: list, registry):
borgmatic_configs = " ".join(borgmatic_configs)
repos = run_command(f"borgmatic info -c {borgmatic_configs} --json")
# Overall repo info and last archive only
repos = run_command(f"borgmatic info -c {borgmatic_configs} --json --last 1")
# All archives
archives = run_command(f"borgmatic list -c {borgmatic_configs} --json")

for i in range(len(repos)):
labels = {"repository": repos[i]["repository"]["location"]}
for r, a in zip(repos, archives):
labels = {"repository": r["repository"]["location"]}

# Total Backups
set_metric(
registry=registry,
metric="borg_total_backups",
labels=labels,
value=len(repos[i]["archives"]),
value=len(a.get("archives", [])),
)

# Total Chunks
set_metric(
registry=registry,
metric="borg_total_chunks",
labels=labels,
value=repos[i]["cache"]["stats"]["total_chunks"],
value=r["cache"]["stats"]["total_chunks"],
)

# Compressed Size
set_metric(
registry=registry,
metric="borg_total_compressed_size",
labels=labels,
value=repos[i]["cache"]["stats"]["total_csize"],
value=r["cache"]["stats"]["total_csize"],
)

# Total Size
set_metric(
registry=registry,
metric="borg_total_size",
labels=labels,
value=repos[i]["cache"]["stats"]["total_size"],
value=r["cache"]["stats"]["total_size"],
)

# Total Deduplicated Compressed Size
set_metric(
registry=registry,
metric="borg_total_deduplicated_compressed_size",
labels=labels,
value=repos[i]["cache"]["stats"]["unique_csize"],
value=r["cache"]["stats"]["unique_csize"],
)

# Total Deduplicated Size
set_metric(
registry=registry,
metric="borg_total_deduplicated_size",
labels=labels,
value=repos[i]["cache"]["stats"]["unique_size"],
value=r["cache"]["stats"]["unique_size"],
)

if len(repos[i]["archives"]) == 0:
continue

latest_archive = repos[i]["archives"][-1]
# Last Backup Timestamp
set_metric(
registry=registry,
metric="borg_last_backup_timestamp",
labels=labels,
value=arrow.get(latest_archive["end"]).replace(tzinfo="local").timestamp(),
)
if r.get("archives") and len(r["archives"]) > 0:
# Last Backup Timestamp
latest_archive = r["archives"][-1]
set_metric(
registry=registry,
metric="borg_last_backup_timestamp",
labels=labels,
value=arrow.get(latest_archive["end"])
.replace(tzinfo="local")
.timestamp(),
)


def run_command(command: str) -> dict:
Expand Down
179 changes: 179 additions & 0 deletions tests/data/list-info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
[
{
"archives": [
{
"chunker_params": [
"buzhash",
19,
23,
21,
4095
],
"command_line": [
"/usr/local/bin/borg",
"create",
"--one-file-system",
"--read-special",
"/borg/1d76edd9::backup-1-{now}",
"/etc/borgmatic.d/foo.yml",
"/root/.borgmatic"
],
"comment": "",
"duration": 136.541604,
"end": "2024-01-08T21:14:56.000000",
"hostname": "borgmatic-54d98478f9-twdbg",
"id": "9aa75aeed2f44e65f3821b4da9bbf0617f875dfc5c6b076342876273e098967e",
"limits": {
"max_archive_size": 2.9277858752832836e-05
},
"name": "k8s-db-2024-01-08T21:12:39",
"start": "2024-01-08T21:12:40.000000",
"stats": {
"compressed_size": 377726017,
"deduplicated_size": 11469767,
"nfiles": 10,
"original_size": 804895490
},
"username": "root"
},
{
"chunker_params": [
"buzhash",
19,
23,
21,
4095
],
"command_line": [
"/usr/local/bin/borg",
"create",
"--one-file-system",
"--read-special",
"--stats",
"/borg/1d76edd9::backup-1-{now}",
"/etc/borgmatic.d/foo.yml",
"/root/.borgmatic"
],
"comment": "",
"duration": 107.499993,
"end": "2024-01-09T04:01:54.000000",
"hostname": "borgmatic-54d98478f9-twdbg",
"id": "fd21c1301bb6b1989ed1fd4502e93edc6c210c5b4e7d9aff34e10ea2b1c658c2",
"limits": {
"max_archive_size": 2.965932922518245e-05
},
"name": "k8s-db-2024-01-09T04:00:05",
"start": "2024-01-09T04:00:06.000000",
"stats": {
"compressed_size": 379050627,
"deduplicated_size": 10351331,
"nfiles": 11,
"original_size": 807712494
},
"username": "root"
}
],
"encryption": {
"mode": "repokey-blake2"
},
"repository": {
"id": "884577c2d43b8206f499526b70497d27fe8aee697fdd75a9e4d41b483de37a60",
"last_modified": "2024-01-17T04:02:05.000000",
"location": "/borg/backup-1"
}
},
{
"archives": [
{
"chunker_params": [
"buzhash",
19,
23,
21,
4095
],
"command_line": [
"/usr/local/bin/borg",
"create",
"--one-file-system",
"--read-special",
"--stats",
"/borg/1d76edd9::backup-2-{now}",
"/etc/borgmatic.d/bar.yml",
"/root/.borgmatic"
],
"comment": "",
"duration": 114.003346,
"end": "2024-01-16T04:02:02.000000",
"hostname": "borgmatic-54d98478f9-47wnk",
"id": "7ab5f931879e0d3978db5e8c3434a3aff20c67d1ac2e90b48fa9ed2dbc5576a2",
"limits": {
"max_archive_size": 3.0088483506575764e-05
},
"name": "backup-2-2024-01-16T04:00:07",
"start": "2024-01-16T04:00:08.000000",
"stats": {
"compressed_size": 414011339,
"deduplicated_size": 10653075,
"nfiles": 12,
"original_size": 881184771
},
"username": "root"
},
{
"chunker_params": [
"buzhash",
19,
23,
21,
4095
],
"command_line": [
"/usr/local/bin/borg",
"create",
"--one-file-system",
"--read-special",
"--stats",
"/borg/1d76edd9::backup02-{now}",
"/etc/borgmatic.d/bar.yml",
"/root/.borgmatic"
],
"comment": "",
"duration": 117.189547,
"end": "2024-01-17T04:02:05.000000",
"hostname": "borgmatic-54d98478f9-47wnk",
"id": "8b668fc0521f34e630e4fdd5d73306f70b99e39d15b21ac426b2dc20f3fa641f",
"limits": {
"max_archive_size": 3.0040799697532062e-05
},
"name": "backup-2-2024-01-17T04:00:06",
"start": "2024-01-17T04:00:08.000000",
"stats": {
"compressed_size": 419966002,
"deduplicated_size": 18718565,
"nfiles": 12,
"original_size": 893501335
},
"username": "root"
}
],
"encryption": {
"mode": "repokey-blake2"
},
"repository": {
"id": "884577c2d43b8206f499526b70497d27fe8aee697fdd75a9e4d41b483de37a61",
"last_modified": "2024-01-17T04:02:05.000000",
"location": "/borg/backup-2"
}
},
{
"encryption": {
"mode": "repokey-blake2"
},
"repository": {
"id": "784577c2d43b8206f499526b70497d27fe8aee697fdd75a9e4d41b483de37a62",
"last_modified": "2024-01-17T04:02:05.000000",
"location": "/borg/backup-3"
}
}
]
Loading

0 comments on commit 32d8592

Please sign in to comment.