Skip to content

Commit

Permalink
Merge pull request #16 from SebastianRzk/develop
Browse files Browse the repository at this point in the history
add initial checkup
  • Loading branch information
SebastianRzk authored Jul 19, 2023
2 parents 6408031 + da06d29 commit e7201a6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
17 changes: 9 additions & 8 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ An example can be found in the example-project folder.

### Push metrics to prometheus push gateway

| parameter | default value | explanation |
|--------------------------------------|-------------------------------|-------------------------------------------------------------------------------------------------------------|
| BORG_PROMETHEUS_PUSHGATEWAY_ENABLED | yes | Everything else than "yes" switches the metric collection as well as the push to the prometheus gateway off |
| BORG_PROMETHEUS_PUSHGATEWAY | prometheus-pushgateway:9091 | Url of the prometheus push gateway |
| BORG_PROMETHEUS_PUSHGATEWAY_USERNAME | none | username used to push to prometheus |
| BORG_PROMETHEUS_PUSHGATEWAY_PASSWORD | none | password used to push to prometheus |
| BORG_PROMETHEUS_PUSHGATEWAY_JOBNAME | push-borg | job name that will be pushed to the prometheus gateway |
| BORG_INSTANCE_NAME | hostname | instance name that is pushed to the prometheus gateway |
| parameter | default value | explanation |
|-----------------------------------------|-------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| BORG_PROMETHEUS_PUSHGATEWAY_ENABLED | yes | Everything else than "yes" switches the metric collection as well as the push to the prometheus gateway off |
| BORG_PROMETHEUS_INITIAL_CHECKUP_ENABLED | yes | Everything else than "yes" switches initial checkup. Initial checkup pushes initial metrics to push gateway on container startup. If the host machine reboots. |
| BORG_PROMETHEUS_PUSHGATEWAY | prometheus-pushgateway:9091 | Url of the prometheus push gateway |
| BORG_PROMETHEUS_PUSHGATEWAY_USERNAME | none | username used to push to prometheus |
| BORG_PROMETHEUS_PUSHGATEWAY_PASSWORD | none | password used to push to prometheus |
| BORG_PROMETHEUS_PUSHGATEWAY_JOBNAME | push-borg | job name that will be pushed to the prometheus gateway |
| BORG_INSTANCE_NAME | hostname | instance name that is pushed to the prometheus gateway |

## Prometheus metrics

Expand Down
2 changes: 2 additions & 0 deletions borg-backup-docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ BASH_ENV=/container.env
$BORG_BACKUP_CRON python3 /main.py
# This extra line makes it a valid cron" > scheduler.txt;

python3 /main.py --initial-checkup

crontab scheduler.txt;
crond -f;

40 changes: 37 additions & 3 deletions borg-backup-docker/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import subprocess
import os
import sys
import json
import socket
import logging
Expand Down Expand Up @@ -52,6 +53,9 @@
BORG_PROMETHEUS_PUSHGATEWAY_PASSWORD_NAME = 'BORG_PROMETHEUS_PUSHGATEWAY_PASSWORD'
BORG_PROMETHEUS_PUSHGATEWAY_PASSWORD_DEFAULT = None

BORG_PROMETHEUS_INITIAL_CHECKUP_ENABLED_NAME = 'BORG_PROMETHEUS_INITIAL_CHECKUP_ENABLED'
BORG_PROMETHEUS_INITIAL_CHECKUP_ENABLED_DEFAULT = 'yes'


def get_or_default(name, default_value):
value = os.environ.get(name)
Expand Down Expand Up @@ -120,6 +124,16 @@ def encryption_passphrase():
return get_or_default(BORG_BACKUP_ENCRYPTION_PASSPHRASE, BORG_BACKUP_ENCRYPTION_PASSPHRASE_DEFAULT)


def initial_checkup():
return "--initial-checkup" in sys.argv


def initial_checkup_enabled():
return get_or_default(
BORG_PROMETHEUS_INITIAL_CHECKUP_ENABLED_NAME,
BORG_PROMETHEUS_INITIAL_CHECKUP_ENABLED_DEFAULT) == 'yes'


def call_in_borg_env(command):
if encryption_enabled():
logging.info("call with BORG_PASSPHRASE set")
Expand Down Expand Up @@ -276,20 +290,40 @@ def time_command(name, description, command, registry):
summary.observe(time_used)


if __name__ == "__main__":
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
def run_backup():
logging.info('triggered by cron')
if is_init_enabled():
init_backup()
registry = CollectorRegistry()
i = Info('instance', 'Information about the borg backup container instance.', registry=registry)
i.info({'instance_name': instance_name()})

time_command('borg_create_backup_time', 'Seconds used to create the backup.', create_backup, registry)
time_command('borg_prune_backup_time', 'Seconds used to prune the backup.', prune_backup, registry)
time_command('borg_compact_backup_time', 'Seconds used to compact the backup.', compact_backup, registry)
if is_push_enabled():
create_info(registry)
push_to_gateway(pushgateway(), job=jobname(), registry=registry, handler=auth_handler)
logging.info('done')


def run_initial_checkup():
logging.info('running initial checkup')
registry = CollectorRegistry()
i = Info('instance', 'Information about the borg backup container instance.', registry=registry)
i.info({'instance_name': instance_name()})
if is_push_enabled():
create_info(registry)
push_to_gateway(pushgateway(), job=jobname(), registry=registry, handler=auth_handler)
logging.info('done')


if __name__ == "__main__":
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
if initial_checkup():
logging.info('Starting on for initial checkup')
if initial_checkup_enabled():
run_initial_checkup()
else:
logging.info("Initial checkup disabled")
else:
run_backup()

0 comments on commit e7201a6

Please sign in to comment.