暂无图片
暂无图片
2
暂无图片
暂无图片
暂无图片

Barman备份方案介绍

2361

作者简介

钟硕

现供职于迪卡侬,PostgreSQL & Oracle DBA

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;
数据参数与访问规则配置
Postgresql.conf
# 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
    ---------------------------------+-----------+-----------------+----------
    6722702146844265873 | 3 | A/A4001AE8 | postgres
    (1 row)

    Barman服务配置

    Global 配置信息
    [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> 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 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

      故障排错

       
      PostgreSQL: Failed
      未给barman配置具有superuser权限的用户访问PG服务器
      psql=# create user barman password '<encrypt_password>' superuser;
                  或者
      $ createuser -s -p 5432 -Upostgres -d postgres barman
       
      continuous archiving:Failed
              检查目标WAL归档是否正常
       
      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)
      强行切换WAL使其归档到Barman服务器
      $ 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
      000000020000001400000096
       
      参考:
      第二象限官方文档
      http://docs.pgbarman.org/release/2.11/index.html

      扫二维码|关注我们

      每周免费看直播





      PostgreSQL中文社区欢迎广大技术人员投稿
      投稿邮箱:press@postgres.cn


       

      文章转载自PostgreSQL中文社区,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

      评论