5.Barman介绍及使用

1. Barman介绍

Barman(备份和恢复管理器)是用于PostgreSQL服务器进行灾难恢复的开源管理工具,是以Python编写的。它支持对多台服务器执行远程备份,以降低风险并帮助DBA进行数据库恢复。Barman基于GNU GPL 3发行,由PostgreSQL项目的白金赞助商2ndQuadrant维护。

  • barman注意事项:
    ○ barman  的三种方式 :仅流复制 ; ssh/rsync;  流复制+ssh/rsync
    ○ barman  增量是文件级别,实用性不大
    ○ barman    仅适用于通过rsync,SSH 配置的服务器 --jobs 4
    ○ barman  主要依赖流复制 ,和 rsync
    ○ 远程恢复,管理多个目标库,可以远程恢复,满足日常要求
    ○ 只需要在管理上安装barman  即可

2.Barman安装

2.1 系统要求

#系统要求
    Linux/Unix
    Python >= 3.4
    Python modules:
        argcomplete
        psycopg2 >= 2.4.2
        python-dateutil
        setuptools
        PostgreSQL >= 8.3
        rsync >= 3.0.4 (optional for PostgreSQL >= 9.2)
# 安装python3-dev,python-dateutil*
    yum -y install  install python3-dev    
    yum -y install python-dateutil*


#安装setuptools
    wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26
    tar -zxvf setuptools-19.6.tar.gz
    cd setuptools-19.6
    python setup.py build
    python setup.py install


#安装 argcomplete 和  psycopg2  ,需安装对应的pip
    pip install argcomplete  --user postgres
    pip install psycopg2

2.2 安装

    下载: https://pgbarman.org/
    安装:
    tar -xzvf barman-2.18.tar.gz 
    cd barman-2.18
    # 系统全局安装
        barman@backup$  ./setup.py build
        # run this command with root privileges or through sudo
        su - root 
        barman@backup# ./setup.py install

    # 本地用户安装(用本地安装就可以了)
        barman@backup$  ./setup.py install --user

3.Barman使用

3.1 目标库配置

# 修改postgresql.conf配置

listen_addresses = '*'
port = 5432
wal_level = replica  #11版本后改为replica

archive_mode = on
archive_command = 'cp %p /var/lib/pgsql/11/data/pg_archive/%f'
max_wal_senders = 10  #建议大于等于3
wal_keep_segments = 64
synchronous_standby_names = '*'
hot_standby = on
log_filename = 'postgresql-%Y%m%d_%H%M.log'

# pg_hba.conf配置
host    replication     barman            0.0.0.0/0               md5
host    replication     streaming_barman  0.0.0.0/0               md5

# 创建用户
su postgres
createuser -s -P barman
#设置密码123456
createuser -P --replication streaming_barman
#设置密码123456

# 授权
GRANT EXECUTE ON FUNCTION pg_start_backup(text, boolean, boolean) to barman;
GRANT EXECUTE ON FUNCTION pg_stop_backup() to barman;
GRANT EXECUTE ON FUNCTION pg_stop_backup(boolean, boolean) to barman;
GRANT EXECUTE ON FUNCTION pg_switch_wal() to barman;
GRANT EXECUTE ON FUNCTION pg_create_restore_point(text) to barman;

GRANT pg_read_all_settings TO barman;
GRANT pg_read_all_stats TO barman;

3.2 barman 配置

3.2.1 配置公共文件

# 修改barman的公共配置 /etc/barman.conf  文件位置固定 
参考 : /home/postgres/barman-2.18/doc/barman.conf
vi  /etc/barman.conf
#如 修改以下配置
[barman]
barman_user = postgres
configuration_files_directory = /etc/barman.d
barman_home = /home/postgres/barman-2.18
log_file = /home/postgres/barman-2.18/barman.log
log_level = INFO
compression = gzip

[streaming-pg]
description =  "Example of PostgreSQL Database (Streaming-Only)"
conninfo = host=pg user=barman dbname=postgres
streaming_conninfo = host=pg user=streaming_barman
backup_method = postgres
streaming_archiver = on
slot_name = barman

[ssh-pg]
description =  "Example of PostgreSQL Database (via Ssh)"
ssh_command = ssh postgres@pg
conninfo = host=pg user=barman dbname=postgres
backup_method = rsync
parallel_jobs = 1
reuse_backup = link
archiver = on
# 注意如果是本地备份,设置
backup_method = local-rsync
#流式备份
backup_method = postgres

#rsync使用/SSH备份
backup_method = rsync
ssh_command = ssh postgres@pg

3.2.2 目标库配置信息

每一个备份目标端生成一个配置文件
#  修改barman对postgresql server的私有配置 
# 参考:https://docs.pgbarman.org/release/2.18/#one-barman-many-postgresql-servers
mkdir -p  /etc/barman.d/
cp /home/postgres/barman-2.18/doc/barman.d/streaming-server.conf-template  /etc/barman.d/s2ahumysqlpg02.conf

#将pg.conf中的[streaming]修改为 /etc/hosts 里面指定的主机名 [s2ahumysqlpg02]
[root@s2ahumysqlpg01 barman.d]# cat pg.conf  | grep -v  ';'
[streaming]
description =  "Example of PostgreSQL Database (Streaming-Only)"
conninfo = host=pg user=barman dbname=postgres
streaming_conninfo = host=pg user=streaming_barman
backup_method = postgres
streaming_archiver = on
slot_name = barman
#修改后
cat  /etc/barman.d/s2ahumysqlpg02.conf
[s2ahumysqlpg02]
description =  "Example of PostgreSQL Database (Streaming-Only)"
conninfo = host=s2ahumysqlpg02 user=barman dbname=postgres
streaming_conninfo = host=s2ahumysqlpg02 user=streaming_barman
backup_method = postgres
streaming_archiver = on
slot_name = barman
archiver = on
create_slot = auto
streaming_archiver_name = barman_receive_wal
streaming_archiver_batch_size = 50
path_prefix = "/u01/postgresql/pg12/bin"


3.2.3 配置免密

# 在/etc/hosts 添加需要备份的目标库
192.168.1.55   s2ahumysqlpg01
192.168.1.56   s2ahumysqlpg02

#创建.pgpass
touch ~/.pgpass
chmod 0600 .pgpass

#添加
s2ahumysqlpg02:5432:*:streaming_barman:123456
s2ahumysqlpg02:5432:*:barman:123456

#更改权限
chmod 600 ~/.pgpass
#验证
psql -c 'SELECT version()' -U barman -h s2ahumysqlpg02 postgres

3.2.4 创建slot槽

postgres@s2ahumysqlpg01-> barman receive-wal --create-slot s2ahumysqlpg02
Creating physical replication slot 'barman' on server 's2ahumysqlpg02'
Replication slot 'barman' created

# 状态检查 
postgres@s2ahumysqlpg01->  barman check s2ahumysqlpg02
Server s2ahumysqlpg02:
        WAL archive: FAILED (please make sure WAL shipping is setup)
        PostgreSQL: OK
        superuser or standard user with backup privileges: OK
        PostgreSQL streaming: OK
        wal_level: OK
        replication slot: FAILED (slot 'barman' not initialised: is 'receive-wal' running?)
        directories: OK
        retention policy settings: OK
        backup maximum age: OK (no last_backup_maximum_age provided)
        backup minimum size: OK (0 B)
        wal maximum age: OK (no last_wal_maximum_age provided)
        wal size: OK (0 B)
        compression settings: OK
        failed backups: OK (there are 0 failed backups)
        minimum redundancy requirements: OK (have 0 backups, expected at least 0)
        pg_basebackup: OK
        pg_basebackup compatible: OK
        pg_basebackup supports tablespaces mapping: OK
        systemid coherence: OK (no system Id stored on disk)
        pg_receivexlog: OK
        pg_receivexlog compatible: OK
        receive-wal running: FAILED (See the Barman log file for more details)
        archive_mode: OK
        archive_command: OK
        continuous archiving: OK
        archiver errors: OK

# 问题1.1:可以关注到一个错误:
replication slot: FAILED (slot ‘pgsql_streaming’ not initialised: is ‘receive-wal’ running?)
解决该错误,我们需要执行以下命令,让barman服务器来强制接受源端的wal日志:
 nohup barman receive-wal s2ahumysqlpg02 &

# 问题 1.2 当恢复后需要重设
postgres@s2ahumysqlpg01-> barman receive-wal s2ahumysqlpg02 
Starting receive-wal for server s2ahumysqlpg02
s2ahumysqlpg02: pg_receivewal: starting log streaming at 0/44000000 (timeline 2)
s2ahumysqlpg02: pg_receivewal: error: could not send replication command "START_REPLICATION": ERROR:  requested starting point 0/44000000 on timeline 2 is not in this server's history
s2ahumysqlpg02: DETAIL:  This server's history forked from timeline 2 at 0/42000000.
s2ahumysqlpg02: pg_receivewal: error: disconnected
ERROR: ArchiverFailure:pg_receivewal terminated with error code: 1
# 重设
postgres@s2ahumysqlpg01->  barman receive-wal --reset  s2ahumysqlpg02
Resetting receive-wal directory status
Removing status file /u01/postgresql/backup/s2ahumysqlpg02/streaming/000000020000000000000044.partial
Creating status file /u01/postgresql/backup/s2ahumysqlpg02/streaming/000000030000000000000045.partial


# 问题2:如果发现,错误只有以下内容:
WAL archive: FAILED (please make sure WAL shipping is setup)
archive_mode: FAILED (please set it to ‘on’ or ‘always’)
archive_command: FAILED (please set it accordingly to documentation)
这三个错误是通过ssh/rsync的方式,如果我们只用streaming的方式可以忽略,然后直接发起备份,当然如果想要处理这些问题,可以这样做:
## 问题2.1 
WAL archive: FAILED (please make sure WAL shipping is setup) 片
解决:

1.执行: barman cron
           barman cron
2.执行barman switch-xlog
          barman switch-xlog  s2ahumysqlpg02

## 问题2.2 
archive_mode: FAILED (please set it to ‘on’ or ‘always’)

根据提示在server端设置archive_mod和archive_command参数:
archive_mod = on
archive_command = 'rsync -a %p barman@barman:/home/barman/pgsql_streaming/incoming/%f' 
#该路径就是我们刚在barman show-server pg_streaming中查询的
# 在次检查状态
postgres@s2ahumysqlpg01->  barman check s2ahumysqlpg02
Server s2ahumysqlpg02:
        PostgreSQL: OK
        superuser or standard user with backup privileges: 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)
        backup minimum size: OK (0 B)
        wal maximum age: OK (no last_wal_maximum_age provided)
        wal size: OK (0 B)
        compression settings: OK
        failed backups: OK (there are 0 failed backups)
        minimum redundancy requirements: OK (have 0 backups, expected at least 0)
        pg_basebackup: OK
        pg_basebackup compatible: OK
        pg_basebackup supports tablespaces mapping: OK
        systemid coherence: OK (no system Id stored on disk)
        pg_receivexlog: OK
        pg_receivexlog compatible: OK
        receive-wal running: OK
        archive_mode: OK
        archive_command: OK
        continuous archiving: OK
        archiver errors: OK

4.备份

4.1 备份

# 注意并行  仅适用于通过rsync
/SSH 配置的服务器  加参数   --jobs 4  如 :   barman backup --jobs 4 s2ahumysqlpg02

# 这里是通过流复制的方式备份如下
postgres@s2ahumysqlpg01-> barman backup s2ahumysqlpg02
Starting backup using postgres method for server s2ahumysqlpg02 in /home/postgres/barman-2.18/s2ahumysqlpg02/base/20220223T164800
Backup start at LSN: 0/3C000060 (00000002000000000000003C, 00000060)
Starting backup copy via pg_basebackup for 20220223T164800
WARNING: pg_basebackup does not copy the PostgreSQL configuration files that reside outside PGDATA. Please manually backup the following files:
        /var/lib/pgsql/tmp/recovery.conf
        /var/lib/pgsql/tmp/rep_mode.conf

Copy done (time: 13 seconds)
Finalising the backup.
This is the first backup for server s2ahumysqlpg02
Backup size: 510.5 MiB
Backup end at LSN: 0/3E000000 (00000002000000000000003D, 00000000)
Backup completed (start time: 2022-02-23 16:48:00.671864, elapsed time: 13 seconds)
Processing xlog segments from streaming for s2ahumysqlpg02
        00000002000000000000003B
        00000002000000000000003C
WARNING: IMPORTANT: this backup is classified as WAITING_FOR_WALS, meaning that Barman has not received yet all the required WAL files for the backup consistency.
This is a common behaviour in concurrent backup scenarios, and Barman automatically set the backup as DONE once all the required WAL files have been archived.
Hint: execute the backup command with '--wait'

4.2 查看备份集

# 查看备份集
postgres@s2ahumysqlpg01-> barman list-backup s2ahumysqlpg02
s2ahumysqlpg02 20220223T164800 - Wed Feb 23 16:48:13 2022 - Size: 510.5 MiB - WAL Size: 0 B - WAITING_FOR_WALS

# 查看 备份详细信息
postgres@s2ahumysqlpg01-> barman show-backup s2ahumysqlpg02  20220223T164800
Backup 20220223T164800:
  Server Name            : s2ahumysqlpg02
  System Id              : 7034328698707118780
  Status                 : WAITING_FOR_WALS
  PostgreSQL Version     : 120004
  PGDATA directory       : /u01/postgresql/data

  Base backup information:
    Disk usage           : 510.5 MiB (510.5 MiB with WALs)
    Incremental size     : 510.5 MiB (-0.00%)
    Timeline             : 2
    Begin WAL            : 00000002000000000000003D
    End WAL              : 00000002000000000000003D
    WAL number           : 0
    Begin time           : 2022-02-23 16:48:00.668043+08:00
    End time             : 2022-02-23 16:48:13.763332+08:00
    Copy time            : 13 seconds
    Estimated throughput : 39.0 MiB/s
    Begin Offset         : 40
    End Offset           : 0
    Begin LSN           : 0/3D000028
    End LSN             : 0/3E000000

  WAL information:
    No of files          : 0
    Disk usage           : 0 B
    Last available       : None

  Catalog information:
    Retention Policy     : not enforced
    Previous Backup      : - (this is the oldest base backup)
    Next Backup          : - (this is the latest base backup)


# 实时查看备份情况:
postgres@s2ahumysqlpg01->  barman status s2ahumysqlpg02
Server s2ahumysqlpg02:
        Description: Example of PostgreSQL Database (Streaming-Only)
        Active: True
        Disabled: False
        PostgreSQL version: 12.4
        Cluster state: in production
        pgespresso extension: Not available
        Current data size: 84.5 MiB
        PostgreSQL Data directory: /u01/postgresql/data
        Current WAL segment: 00000002000000000000003E
        PostgreSQL 'archive_command' setting: test ! -f /u01/postgresql/archive/%f && cp %p /u01/postgresql/archive/%f
        Last archived WAL: 00000002000000000000003D.00000028.backup, at Wed Feb 23 16:48:11 2022
        Failures of WAL archiver: 0
        Server WAL archiving rate: 0.01/hour
        Passive node: False
        Retention policies: not enforced
        No. of available backups: 0
        First available backup: 20220223T164800
        Last available backup: 20220223T164800
        Minimum redundancy requirements: satisfied (0/0)

# 查看 复制信息
postgres@s2ahumysqlpg01-> barman replication-status s2ahumysqlpg02
Status of streaming clients for server 's2ahumysqlpg02':
  Current LSN on master: 0/47000060
  Number of streaming clients: 2

  1. Async WAL streamer
     Application name: barman_receive_wal
     Sync stage      : 3/3 Remote write
     Communication   : TCP/IP
     IP Address      : 192.168.1.55 / Port: 46900 / Host: -
     User name       : streaming_barman
     Current state   : streaming (async)
     Replication slot: barman
     WAL sender PID  : 27763
     Started at      : 2022-02-23 18:14:37.223449+08:00
     Sent LSN   : 0/47000060 (diff: 0 B)
     Write LSN  : 0/47000060 (diff: 0 B)
     Flush LSN  : 0/47000000 (diff: -96 B)

  2. Async WAL streamer
     Application name: barman_streaming_backup
     Sync stage      : 1/3 1-safe
     Communication   : TCP/IP
     IP Address      : 192.168.1.55 / Port: 46910 / Host: -
     User name       : streaming_barman
     Current state   : backup (async)
     WAL sender PID  : 27784
     Started at      : 2022-02-23 18:14:54.462040+08:00

# 查看连接服务的信息
postgres@s2ahumysqlpg01-> barman show-server  s2ahumysqlpg02
Server s2ahumysqlpg02:
        active: True
        archive_command: test ! -f /u01/postgresql/archive/%f && cp %p /u01/postgresql/archive/%f
        archive_mode: on
        archive_timeout: 0
        archived_count: 9
        archiver: True
        archiver_batch_size: 0
        backup_directory: /u01/postgresql/backup/s2ahumysqlpg02
        backup_method: postgres
        backup_options: BackupOptions(['concurrent_backup'])
        bandwidth_limit: None
        barman_home: /u01/postgresql/backup
        barman_lock_directory: /u01/postgresql/backup
        basebackup_retry_sleep: 30
        basebackup_retry_times: 0
        basebackups_directory: /u01/postgresql/backup/s2ahumysqlpg02/base
        check_timeout: 30
        checkpoint_timeout: 300
        compression: gzip
        config_file: /u01/postgresql/data/postgresql.conf
        connection_error: None
        conninfo: host=s2ahumysqlpg02 user=barman dbname=postgres
        create_slot: auto
        current_archived_wals_per_second: 6.81142066026e-06
        current_lsn: 0/410000C8
        current_size: 88627323
        current_xlog: 000000020000000000000041
        custom_compression_filter: None
        custom_compression_magic: None
        custom_decompression_filter: None
        data_checksums: off
        data_directory: /u01/postgresql/data
        description: Example of PostgreSQL Database (Streaming-Only)
        disabled: False
        errors_directory: /u01/postgresql/backup/s2ahumysqlpg02/errors
        failed_count: 0
        forward_config_path: False
        has_backup_privileges: True
        hba_file: /u01/postgresql/data/pg_hba.conf
        hot_standby: on
        ident_file: /u01/postgresql/data/pg_ident.conf
        immediate_checkpoint: False
        included_files: ['/u01/postgresql/data/postgresql.auto.conf', '/var/lib/pgsql/tmp/recovery.conf', '/var/lib/pgsql/tmp/rep_mode.conf']
        incoming_wals_directory: /u01/postgresql/backup/s2ahumysqlpg02/incoming
        is_archiving: True
        is_in_recovery: False
        is_superuser: True
        last_archived_time: 2022-02-23 17:04:17.828209+08:00
        last_archived_wal: 000000020000000000000040.00000028.backup
        last_backup_maximum_age: None
        last_backup_minimum_size: None
        last_failed_time: None
        last_failed_wal: None
        last_wal_maximum_age: None
        max_incoming_wals_queue: None
        max_replication_slots: 10
        max_wal_senders: 10
        minimum_redundancy: 0
        msg_list: []
        name: s2ahumysqlpg02
        network_compression: False
        parallel_jobs: 1
        passive_node: False
        path_prefix: /u01/postgresql/pg12/bin
        pg_basebackup_bwlimit: True
        pg_basebackup_compatible: True
        pg_basebackup_installed: True
        pg_basebackup_path: /u01/postgresql/pg12/bin/pg_basebackup
        pg_basebackup_tbls_mapping: True
        pg_basebackup_version: 12.4
        pg_receivexlog_compatible: True
        pg_receivexlog_installed: True
        pg_receivexlog_path: /u01/postgresql/pg12/bin/pg_receivewal
        pg_receivexlog_supports_slots: True
        ~~~~~~~~~~~~~~~~
        recovery_options: RecoveryOptions([])
        replication_slot: Record(slot_name='barman', active=True, restart_lsn='0/41000000')
        replication_slot_support: True
        retention_policy: None
        retention_policy_mode: auto
        reuse_backup: None
        server_txt_version: 12.4
        slot_name: barman
        ssh_command: None
        stats_reset: 2022-02-08 10:03:42.679836+08:00
        streaming: True
        streaming_archiver: True
        streaming_archiver_batch_size: 50
        streaming_archiver_name: barman_receive_wal
        streaming_backup_name: barman_streaming_backup
        streaming_conninfo: host=s2ahumysqlpg02 user=streaming_barman
        streaming_supported: True
        streaming_systemid: 7034328698707118780
        streaming_wals_directory: /u01/postgresql/backup/s2ahumysqlpg02/streaming
        synchronous_standby_names: ['']
        tablespace_bandwidth_limit: None
        timeline: 2
        wal_compression: off
        wal_keep_segments: 1000
        wal_level: replica
        wal_retention_policy: main
        wals_directory: /u01/postgresql/backup/s2ahumysqlpg02/wals
        xlog_segment_size: 16777216
        xlogpos: 0/410000C8

4.3 备份文件

说明:
    配置文件 barman.conf 中的barman_home,即为备份存放路径,打开barman_home下对应的备份服务下,可以看到相关目录:

    其中,比较重要的有wals、base、streaming、incoming

    base中,存放的是数据库的基本备份文件(最近的一份全备)
    streaming中,存放的是实时传过来的wal文件
    incoming中,存放的是主库传输过来的归档文件
    wals中,是所有的归档文件,可以根据这个和基础备份恢复到任意时间点 

    postgres@s2ahumysqlpg01-> cd s2ahumysqlpg02
    postgres@s2ahumysqlpg01-> ll
    total 4.0K
    drwxrwxr-x. 3 postgres postgres 29 Feb 23 16:48 base
    drwxrwxr-x. 2 postgres postgres  6 Feb 23 16:20 errors
    -rw-rw-r--. 1 postgres postgres 64 Feb 23 16:48 identity.json
    drwxrwxr-x. 2 postgres postgres  6 Feb 23 16:20 incoming
    drwxrwxr-x. 2 postgres postgres 78 Feb 23 16:48 streaming
    drwxrwxr-x. 2 postgres postgres 45 Feb 23 16:48 wals

5.恢复

# 查看拥有的备份集:
 postgres@s2ahumysqlpg01-> barman list-backup s2ahumysqlpg02
s2ahumysqlpg02 20220223T170806 - Wed Feb 23 17:08:14 2022 - Size: 510.8 MiB - WAL Size: 0 B - WAITING_FOR_WALS
s2ahumysqlpg02 20220223T170405 - Wed Feb 23 17:04:19 2022 - Size: 510.8 MiB - WAL Size: 16.1 KiB - WAITING_FOR_WALS

# 检查选定的备份的详细信息: 
postgres@s2ahumysqlpg01-> barman show-backup s2ahumysqlpg02 20220223T170405
Backup 20220223T170405:
  Server Name            : s2ahumysqlpg02
  System Id              : 7034328698707118780
  Status                 : WAITING_FOR_WALS
  PostgreSQL Version     : 120004
  PGDATA directory       : /u01/postgresql/data

  Base backup information:
    Disk usage           : 510.8 MiB (510.8 MiB with WALs)
    Incremental size     : 510.8 MiB (-0.00%)
    Timeline             : 2
    Begin WAL            : 000000020000000000000040
    End WAL              : 000000020000000000000040
    WAL number           : 1
    WAL compression ratio: 99.90%
    Begin time           : 2022-02-23 17:04:05.607850+08:00
    End time             : 2022-02-23 17:04:19.679822+08:00
    Copy time            : 14 seconds
    Estimated throughput : 36.3 MiB/s
    Begin Offset         : 40
    End Offset           : 0
    Begin LSN           : 0/40000028
    End LSN             : 0/41000000

  WAL information:
    No of files          : 1
    Disk usage           : 16.1 KiB
    WAL rate             : 31.46/hour
    Compression ratio    : 99.90%
    Last available       : 000000020000000000000041

  Catalog information:
    Retention Policy     : not enforced
    Previous Backup      : - (this is the oldest base backup)
    Next Backup          : 20220223T170806

# 恢复到远端数据库 ,节点间需做ssh 互信,做免密通信
postgres@s2ahumysqlpg01-> barman recover s2ahumysqlpg02 20220223T170405 /u01/postgresql/data --remote-ssh-command "ssh postgres@s2ahumysqlpg02" --target-time "2022-02-23 17:04:19.679822+08:00"
postgres@s2ahumysqlpg02's password: 
Starting remote restore for server s2ahumysqlpg02 using backup 20220223T170405
Destination directory: /u01/postgresql/data
Remote command: sshpostgres@s2ahumysqlpg02
WARNING: IMPORTANT: You have requested a recovery operation for a backup that does not have yet all the WAL files that are required for consistency.
Doing PITR. Recovery target time: '2022-02-23 17:04:19.679822+08:00'
Copying the base backup.
WARNING: IMPORTANT: The backup we have recovered IS NOT VALID. Required WAL files for consistency are missing. Please verify that WAL archiving is working correctly or evaluate using the 'get-wal' option for recovery
Copying required WAL segments.
Identify dangerous settings in destination directory.
IMPORTANT
These settings have been modified to prevent data losses
postgresql.conf line 758: archive_command = false

WARNING
You are required to review the following options as potentially dangerous
postgresql.conf line 781: include = '/var/lib/pgsql/tmp/recovery.conf' # added by pgsql RA
postgresql.conf line 782: include = '/var/lib/pgsql/tmp/rep_mode.conf' # added by pgsql RA
Recovery completed (start time: 2022-02-23 18:01:41.227264, elapsed time: 40 seconds)
Your PostgreSQL server has been successfully prepared for recovery!

#当出现has been successfully则表示恢复成功。

6.常用命令

6.1.归档

#开始日志循环备份 
  nohup barman receive-wal s2ahumysqlpg02 &

#从新的日志开始接收
   barman receive-wal --reset  s2ahumysqlpg02

#在服务器基础上同时执行 WAL 归档操作    
   barman cron
#日志切换
  barman switch-xlog  s2ahumysqlpg02
  barman switch-wal   <server_name>
#归档检查 
 barman switch-wal --force --archive s2ahumysqlpg02     
#  验证 WAL 归档配置
postgres@s2ahumysqlpg01->  barman switch-wal --force --archive s2ahumysqlpg02 
The WAL file 000000030000000000000049 has been closed on server 's2ahumysqlpg02'
Waiting for the WAL file 000000030000000000000049 from server 's2ahumysqlpg02' (max: 30 seconds)
Processing xlog segments from streaming for s2ahumysqlpg02
        000000030000000000000047
        000000030000000000000048
Processing xlog segments from streaming for s2ahumysqlpg02
        000000030000000000000049

6.2.查看

#备份环境检查 
  barman check s2ahumysqlpg02

#显示 PostgreSQL 服务器或所有服务器的实时信息和状态
barman status <server_name>

#显示给定服务器的配置参数:
barman show-servers <server_name>

# 查看 复制信息
  barman replication-status s2ahumysqlpg02

6.3.备份

barman backup s2ahumysqlpg02

6.4.恢复

#recover命令用于在使用该backup命令执行备份后恢复整个服务器。
barman recover <server_name> <backup_id> /path/to/recover/dir

# 远程恢复示例
barman recover s2ahumysqlpg02 20220223T170405 /u01/postgresql/data --remote-ssh-command "ssh postgres@s2ahumysqlpg02" --target-time "2022-02-23 17:04:19.679822+08:00"

6.5.备份集查看

#查看备份集
  barman list-backup s2ahumysqlpg02 

#查看备份详细信息
  barman show-backup s2ahumysqlpg02  20220223T164800

# 实时查看备份情况:
  barman status s2ahumysqlpg02

6.6.保留策略

PostgreSQL 定期基础备份:通过retention_policy配置选项
retention_policy = {REDUNDANCY value | RECOVERY WINDOW OF value {DAYS | WEEKS | MONTHS}}

存档日志,用于时间点恢复:通过wal_retention_policy配置选项
wal_retention_policy选项限制为main.

保留超出服务器保留策略的备份
barman keep <server_name> <backup_id> [--target TARGET, --status, --release]

6.7.删除备份集

#除目录中可用的最旧备份并回收磁盘空间。
barman delete <server_name> oldest

    last/latest: 标识目录中的最新备份
    first/oldest:标识目录中最旧的备份
    last-failed:标识目录中最新的失败备份

#删除给定的备份:
barman delete <server_name> <backup_id>

6.8.查看目标机器

 barman list-servers
 barman list-servers --minimal
postgres@s2ahumysqlpg01-> barman list-servers
WARNING: No backup strategy set for server 'ssh-pg' (using default 'exclusive_backup').
WARNING: The default backup strategy will change to 'concurrent_backup' in the future. Explicitly set 'backup_options' to silence this warning.
s2ahumysqlpg02 - Example of PostgreSQL Database (Streaming-Only)
ssh-pg - Example of PostgreSQL Database (via Ssh)
streaming - Example of PostgreSQL Database (Streaming-Only)
streaming-pg - Example of PostgreSQL Database (Streaming-Only)

postgres@s2ahumysqlpg01-> barman list-servers --minimal
WARNING: No backup strategy set for server 'ssh-pg' (using default 'exclusive_backup').
WARNING: The default backup strategy will change to 'concurrent_backup' in the future. Explicitly set 'backup_options' to silence this warning.
s2ahumysqlpg02
ssh-pg
streaming
streaming-pg

参考

https://blog.csdn.net/wx370092877/article/details/110870605
https://docs.pgbarman.org/release/2.18/#barman-client-utilities-for-the-cloud-barman-cli-cloud
https://github.com/EnterpriseDB/barman





posted @ 2022-02-23 19:33  www.cqdba.cn  阅读(663)  评论(0编辑  收藏  举报