Overview
With python we can also automate the barman backup and the zabbix monitoring as follows:
Source
import os import sys sys.path.append('/opt/applications/management-tools/apps/nms-scripts/service/scripts/functions/') sys.path.append('/opt/applications/management-tools/apps/nms-scripts/service/scripts/conf/') import zabbixconfig from zabbixconfig import zabbixmetric def run_barmanbackup(): try: print('Starting Backup') stream = os.popen('barman list-backup pgsql') failed_bkp_id = "" last_backup_id = "" failed_backups = 0 total_backups = 0 for line in stream.read().splitlines(): if "FAILED" in line: failed_backups += 1 failed_bkp_id = line.split(" ")[1] print('Found failed backup %s, will delete it.' % failed_bkp_id) delete_command = ('barman delete pgsql %s' % failed_bkp_id) os.system(delete_command) last_backup_id = line.split(" ")[1] total_backups += 1 if total_backups - failed_backups >= 3: print('We have at least two healthy backups, will delete the last one: %s' % last_backup_id) delete_command = ('barman delete pgsql %s' % last_backup_id) os.system(delete_command) barman = os.popen('barman backup pgsql') output = barman.read() print(output) if output in 'ERROR': print("Failed running barman PostgreSQL backup") packet = zabbixmetric('bkp_status', "ERROR - FAILED running barman PostgreSQL backup") else: print("Backup run successfully") packet = zabbixmetric('bkp_status', "Backup completed successfully") except Exception as err: print(f'Failed running barman PostgreSQL backup : {err}') packet = zabbixmetric(f'bkp_status', 'ERROR - FAILED running barman PostgreSQL backup : {err}') run_barmanbackup()
That script will keep 3 backups on the barman server.