2025年Barman备份方案介绍

Barman备份方案介绍Barman 是一款开源的基于 Python 开发的集备份和恢复为一体的优秀 PG 数据库备份工具 它可以对单个 多个 PG 数据库进行远程 本地备份 用以增强业务数据的安全性 为 DBA 提供恢复数据库提供可靠的帮助 Barman 优点 支持对多版本 PG 的备份 已知支持 PG 8 2 及之后的所有版本 基于数据库全量的备份和 WAL 的实时备份

大家好,我是讯享网,很高兴认识大家。

Barman 是一款开源的基于Python开发的集备份和恢复为一体的优秀PG数据库备份工具,它可以对单个/多个PG数据库进行远程/本地备份,用以增强业务数据的安全性,为DBA提供恢复数据库提供可靠的帮助。

Barman 优点

  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/
以版本PG版本11为例
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
可以获取到yum源中barman各个版本的信息
yum provides barman
yum install -y barman

Barman配置

以流复制为例(该方式Barman和PostgreSQL 之间不依赖SSH通讯):
在这里插入图片描述
讯享网
目标数据库:
创建barman需要的用户

psql=# create user barman password '<encrypt_password>' superuser; psql=# create user streaming_barman password '<encrypt_password>' superuser; 

讯享网
讯享网# WAL级别设置至少保证archive(v9.5为replica)以上的 wal_level = replica # 开启归档模式 archive_mode = on # 如果使用流复制的方式传输WAL到barman,archive_command则不是必须项 archive_command = ' ' # 最大WAL sender进程的数量,总数 > wal archive + streaming/logical replication max_wal_senders = 5 # 最少保留WAL文件的数量,防止WAL被服务节点过早移除 wal_keep_segments = 64 

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 ---------------------------------+-----------+-----------------+----------  | 3 | A/A4001AE8 | postgres (1 row) 
讯享网[barman] # barman 备份集存放的目录 barman_home = barman barman_user = barman # barman 服务日志输出的位置 log_file = var/log/barman/barman.log # 服务日志输出的级别 log_level = INFO # 对于PG9.6之后的建议使用一致性备份方式 backup_options = concurrent_backup # 开启备份压缩 compression = gzip minimum_redundancy = 0 # 备份集,备份集中数据,WAL文件的保留策略 retention_policy = RECOVERY WINDOW OF 1 WEEK retention_policy_mode = auto wal_retention_policy = main # 指定目标数据库备份配置的所在目录 configuration_files_directory = etc/barman.conf.d check_timeout = 5 

Individual(目标数据库备份) 配置信息

[<target_database>] description = "<target_database> master database(Streaming Only)" # Barman通过conninfo连接字符 conninfo = host=<target_database> port=5432 user=barman dbname=postgres password=<encrypt_password> # 建立流复制的连接字符串和WAL流复制归档方式 streaming_conninfo = host=target_database port=5432 user=streaming_barman dbname=postgres password=<encrypt_password> # 开启流复制方式的归档 streaming_archiver = on # 通过插槽的方式管控WAL的流传输 slot_name = streaming_barman # 建议针对数据库的备份方式 backup_method = postgres backup_options = concurrent_backup # 通过流复制传输的WAL存放的位置 streaming_wals_directory=/barman/<target_database>/streaming minimum_redundancy = 2 retention_policy = RECOVERY WINDOW OF 1 DAYS # 指定Barman服务器客户端的安装位置 path_prefix=/usr/pgsql-10/bin 
讯享网#验证备份服务配置的状态,确保所有状态OK后,才可以开启备份 barman check <target_database> Server <target_database>: PostgreSQL: OK is_superuser: OK PostgreSQL streaming: OK wal_level: OK replication slot: OK directories: OK retention policy settings: OK backup maximum age: OK (no last_backup_maximum_age provided) compression settings: OK failed backups: OK (there are 0 failed backups) minimum redundancy requirements: OK (have 3 backups, expected at least 3) pg_basebackup: OK pg_basebackup compatible: OK pg_basebackup supports tablespaces mapping: OK archive_mode: OK archive_command: OK continuous archiving: OK pg_receivexlog: OK pg_receivexlog compatible: OK receive-wal running: OK archiver errors: OK 

发起备份

$ barman backup <target_database> # 加入到crontab crontab -e 0 23 * * * usr/bin/barman backup <target_database> > barman/<target_database>/base/barman.log 2>&1 

基于全量恢复

讯享网$ barman recover <target_database> T ${PGDATA} 

基于PITR的恢复

$ barman recover --target-time "2019-11-27 15:00:00" <target_database> T ${PGDATA} 

远程恢复目标机

讯享网$ barman recover --remote-ssh-command "ssh postgres@<target_host>" --target-time "2019-11-27 15:00:00" <target_database> T ${PGDATA} 

日常维护命令

#列出所有的备份服务信息(--minimal 参数仅显示服务名称,不包含描述信息) $ barman list-server #列出单个备份服务的所有配置信息 $ 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> 以Json格式列出所有备份服务及备份集的详细信息 $ barman diagnose 

更多信息可以参考barman --help

故障排错

讯享网psql=# create user barman password '<encrypt_password>' superuser; 

或者

$ createuser -s -p 5432 -Upostgres -d postgres barman 
讯享网barman receive-wal --stop <target_database> barman receive-wal --reset <target_database> barman receive-wal --create-slot <target_database> barman switch-wal <target_database> 
$ barman switch-wal --force --archive <target_database> The WAL file 0000000000096 has been closed on server '<target_database>' Waiting for the WAL file 0000000000096 from server '<target_database>' (max: 30 seconds) Processing xlog segments from streaming for fcden1pgs01_master 0000000000096 

参考:

第二象限官方文档 http://docs.pgbarman.org/release/2.11/index.html

小讯
上一篇 2025-03-13 18:29
下一篇 2025-02-08 17:39

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/89807.html