Send alert when something went wrong during backup

This commit is contained in:
Einar Forselv 2019-12-04 20:55:33 +01:00
parent d9a082d044
commit 1ca678f6b4
1 changed files with 17 additions and 16 deletions

View File

@ -100,7 +100,6 @@ def backup(config, containers):
}, },
) )
logger.info('Backup container exit code: %s', result) logger.info('Backup container exit code: %s', result)
# TODO: Alert
def start_backup_process(config, containers): def start_backup_process(config, containers):
@ -114,6 +113,7 @@ def start_backup_process(config, containers):
return return
status(config, containers) status(config, containers)
errors = False
# Back up volumes # Back up volumes
try: try:
@ -122,10 +122,10 @@ def start_backup_process(config, containers):
logger.debug('Volume backup exit code: %s', vol_result) logger.debug('Volume backup exit code: %s', vol_result)
if vol_result != 0: if vol_result != 0:
logger.error('Backup command exited with non-zero code: %s', vol_result) logger.error('Backup command exited with non-zero code: %s', vol_result)
# TODO: Alert errors = True
except Exception as ex: except Exception as ex:
logger.error(ex) logger.error(ex)
# TODO: Alert errors = True
# back up databases # back up databases
for container in containers.containers_for_backup(): for container in containers.containers_for_backup():
@ -137,27 +137,28 @@ def start_backup_process(config, containers):
logger.debug('Exit code: %s', result) logger.debug('Exit code: %s', result)
if result != 0: if result != 0:
logger.error('Backup command exited with non-zero code: %s', result) logger.error('Backup command exited with non-zero code: %s', result)
# TODO: Alert errors = True
except Exception as ex: except Exception as ex:
logger.error(ex) logger.error(ex)
# TODO: Alert errors = True
# Alert the user if something went wrong
if errors:
alerts.send(
subject="Backup process exited with non-zero code",
body=open('backup.log').read(),
alert_type='ERROR',
)
def alert(config, containers): def alert(config, containers):
"""Test alerts""" """Test alerts"""
logger.info("Testing alerts") logger.info("Testing alerts")
alerts.send(
alert_classes = alerts.configured_alert_classes()
for instance in alert_classes:
logger.info('Configured: %s', instance.name)
instance.send(
subject="{}: Test Alert".format(containers.project_name), subject="{}: Test Alert".format(containers.project_name),
body="Test message", body="Test message",
) )
if len(alert_classes) == 0:
logger.info("No alerts configured")
def parse_args(): def parse_args():
parser = argparse.ArgumentParser(prog='restic_compose_backup') parser = argparse.ArgumentParser(prog='restic_compose_backup')