Barman (Backup and Recovery Manager-备份恢复管理器) 是一个用Python语言实现的PostgreSQL灾难恢复管理工具,它由第二象限公司(2ndQuadrant)开源并维护。它允许我们在关键业务环境中执行远程备份,为数据库管理员在恢复阶段提供有效的数据保证。Barman最优秀的功能包括备份元数据、增量备份、保留策略、远程回复、WAL文件归档压缩和备份。
1. 支持对多版本PG的备份, 已知支持PG 8.2及之后的所有版本
2. 基于数据库全量的备份和WAL的实时备份,有效的满足RPO≈0的需求
3. 支持对数据库文件级的增量备份,依赖于rysnc和hard link
4. 从PG 9.2之后,支持并发(一致性)模式的备份方式,避免排他模式下数据文件中数据不一致的情况。
5. 提供流复制方式的WAL传输备份,减少WAL备份的延迟,保证WAL冗余备份的同时,避免数据库节点上WAL丢失无法还原的情况。
6. 支持压缩传输和带宽限制
7. 提供可靠的监控信息。用于收集Barman备份及服务状态的报告。
8. 可集中化管理的备份集目录,用户便于获取备份数据文件和WAL文件等相应的信息。
9. 自定义的备份集保留策略,以满足业务部门的需求。
10. 从2.2版本开始支持并行的备份和恢复,加速备份和恢复的过程。
11. 支持本地及远程的恢复
Barman部署
Barman 安装
目前barman被集成到PG的yum源中,可以通过PG官网提供的yum源获取安装包进行安装:
https://www.postgresql.org/download/linux/redhat/
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Barman配置
以流复制为例(该方式Barman和PostgreSQL 之间不依赖SSH通讯):
目标数据库:
创建barman需要的用户
psql=# create user barman password '<encrypt_password>' superuser;
psql=# create user streaming_barman password '<encrypt_password>' superuser;
数据参数与访问规则配置
Postgresql.conf
# WAL级别设置至少保证archive(v9.5为replica)以上的
# 如果使用流复制的方式传输WAL到barman,archive_command则不是必须项
# 最大WAL sender进程的数量,总数 > wal archive + streaming/logical replication
# 最少保留WAL文件的数量,防止WAL被服务节点过早移除
Pg_hba.conf
# 访问控制,允许barman的用户分别以superuser和replication的权限访问数据库
host all barman 0.0.0.0/0 md5
host replication streaming_barman 0.0.0.0/0 md5
psql "dbname=postgres port=5432 replication=database user=streaming_barman password=<encrypt_password>" -c "IDENTIFY_SYSTEM;"
systemid | timeline | xlogpos | dbname ---------------------------------+-----------+-----------------+---------- 6722702146844265873 | 3 | A/A4001AE8 | postgres(1 row)
Barman服务配置
Global 配置信息
log_file = /var/log/barman/barman.log
backup_options = concurrent_backup
retention_policy = RECOVERY WINDOW OF 1 WEEK
retention_policy_mode = auto
wal_retention_policy = main
configuration_files_directory = /etc/barman.conf.d
Individual(目标数据库备份) 配置信息
description = "<target_database> master database(Streaming Only)"
conninfo = host=<target_database> port=5432 user=barman dbname=postgres password=<encrypt_password>
streaming_conninfo = host=target_database port=5432 user=streaming_barman dbname=postgres password=<encrypt_password>
slot_name = streaming_barman
backup_options = concurrent_backup
streaming_wals_directory=/barman/<target_database>/streaming
retention_policy = RECOVERY WINDOW OF 1 DAYS
path_prefix=/usr/pgsql-10/bin
备份目标库
备份前验证
#验证备份服务配置的状态,确保所有状态OK后,才可以开启备份
barman check <target_database>
Server <target_database>:
retention policy settings: OK
backup maximum age: OK (no last_backup_maximum_age provided)
failed backups: OK (there are 0 failed backups)
minimum redundancy requirements: OK (have 3 backups, expected at least 3)
pg_basebackup compatible: OK
pg_basebackup supports tablespaces mapping: OK
pg_receivexlog compatible: OK
发起备份
$ barman backup <target_database># 加入到crontabcrontab -e 23 * * * /usr/bin/barman backup <target_database> > /barman/<target_database>/base/barman.log 2>&1
恢复目标库
恢复前最好保留目标数据库的数据文件目录,避免恢复失败时无法复原。
基于全量恢复
$ barman recover <target_database> 20191127T220002 ${PGDATA}
PITR
基于PITR的恢复
$ barman recover --target-time "2019-11-27 15:00:00" <target_database> 20191127T220002 ${PGDATA}
远程恢复目标机
$ barman recover --remote-ssh-command "ssh postgres@<target_host>" --target-time "2019-11-27 15:00:00" <target_database> 20191127T220002 ${PGDATA}
日常维护命令
#列出所有的备份服务信息(--minimal 参数仅显示服务名称,不包含描述信息)
$ barman show-server <target_database>
$ barman list-backup <target_database>
$ barman show-backup <target_database> <backup_id>
$ barman check <target_database>
$ barman status <target_database>
故障排错
未给barman配置具有superuser权限的用户访问PG服务器
psql=# create user barman password '<encrypt_password>' superuser;
$ createuser -s -p 5432 -Upostgres -d postgres barman
continuous archiving:Failed
receive-wal running: Failed
可以尝试通过重建replication slot刷新WAL的接收状态
barman receive-wal --stop <target_database>
barman receive-wal --reset <target_database>
barman receive-wal --create-slot <target_database>
barman switch-wal <target_database>
WAL archive: FAILED (please make sure WAL shipping is setup)
$ barman switch-wal --force --archive <target_database>
The WAL file 000000020000001400000096 has been closed on server '<target_database>'
Waiting for the WAL file 000000020000001400000096 from server '<target_database>' (max: 30 seconds)
Processing xlog segments from streaming for fcden1pgs01_master