Git Product home page Git Product logo

giner / ansible-role-barman Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 5.0 51 KB

Ansible role to Install and configure Barman on Linux with optional: regular backup (running by cron), wals and snapshots uploading to S3 (running by cron), metrics collection (by telegraf)

Home Page: https://galaxy.ansible.com/giner/barman

License: Apache License 2.0

Shell 61.02% Jinja 38.98%
barman backup ansible ansible-role postgresql

ansible-role-barman's Introduction

Run tests

Ansible Role: Barman

Installs and configures Barman on Linux with optional:

  • regular backup (running by cron)
  • wals and snapshots uploading to S3 (running by cron)
  • metrics collection (by telegraf)

This role is aimed to be as unopinionated as possible, i.e. all configuration parameters are passed through directly to barman configs.

Requirements

  • Ubuntu

Installation

Install via Ansible Galaxy:

ansible-galaxy collection install giner.barman

Or include this role in your requirements.yml file:

roles:
  - name: giner.barman

Role Variables

Available variables are listed below, along with default values (see defaults/main.yml):

Barman requested state. Override with absent to uninstall Barman. Data won't be removed.

barman_state: started

Barman package requested state. Override with latest to upgrade Barman.

barman_package_state: present

Barman's global config overrides (key/value).

barman_config: {}

PostgreSQL servers to backup.

barman_pg_servers: []    # The keys are: name, params, pgpass and cron. See an example below.

Configure telegraf to collect Barman metrics.

barman_telegraf_enabled: false

Metrics collection interval.

barman_telegraf_interval: 1m

Dependencies

None.

Example Playbook

# Configure users and their privileges on PostgreSQL db server
# http://docs.pgbarman.org/release/2.12/#postgresql-connection
- 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 FUNCTION pg_XXX TO barman_user
    community.postgresql.postgresql_privs:
      db: postgres
      privs: EXECUTE
      type: function
      obj: pg_start_backup(text:boolean:boolean),pg_stop_backup(),pg_stop_backup(boolean:boolean),pg_switch_wal(),pg_create_restore_point(text)
      schema: pg_catalog
      roles: "{{ barman_user }}"

# Setup and configure 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

Install test dependencies:

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

Run all tests (requires docker to be installed):

molecule test --all

License

Apache 2.0

Authors

This role was created in 2021 by Stanislav German-Evtushenko

ansible-role-barman's People

Contributors

giner avatar hit0ri avatar jabbors avatar martbhell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

ansible-role-barman's Issues

Support for defining custom cron jobs

I have a use case where I'd like to manage custom cron jobs for each backup job, ie I'd want to backup the backups to another remote server using tools like borg or restic.

The configuration could look something like

- name: my-server
    params:
      - <irrelevant>
    pgpass:
      - <irrelevant>
    backup_schedule:
      cron:
        hour: 2
        minute: 11
      custom:
        job: 'borg create ssh://[email protected]:/home/barman/my-server::$(date --iso-8601=date) /var/lib/barman/my-server'
        cron:
          hour: 3
          minute: 13

Do you think this would be useful? I can create a PR if you think it will be valuable for this module.

Variable to control running barman tasks

What do you think of adding a variable ie barman_run_commands: true to control whether or not to run the ansible.builtin.commands tasks part of configure.yaml?

In cases where the database is big (many terabytes) it will take a long time to complete the first backup and relying on the Ansible SSH connection to stay open the entire time is unlikely. In these cases it's preferable to start the initial backup manually.

If you like this idea I can contribute with a PR.

Manage SSH keys for rsync backups

When setting up rsync backups it would simply the Ansible manifests if the SSH keys could be managed with the same module.

Happy to wrap up a proposal PR if it would be an acceptable contribution.

Allow logging output from sensitive tasks

What do you think of changing all no_log: true statements to no_log: "{{ barman_configure_no_log }}"?

This would allow the user to override the value when executing with --extra-vars "barman_configure_no_log=false". I find this pattern useful in other modules when I perform a --diff --check to actually see what would change.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.