Skip to content

Commit e7201a6

Browse files
authored
Merge pull request #16 from SebastianRzk/develop
add initial checkup
2 parents 6408031 + da06d29 commit e7201a6

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

Readme.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,15 @@ An example can be found in the example-project folder.
8080

8181
### Push metrics to prometheus push gateway
8282

83-
| parameter | default value | explanation |
84-
|--------------------------------------|-------------------------------|-------------------------------------------------------------------------------------------------------------|
85-
| BORG_PROMETHEUS_PUSHGATEWAY_ENABLED | yes | Everything else than "yes" switches the metric collection as well as the push to the prometheus gateway off |
86-
| BORG_PROMETHEUS_PUSHGATEWAY | prometheus-pushgateway:9091 | Url of the prometheus push gateway |
87-
| BORG_PROMETHEUS_PUSHGATEWAY_USERNAME | none | username used to push to prometheus |
88-
| BORG_PROMETHEUS_PUSHGATEWAY_PASSWORD | none | password used to push to prometheus |
89-
| BORG_PROMETHEUS_PUSHGATEWAY_JOBNAME | push-borg | job name that will be pushed to the prometheus gateway |
90-
| BORG_INSTANCE_NAME | hostname | instance name that is pushed to the prometheus gateway |
83+
| parameter | default value | explanation |
84+
|-----------------------------------------|-------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
85+
| BORG_PROMETHEUS_PUSHGATEWAY_ENABLED | yes | Everything else than "yes" switches the metric collection as well as the push to the prometheus gateway off |
86+
| 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. |
87+
| BORG_PROMETHEUS_PUSHGATEWAY | prometheus-pushgateway:9091 | Url of the prometheus push gateway |
88+
| BORG_PROMETHEUS_PUSHGATEWAY_USERNAME | none | username used to push to prometheus |
89+
| BORG_PROMETHEUS_PUSHGATEWAY_PASSWORD | none | password used to push to prometheus |
90+
| BORG_PROMETHEUS_PUSHGATEWAY_JOBNAME | push-borg | job name that will be pushed to the prometheus gateway |
91+
| BORG_INSTANCE_NAME | hostname | instance name that is pushed to the prometheus gateway |
9192

9293
## Prometheus metrics
9394

borg-backup-docker/entrypoint.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ BASH_ENV=/container.env
1111
$BORG_BACKUP_CRON python3 /main.py
1212
# This extra line makes it a valid cron" > scheduler.txt;
1313

14+
python3 /main.py --initial-checkup
15+
1416
crontab scheduler.txt;
1517
crond -f;
1618

borg-backup-docker/main.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import subprocess
22
import os
3+
import sys
34
import json
45
import socket
56
import logging
@@ -52,6 +53,9 @@
5253
BORG_PROMETHEUS_PUSHGATEWAY_PASSWORD_NAME = 'BORG_PROMETHEUS_PUSHGATEWAY_PASSWORD'
5354
BORG_PROMETHEUS_PUSHGATEWAY_PASSWORD_DEFAULT = None
5455

56+
BORG_PROMETHEUS_INITIAL_CHECKUP_ENABLED_NAME = 'BORG_PROMETHEUS_INITIAL_CHECKUP_ENABLED'
57+
BORG_PROMETHEUS_INITIAL_CHECKUP_ENABLED_DEFAULT = 'yes'
58+
5559

5660
def get_or_default(name, default_value):
5761
value = os.environ.get(name)
@@ -120,6 +124,16 @@ def encryption_passphrase():
120124
return get_or_default(BORG_BACKUP_ENCRYPTION_PASSPHRASE, BORG_BACKUP_ENCRYPTION_PASSPHRASE_DEFAULT)
121125

122126

127+
def initial_checkup():
128+
return "--initial-checkup" in sys.argv
129+
130+
131+
def initial_checkup_enabled():
132+
return get_or_default(
133+
BORG_PROMETHEUS_INITIAL_CHECKUP_ENABLED_NAME,
134+
BORG_PROMETHEUS_INITIAL_CHECKUP_ENABLED_DEFAULT) == 'yes'
135+
136+
123137
def call_in_borg_env(command):
124138
if encryption_enabled():
125139
logging.info("call with BORG_PASSPHRASE set")
@@ -276,20 +290,40 @@ def time_command(name, description, command, registry):
276290
summary.observe(time_used)
277291

278292

279-
if __name__ == "__main__":
280-
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
293+
def run_backup():
281294
logging.info('triggered by cron')
282295
if is_init_enabled():
283296
init_backup()
284297
registry = CollectorRegistry()
285298
i = Info('instance', 'Information about the borg backup container instance.', registry=registry)
286299
i.info({'instance_name': instance_name()})
287-
288300
time_command('borg_create_backup_time', 'Seconds used to create the backup.', create_backup, registry)
289301
time_command('borg_prune_backup_time', 'Seconds used to prune the backup.', prune_backup, registry)
290302
time_command('borg_compact_backup_time', 'Seconds used to compact the backup.', compact_backup, registry)
303+
if is_push_enabled():
304+
create_info(registry)
305+
push_to_gateway(pushgateway(), job=jobname(), registry=registry, handler=auth_handler)
306+
logging.info('done')
307+
291308

309+
def run_initial_checkup():
310+
logging.info('running initial checkup')
311+
registry = CollectorRegistry()
312+
i = Info('instance', 'Information about the borg backup container instance.', registry=registry)
313+
i.info({'instance_name': instance_name()})
292314
if is_push_enabled():
293315
create_info(registry)
294316
push_to_gateway(pushgateway(), job=jobname(), registry=registry, handler=auth_handler)
295317
logging.info('done')
318+
319+
320+
if __name__ == "__main__":
321+
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
322+
if initial_checkup():
323+
logging.info('Starting on for initial checkup')
324+
if initial_checkup_enabled():
325+
run_initial_checkup()
326+
else:
327+
logging.info("Initial checkup disabled")
328+
else:
329+
run_backup()

0 commit comments

Comments
 (0)