giner.barman

Ansible Role: Barman

This role installs and sets up Barman on Linux, with options for:

  • Running regular backups using cron jobs.
  • Uploading WAL files and snapshots to S3 using cron jobs.
  • Collecting metrics with Telegraf.

The configuration is designed to be as flexible as possible, allowing all settings to go directly to Barman's configuration files.

Requirements

  • Ubuntu

Installation

You can install it using Ansible Galaxy:

ansible-galaxy collection install giner.barman

Or add this role to your requirements.yml file:

roles:
  - name: giner.barman

Role Variables

Here are the available variables, along with their default values (see defaults/main.yml):

  • Barman state: Set to started to run Barman, or absent to uninstall (data will remain).

    barman_state: started
    
  • Barman package state: Set to latest to upgrade Barman.

    barman_package_state: present
    
  • Global Barman config overrides (key/value).

    barman_config: {}
    
  • PostgreSQL servers to backup. Use keys: name, params, pgpass, and cron. Example provided below.

    barman_pg_servers: []
    
  • Enable Telegraf for metrics collection.

    barman_telegraf_enabled: false
    
  • Metrics collection interval.

    barman_telegraf_interval: 1m
    

Dependencies

None.

Example Playbook

Here's a sample playbook for configuring PostgreSQL users and setting up Barman:

Configure PostgreSQL Users

- hosts: postgresqls
  vars:
    barman_user: barman
    barman_pass: BARMANPASS_CHANGEME
    barman_streaming_user: streaming_barman
    barman_streaming_pass: STREAMINGPASS_CHANGEME
  tasks:
    - name: Add PostgreSQL user barman_user
      community.postgresql.postgresql_user:
        user: "{{ barman_user }}"
        password: "{{ barman_pass }}"
        role_attr_flags: replication
        groups: [pg_read_all_settings, pg_read_all_stats]

    - name: Add PostgreSQL user barman_streaming_user
      community.postgresql.postgresql_user:
        user: "{{ barman_streaming_user }}"
        password: "{{ barman_streaming_pass }}"
        role_attr_flags: replication

    - name: Grant execute privileges on functions to barman_user
      community.postgresql.postgresql_privs:
        db: postgres
        privs: EXECUTE
        type: function
        obj: pg_start_backup(text:boolean:boolean),pg_stop_backup(),pg_switch_wal(),pg_create_restore_point(text)
        schema: pg_catalog
        roles: "{{ barman_user }}"

Setup Barman

- hosts: barmans
  vars:
    barman_name: mypgserver
    barman_pg_hosts: 10.10.10.10
    barman_pg_ports: 5432
    barman_user: barman
    barman_pass: BARMANPASS_CHANGEME
    barman_streaming_user: streaming_barman
    barman_streaming_pass: STREAMINGPASS_CHANGEME

    barman_pg_servers:
    - name: "{{ barman_name }}"
      params:
      - description: "PostgreSQL Database (Streaming-Only)"
      - conninfo: 'host={{ barman_pg_hosts }} port={{ barman_pg_ports }} user={{ barman_user }} dbname=postgres'
      - streaming_conninfo: 'host={{ barman_pg_hosts }} port={{ barman_pg_ports }} user={{ barman_streaming_user }}'
      - backup_method: "postgres"
      - streaming_archiver: "on"
      - slot_name: "barman"
      - create_slot: "auto"
      - retention_policy: "recovery window of 31 days"
      pgpass:
      - "*:*:postgres:{{ barman_user }}:{{ barman_pass }}"
      - "*:*:replication:{{ barman_streaming_user }}:{{ barman_streaming_pass }}"
      
      backup_schedule:
        cron:
          hour: 21
          minute: 5
        s3_sync:
          src: "{{ barman_config['barman_home'] | default('/var/lib/barman') }}/{{ barman_name }}"
          dst: "s3://db-backup"
          base_cron:
            hour: 22
            minute: 5
          wals_cron:
            minute: 10
        custom:
          job: "barman delete '{{ barman_name }}' oldest"
          cron:
            hour: 23
            minute: 15
  roles:
  - giner.barman

Development

To install test dependencies:

python3 -m pip install ansible -Ur requirements-molecule.txt

Run all tests (Docker must be installed):

molecule test --all

License

This code is licensed under Apache 2.0.

Authors

This role was created by Stanislav German-Evtushenko in 2021.

Informazioni sul progetto

Install and configure Barman on Linux with optional: metrics collection (telegraf), regular snapshots (cron) and uploading to S3 (cron)

Installa
ansible-galaxy install giner.barman
Licenza
apache-2.0
Download
14.6k