giner.barman
Ansible 角色:Barman
在 Linux 上安装和配置 Barman,并可选:
- 定期备份(通过 cron 执行)
- 上传 wals 和快照到 S3(通过 cron 执行)
- 指标收集(通过 telegraf)
这个角色尽量保持中立,即所有配置参数直接传递给 barman 配置。
需求
- Ubuntu
安装
通过 Ansible Galaxy 安装:
ansible-galaxy collection install giner.barman
或在你的 requirements.yml
文件中包含这个角色:
roles:
- name: giner.barman
角色变量
可用变量如下所示,包含默认值(见 defaults/main.yml
):
Barman 请求的状态。用 absent
覆盖以卸载 Barman,数据不会被删除。
barman_state: started
Barman 软件包请求的状态。用 latest
覆盖以升级 Barman。
barman_package_state: present
Barman 的全局配置覆盖(键/值)。
barman_config: {}
要备份的 PostgreSQL 服务器。
barman_pg_servers: [] # 键包括:name, params, pgpass 和 cron。下面有示例。
配置 telegraf 以收集 Barman 指标。
barman_telegraf_enabled: false
指标收集间隔。
barman_telegraf_interval: 1m
依赖
无。
示例剧本
# 在 PostgreSQL 数据库服务器上配置用户及其权限
# 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: 添加 PostgreSQL 用户 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: 添加 PostgreSQL 用户 barman_streaming_user
community.postgresql.postgresql_user:
user: "{{ barman_streaming_user }}"
password: "{{ barman_streaming_pass }}"
role_attr_flags: replication
- name: 授予 barman_user 对函数 pg_XXX 的执行权限
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 }}"
# 设置和配置 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 数据库(仅流式)"
- 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: "恢复窗口为 31 天"
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
开发
安装测试依赖:
python3 -m pip install ansible -Ur requirements-molecule.txt
运行所有测试(需要安装 docker):
molecule test --all
许可证
Apache 2.0
作者
这个角色是由 Stanislav German-Evtushenko 于 2021 年创建。
关于项目
Install and configure Barman on Linux with optional: metrics collection (telegraf), regular snapshots (cron) and uploading to S3 (cron)
安装
ansible-galaxy install giner.barman
许可证
apache-2.0
下载
14.6k