暂无图片
暂无图片
3
暂无图片
暂无图片
暂无图片

patroni 使用 pgbackrest 代替 pg_basebackup

原创 黄山谷 2025-12-24
484

pg的 patroni+DCS 架构中,也可以用 pgbackrest 代替 pg_basebackup

看了 percona 官网也是使用了 pgbackrest 作为 PostgreSQL数据库设计的高级备份和恢复工具。

这个大家可以参考:

pgBackRest 设置 - Percona PostgreSQL 发行版
https://docs.percona.com/postgresql/15/solutions/pgbackrest.html#configure-database-servers

pgbackrest的安装我这里就不介绍了
可以参考 之前的文章:https://www.modb.pro/db/2003488856398716928

1. 配置 pgbackrest

我这个是测试环境,限于存储空间极限,生产大家备份参数、目录根据自己需求设置

[postgres@node83 ~]$ cat /etc/pgbackrest/pgbackrest.conf 
[global]
repo1-path=/home/postgres/pgbackup
repo1-retention-full=1          # 保留完整备份数量
repo1-retention-diff=6          # 保留增量备份数量,差异和增量共享参数
repo1-retention-archive=7       # WAL归档保留天数
repo1-host-user=sysdba
compress-type=gz                # 使用gzip压缩算法
compress-level=6                # gzip压缩级别,(1-9,6是gzip)
log-subprocess=y                # 记录子进程日志
log-level-console=info          # 控制台日志级别
log-level-file=debug            # 文件日志级别
archive-timeout=600
start-fast=y                    # 跳过初始检查,快速开始
link-all=y                      # 使用硬链接优化存储
delta=y                         # 启用差异检测
process-max=4                   # 最大并行进程数
# 添加以下参数
archive-async=y                 # 启用异步WAL归档
spool-path=/home/postgres/pgbackrest   # 异步归档临时目录
archive-check=y                 # 验证归档完整性
archive-copy=y                  # 复制而非移动WAL文件

# 集群配置(所有节点使用相同的stanza名称)
[cndb]                            
pg1-path=/home/postgres/pgdata  # 下面几个数据库的配置
pg1-port=1622
pg1-user=sysdba
pg1-database=postgres

[global:archive-push]
compress-level=3                # 归档压缩

每一个数据库节点都需要,因为每个节点都可能成为主库,另外我还是觉得备份归档到本地比较稳妥点。

我这里pg 三个节点,patroni + consul ,etcd 配置也是一样的

[postgres@node83 ~]$ inctl list
+ Cluster: cndb (7587033323897876858) ----+-----------+----+-----------+
| Member | Host            | Role         | State     | TL | Lag in MB |
+--------+-----------------+--------------+-----------+----+-----------+
| node83 | 10.10.3.83:1622 | Leader       | running   | 14 |           |
| node84 | 10.10.3.84:1622 | Sync Standby | streaming | 14 |         0 |
| node85 | 10.10.3.85:1622 | Replica      | streaming | 14 |         0 |
+--------+-----------------+--------------+-----------+----+-----------+

2. patroni 需要修改的配置

bootstrap:
  postgresql:
    parameters:
      cluster_name: "cndb"  # 这个要和 stanza 相同
      archive_mode: "on"
      archive_command: "pgbackrest --stanza=cndb archive-push %p" 
    
    parameters:
      port: 1622            # 这里端口在指定下
      listen_addresses: '*'
  # 添加 pgBackRest 恢复配置
    recovery_conf:
    restore_command: 'pgbackrest --stanza=cndb archive-get %f "%p"'    

下面是我的完整patroni 的配置文件

[postgres@node83 ~]$ cat /home/postgres/app/patroni/patroni_config.yml
scope: cndb
namespace: /service/
name: node83

log:
  level: INFO
  traceback_level: ERROR
  dir: /home/postgres/patroni_log
  file_num: 10
  file_size: 104857600
  
restapi:
  listen: 10.10.3.83:8010
  connect_address: 10.10.3.83:8010

consul:
  hosts:
    - 10.10.3.83:8500
    - 10.10.3.84:8500
    - 10.10.3.85:8500

bootstrap:
  dcs:
    ttl: 30
    loop_wait: 10
    retry_timeout: 10
    maximum_lag_on_failover: 1048576
    max_timelines_history: 0
    master_start_timeout: 300
    master_stop_timeout: 0
    synchronous_mode: false
    leader_key: /service/cndb/leader
    postgresql:
      use_pg_rewind: true
      use_slots: true
      parameters:
        max_connections: 2000
        superuser_reserved_connections: 100
        max_locks_per_transaction: 64
        max_worker_processes: 2
        max_prepared_transactions: 0
        wal_level: replica
        wal_log_hints: on
        track_commit_timestamp: off
        max_wal_senders: 32
        max_replication_slots: 10
        wal_keep_size: 4096MB
        hot_standby: "on"
        hot_standby_feedback: "on"
        listen_addresses: "*"
        port: 1622
        cluster_name: "cndb"
        archive_mode: "on"
        archive_command: "pgbackrest --stanza=cndb archive-push %p"
        archive_timeout: 1800s

postgresql:
  listen: 0.0.0.0:1622
  connect_address: 10.10.3.83:1622
  bin_dir: /home/postgres/app/postgresql/bin
  data_dir: /home/postgres/pgdata
  config_dir: /home/postgres/pgdata
  pgpass: /home/postgres/.pgpass
  pg_ctl_timeout: 60
  use_pg_rewind: true
  remove_data_directory_on_rewind_failure: false
  remove_data_directory_on_diverged_timelines: true
  
  parameters:
    port: 1622
    listen_addresses: '*'
  # 添加 pgBackRest 恢复配置
  recovery_conf:
    restore_command: 'pgbackrest --stanza=cndb archive-get %f "%p"'

  authentication:
    replication:
      username: repuser
      password: 'passwd'
    superuser:
      username: sysdba
      password: 'passwd'

tags:
  nofailover: false
  noloadbalance: false
  clonefrom: false
  nosync: false

3. 设置下主机互信,没台机器都要执行

ssh-keygen
ssh-copy-id postgres@10.10.3.83
ssh-copy-id postgres@10.10.3.84
ssh-copy-id postgres@10.10.3.85
ssh postgres@10.10.3.83
ssh postgres@10.10.3.84
ssh postgres@10.10.3.85


ssh-keygen
ssh-copy-id postgres@10.10.3.83
ssh-copy-id postgres@10.10.3.84
ssh-copy-id postgres@10.10.3.85
ssh postgres@10.10.3.83
ssh postgres@10.10.3.84
ssh postgres@10.10.3.85


ssh-keygen
ssh-copy-id postgres@10.10.3.83
ssh-copy-id postgres@10.10.3.84
ssh-copy-id postgres@10.10.3.85
ssh postgres@10.10.3.83
ssh postgres@10.10.3.84
ssh postgres@10.10.3.85

修改完配置后重启服务

sudo systemctl daemon-reload
sudo systemctl restart patroni

4. 测试归档

# 创建 stanza pgbackrest --stanza=cndb --log-level-console=info stanza-create # 检查备份配置 pgbackrest --stanza=cndb --log-level-console=info check # 看下能不能正常归档

image.png

5.查看集群状态,

测试1 :尝试 switchover 验证
[postgres@node83 ~]$ inctl switchover
Current cluster topology
+ Cluster: cndb (7587033323897876858) ----+-----------+----+-----------+
| Member | Host            | Role         | State     | TL | Lag in MB |
+--------+-----------------+--------------+-----------+----+-----------+
| node83 | 10.10.3.83:1622 | Leader       | running   | 14 |           |
| node84 | 10.10.3.84:1622 | Sync Standby | streaming | 14 |         0 |
| node85 | 10.10.3.85:1622 | Replica      | streaming | 14 |         0 |
+--------+-----------------+--------------+-----------+----+-----------+
Primary [node83]: 
Candidate ['node84', 'node85'] []: 
When should the switchover take place (e.g. 2025-12-24T19:39 )  [now]: 
Are you sure you want to switchover cluster cndb, demoting current leader node83? [y/N]: y
2025-12-24 18:39:29.17203 Successfully switched over to "node84"
+ Cluster: cndb (7587033323897876858) -----------+----+-----------+
| Member | Host            | Role    | State     | TL | Lag in MB |
+--------+-----------------+---------+-----------+----+-----------+
| node83 | 10.10.3.83:1622 | Replica | stopped   |    |   unknown |
| node84 | 10.10.3.84:1622 | Leader  | running   | 14 |           |
| node85 | 10.10.3.85:1622 | Replica | streaming | 14 |         0 |
+--------+-----------------+---------+-----------+----+-----------+
[postgres@node83 ~]$ inctl list
+ Cluster: cndb (7587033323897876858) ---------------------+----+-----------+
| Member | Host            | Role    | State               | TL | Lag in MB |
+--------+-----------------+---------+---------------------+----+-----------+
| node83 | 10.10.3.83:1622 | Replica | stopped             |    |   unknown |
| node84 | 10.10.3.84:1622 | Leader  | running             | 15 |           |
| node85 | 10.10.3.85:1622 | Replica | in archive recovery | 14 |         0 |
+--------+-----------------+---------+---------------------+----+-----------+
[postgres@node83 ~]$ inctl list
+ Cluster: cndb (7587033323897876858) ----+-----------+----+-----------+
| Member | Host            | Role         | State     | TL | Lag in MB |
+--------+-----------------+--------------+-----------+----+-----------+
| node83 | 10.10.3.83:1622 | Replica      | streaming | 15 |         0 |
| node84 | 10.10.3.84:1622 | Leader       | running   | 15 |           |
| node85 | 10.10.3.85:1622 | Sync Standby | streaming | 15 |         0 |
+--------+-----------------+--------------+-----------+----+-----------+

可以看到节点正常切换,详细的日志

2025-12-24 18:39:27.859 P00   INFO: pushed WAL file '0000000E0000000000000062' to the archive asynchronously
2025-12-24 18:39:27.860 P00   INFO: archive-push command end: completed successfully (628ms)
2025-12-24 18:39:27 CST::@:[21407]:LOG:  database system is shut down
2025-12-24 18:39:30 CST::@:[22686]:LOG:  starting PostgreSQL 16.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 11.5.0 20240719 (Red Hat 11.5.0-2), 64-bit
2025-12-24 18:39:30 CST::@:[22686]:LOG:  listening on IPv4 address "0.0.0.0", port 1622
2025-12-24 18:39:30 CST::@:[22686]:LOG:  listening on Unix socket "./.s.PGSQL.1622"
2025-12-24 18:39:30 CST::@:[22691]:LOG:  database system was shut down at 2025-12-24 18:39:27 CST
2025-12-24 18:39:30.684 P00   INFO: archive-get command begin 2.54.2: [0000000F.history, pg_wal/RECOVERYHISTORY] --archive-async --archive-timeout=600 --exec-id=22692-87235877 --log-level-console=info --log-level-file=debug --log-subprocess --pg1-path=/home/postgres/pgdata --process-max=4 --repo1-path=/home/postgres/pgbackup --spool-path=/home/postgres/pgbackrest --stanza=cndb
2025-12-24 18:39:30.685 P00   INFO: unable to find 0000000F.history in the archive
2025-12-24 18:39:30.685 P00   INFO: archive-get command end: completed successfully (3ms)
2025-12-24 18:39:30 CST::@:[22691]:LOG:  entering standby mode
2025-12-24 18:39:30.692 P00   INFO: archive-get command begin 2.54.2: [0000000E.history, pg_wal/RECOVERYHISTORY] --archive-async --archive-timeout=600 --exec-id=22693-6801992e --log-level-console=info --log-level-file=debug --log-subprocess --pg1-path=/home/postgres/pgdata --process-max=4 --repo1-path=/home/postgres/pgbackup --spool-path=/home/postgres/pgbackrest --stanza=cndb
2025-12-24 18:39:30.693 P00   INFO: found 0000000E.history in the repo1: 16-1 archive
2025-12-24 18:39:30.693 P00   INFO: archive-get command end: completed successfully (2ms)
2025-12-24 18:39:30 CST::@:[22691]:LOG:  restored log file "0000000E.history" from archive
2025-12-24 18:39:30.703 P00   INFO: archive-get command begin 2.54.2: [0000000E0000000000000063, pg_wal/RECOVERYXLOG] --archive-async --archive-timeout=600 --exec-id=22694-ac4c9ba8 --log-level-console=info --log-level-file=debug --log-subprocess --pg1-path=/home/postgres/pgdata --process-max=4 --repo1-path=/home/postgres/pgbackup --spool-path=/home/postgres/pgbackrest --stanza=cndb
2025-12-24 18:39:30.805 P00   INFO: unable to find 0000000E0000000000000063 in the archive asynchronously
2025-12-24 18:39:30.805 P00   INFO: archive-get command end: completed successfully (104ms)
2025-12-24 18:39:30 CST::@:[22691]:LOG:  consistent recovery state reached at 0/630000A0
2025-12-24 18:39:30 CST::@:[22691]:LOG:  invalid record length at 0/630000A0: expected at least 24, got 0
2025-12-24 18:39:30 CST::@:[22686]:LOG:  database system is ready to accept read-only connections
2025-12-24 18:39:30 CST::@:[22697]:LOG:  fetching timeline history file for timeline 15 from primary server
2025-12-24 18:39:30 CST::@:[22697]:LOG:  started streaming WAL from primary at 0/63000000 on timeline 14
2025-12-24 18:39:30 CST::@:[22697]:LOG:  replication terminated by primary server
2025-12-24 18:39:30 CST::@:[22697]:DETAIL:  End of WAL reached on timeline 14 at 0/630000A0.
2025-12-24 18:39:30 CST::@:[22697]:FATAL:  terminating walreceiver process due to administrator command
2025-12-24 18:39:30.828 P00   INFO: archive-get command begin 2.54.2: [0000000F.history, pg_wal/RECOVERYHISTORY] --archive-async --archive-timeout=600 --exec-id=22698-8bed0cc3 --log-level-console=info --log-level-file=debug --log-subprocess --pg1-path=/home/postgres/pgdata --process-max=4 --repo1-path=/home/postgres/pgbackup --spool-path=/home/postgres/pgbackrest --stanza=cndb
2025-12-24 18:39:30.828 P00   INFO: unable to find 0000000F.history in the archive
2025-12-24 18:39:30.828 P00   INFO: archive-get command end: completed successfully (2ms)
2025-12-24 18:39:30.836 P00   INFO: archive-get command begin 2.54.2: [00000010.history, pg_wal/RECOVERYHISTORY] --archive-async --archive-timeout=600 --exec-id=22699-15838a46 --log-level-console=info --log-level-file=debug --log-subprocess --pg1-path=/home/postgres/pgdata --process-max=4 --repo1-path=/home/postgres/pgbackup --spool-path=/home/postgres/pgbackrest --stanza=cndb
2025-12-24 18:39:30.837 P00   INFO: unable to find 00000010.history in the archive
2025-12-24 18:39:30.837 P00   INFO: archive-get command end: completed successfully (2ms)
2025-12-24 18:39:30.846 P00   INFO: archive-get command begin 2.54.2: [0000000F.history, pg_wal/RECOVERYHISTORY] --archive-async --archive-timeout=600 --exec-id=22700-d3084c40 --log-level-console=info --log-level-file=debug --log-subprocess --pg1-path=/home/postgres/pgdata --process-max=4 --repo1-path=/home/postgres/pgbackup --spool-path=/home/postgres/pgbackrest --stanza=cndb
2025-12-24 18:39:30.846 P00   INFO: unable to find 0000000F.history in the archive
2025-12-24 18:39:30.847 P00   INFO: archive-get command end: completed successfully (3ms)
2025-12-24 18:39:30 CST::@:[22691]:LOG:  new target timeline is 15
2025-12-24 18:39:30.855 P00   INFO: archive-get command begin 2.54.2: [0000000F0000000000000063, pg_wal/RECOVERYXLOG] --archive-async --archive-timeout=600 --exec-id=22701-bb77b6bd --log-level-console=info --log-level-file=debug --log-subprocess --pg1-path=/home/postgres/pgdata --process-max=4 --repo1-path=/home/postgres/pgbackup --spool-path=/home/postgres/pgbackrest --stanza=cndb
2025-12-24 18:39:30.957 P00   INFO: unable to find 0000000F0000000000000063 in the archive asynchronously
2025-12-24 18:39:30.957 P00   INFO: archive-get command end: completed successfully (104ms)
2025-12-24 18:39:30.966 P00   INFO: archive-get command begin 2.54.2: [0000000E0000000000000063, pg_wal/RECOVERYXLOG] --archive-async --archive-timeout=600 --exec-id=22704-a2e7df6e --log-level-console=info --log-level-file=debug --log-subprocess --pg1-path=/home/postgres/pgdata --process-max=4 --repo1-path=/home/postgres/pgbackup --spool-path=/home/postgres/pgbackrest --stanza=cndb
2025-12-24 18:39:31.068 P00   INFO: unable to find 0000000E0000000000000063 in the archive asynchronously
2025-12-24 18:39:31.068 P00   INFO: archive-get command end: completed successfully (104ms)
2025-12-24 18:39:31 CST::@:[22707]:LOG:  started streaming WAL from primary at 0/63000000 on timeline 15
2025-12-24 18:39:31 CST::@:[22691]:LOG:  redo starts at 0/630000A0
测试2 :尝试删除节点83 的数据目录,可以看到可以重建备库。
[postgres@node83 pgdata]$ cd /home/postgres/pgdata/
[postgres@node83 pgdata]$ rm -rf *
+--------+-----------------+--------------+-----------+----+-----------+
[postgres@node83 pgdata]$ inctl list
+ Cluster: cndb (7587033323897876858) ----+-----------+----+-----------+
| Member | Host            | Role         | State     | TL | Lag in MB |
+--------+-----------------+--------------+-----------+----+-----------+
| node83 | 10.10.3.83:1622 | Replica      | stopped   |    |   unknown |
| node84 | 10.10.3.84:1622 | Leader       | running   | 15 |           |
| node85 | 10.10.3.85:1622 | Sync Standby | streaming | 15 |         0 |
+--------+-----------------+--------------+-----------+----+-----------+
[postgres@node83 pgdata]$ inctl list
+ Cluster: cndb (7587033323897876858) ----+-----------+----+-----------+
| Member | Host            | Role         | State     | TL | Lag in MB |
+--------+-----------------+--------------+-----------+----+-----------+
| node83 | 10.10.3.83:1622 | Replica      | streaming | 15 |         0 |
| node84 | 10.10.3.84:1622 | Leader       | running   | 15 |           |
| node85 | 10.10.3.85:1622 | Sync Standby | streaming | 15 |         0 |
+--------+-----------------+--------------+-----------+----+-----------+

日志恢复规程

2025-12-24 18:39:31 CST:127.0.0.1(9220):[unknown]@[unknown]:[22709]:LOG:  connection received: host=127.0.0.1 port=9220
2025-12-24 18:39:31 CST:127.0.0.1(9232):[unknown]@[unknown]:[22711]:LOG:  connection received: host=127.0.0.1 port=9232
2025-12-24 18:39:37 CST:127.0.0.1(54512):[unknown]@[unknown]:[22713]:LOG:  connection received: host=127.0.0.1 port=54512
2025-12-24 18:39:37 CST:127.0.0.1(54512):sysdba@postgres:[22713]:LOG:  connection authenticated: identity="sysdba" method=scram-sha-256 (/home/postgres/pgdata/pg_hba.conf:116)
2025-12-24 18:39:37 CST:127.0.0.1(54512):sysdba@postgres:[22713]:LOG:  connection authorized: user=sysdba database=postgres application_name=Patroni heartbeat
2025-12-24 18:44:27 CST::@:[22686]:LOG:  received immediate shutdown request
2025-12-24 18:44:27 CST::@:[22686]:LOG:  could not open file "postmaster.pid": No such file or directory
2025-12-24 18:44:27 CST::@:[22686]:LOG:  could not write file "pg_stat/pg_stat_statements.stat.tmp": No such file or directory
2025-12-24 18:44:27 CST::@:[22686]:LOG:  database system is shut down
2025-12-24 18:44:28 CST::@:[22744]:LOG:  starting PostgreSQL 16.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 11.5.0 20240719 (Red Hat 11.5.0-2), 64-bit
2025-12-24 18:44:28 CST::@:[22744]:LOG:  listening on IPv4 address "0.0.0.0", port 1622
2025-12-24 18:44:28 CST::@:[22744]:LOG:  listening on Unix socket "./.s.PGSQL.1622"
2025-12-24 18:44:28 CST::@:[22749]:LOG:  database system was interrupted; last known up at 2025-12-24 18:44:27 CST
2025-12-24 18:44:28.445 P00   INFO: archive-get command begin 2.54.2: [00000010.history, pg_wal/RECOVERYHISTORY] --archive-async --archive-timeout=600 --exec-id=22750-42f2a403 --log-level-console=info --log-level-file=debug --log-subprocess --pg1-path=/home/postgres/pgdata --process-max=4 --repo1-path=/home/postgres/pgbackup --spool-path=/home/postgres/pgbackrest --stanza=cndb
2025-12-24 18:44:28.445 P00   INFO: unable to find 00000010.history in the archive
2025-12-24 18:44:28.445 P00   INFO: archive-get command end: completed successfully (2ms)
2025-12-24 18:44:28 CST::@:[22749]:LOG:  entering standby mode
2025-12-24 18:44:28 CST::@:[22749]:LOG:  starting backup recovery with redo LSN 0/64000028, checkpoint LSN 0/64000060, on timeline ID 15
2025-12-24 18:44:28.454 P00   INFO: archive-get command begin 2.54.2: [0000000F.history, pg_wal/RECOVERYHISTORY] --archive-async --archive-timeout=600 --exec-id=22751-438368b8 --log-level-console=info --log-level-file=debug --log-subprocess --pg1-path=/home/postgres/pgdata --process-max=4 --repo1-path=/home/postgres/pgbackup --spool-path=/home/postgres/pgbackrest --stanza=cndb
2025-12-24 18:44:28.455 P00   INFO: unable to find 0000000F.history in the archive
2025-12-24 18:44:28.455 P00   INFO: archive-get command end: completed successfully (3ms)
2025-12-24 18:44:28.462 P00   INFO: archive-get command begin 2.54.2: [0000000F0000000000000064, pg_wal/RECOVERYXLOG] --archive-async --archive-timeout=600 --exec-id=22752-5d07da8d --log-level-console=info --log-level-file=debug --log-subprocess --pg1-path=/home/postgres/pgdata --process-max=4 --repo1-path=/home/postgres/pgbackup --spool-path=/home/postgres/pgbackrest --stanza=cndb
2025-12-24 18:44:28.565 P00   INFO: unable to find 0000000F0000000000000064 in the archive asynchronously
2025-12-24 18:44:28.565 P00   INFO: archive-get command end: completed successfully (104ms)
2025-12-24 18:44:28 CST::@:[22749]:LOG:  redo starts at 0/64000028
2025-12-24 18:44:28.577 P00   INFO: archive-get command begin 2.54.2: [0000000F0000000000000065, pg_wal/RECOVERYXLOG] --archive-async --archive-timeout=600 --exec-id=22755-221db05c --log-level-console=info --log-level-file=debug --log-subprocess --pg1-path=/home/postgres/pgdata --process-max=4 --repo1-path=/home/postgres/pgbackup --spool-path=/home/postgres/pgbackrest --stanza=cndb
2025-12-24 18:44:28.679 P00   INFO: unable to find 0000000F0000000000000065 in the archive asynchronously
2025-12-24 18:44:28.680 P00   INFO: archive-get command end: completed successfully (105ms)
2025-12-24 18:44:28 CST::@:[22749]:LOG:  completed backup recovery with redo LSN 0/64000028 and end LSN 0/64000138
2025-12-24 18:44:28 CST::@:[22749]:LOG:  consistent recovery state reached at 0/64000138
2025-12-24 18:44:28 CST::@:[22744]:LOG:  database system is ready to accept read-only connections
2025-12-24 18:44:28.689 P00   INFO: archive-get command begin 2.54.2: [0000000F0000000000000065, pg_wal/RECOVERYXLOG] --archive-async --archive-timeout=600 --exec-id=22758-37a080d7 --log-level-console=info --log-level-file=debug --log-subprocess --pg1-path=/home/postgres/pgdata --process-max=4 --repo1-path=/home/postgres/pgbackup --spool-path=/home/postgres/pgbackrest --stanza=cndb
2025-12-24 18:44:28.791 P00   INFO: unable to find 0000000F0000000000000065 in the archive asynchronously
2025-12-24 18:44:28.791 P00   INFO: archive-get command end: completed successfully (104ms)
2025-12-24 18:44:28 CST::@:[22761]:LOG:  started streaming WAL from primary at 0/65000000 on timeline 15

6. 测试备份

pgbackrest --stanza=cndb --log-level-console=info --start-fast --process-max=2 --archive-copy --type=full backup
pgbackrest info
pgbackrest --stanza=cndb --log-level-console=info --archive-copy --type=diff backup

全量备份
[postgres@node84 ~]$ pgbackrest --stanza=cndb --log-level-console=info --start-fast --process-max=2 --archive-copy --type=full backup
2025-12-24 18:48:25.401 P00   INFO: backup command begin 2.54.2: --archive-check --archive-copy --archive-timeout=600 --compress-level=6 --compress-type=gz --delta --exec-id=11755-9c265698 --log-level-console=info --log-level-file=debug --log-subprocess --pg1-database=postgres --pg1-path=/home/postgres/pgdata --pg1-port=1622 --pg1-user=sysdba --process-max=2 --repo1-path=/home/postgres/pgbackup --repo1-retention-archive=7 --repo1-retention-diff=6 --repo1-retention-full=1 --stanza=cndb --start-fast --type=full
2025-12-24 18:48:25.427 P00   INFO: execute non-exclusive backup start: backup begins after the requested immediate checkpoint completes
2025-12-24 18:48:25.788 P00   INFO: backup start archive = 0000000F0000000000000066, lsn = 0/66000060
2025-12-24 18:48:25.788 P00   INFO: check archive for prior segment 0000000F0000000000000065
2025-12-24 18:48:25.899 P00   WARN: exclude special file '/home/postgres/pgdata/.s.PGSQL.1622' from backup
2025-12-24 18:48:26.895 P00   INFO: execute non-exclusive backup stop and wait for all WAL segments to archive
2025-12-24 18:48:26.934 P00   INFO: backup stop archive = 0000000F0000000000000066, lsn = 0/66000138
2025-12-24 18:48:26.943 P00   INFO: check archive for segment(s) 0000000F0000000000000066:0000000F0000000000000066
2025-12-24 18:48:27.259 P00   INFO: new backup label = 20251224-184825F
2025-12-24 18:48:27.277 P00   INFO: full backup size = 38.8MB, file total = 977
2025-12-24 18:48:27.277 P00   INFO: backup command end: completed successfully (1892ms)
2025-12-24 18:48:27.277 P00   INFO: expire command begin 2.54.2: --exec-id=11755-9c265698 --log-level-console=info --log-level-file=debug --repo1-path=/home/postgres/pgbackup --repo1-retention-archive=7 --repo1-retention-diff=6 --repo1-retention-full=1 --stanza=cndb
2025-12-24 18:48:27.278 P00   INFO: repo1: expire full backup 20251224-081408F
2025-12-24 18:48:27.279 P00   INFO: repo1: remove expired backup 20251224-081408F
2025-12-24 18:48:27.297 P00   INFO: expire command end: completed successfully (20ms)
差异备份
[postgres@node84 ~]$ pgbackrest --stanza=cndb --log-level-console=info --archive-copy --type=diff backup
2025-12-24 18:48:34.949 P00   INFO: backup command begin 2.54.2: --archive-check --archive-copy --archive-timeout=600 --compress-level=6 --compress-type=gz --delta --exec-id=11774-aea13688 --log-level-console=info --log-level-file=debug --log-subprocess --pg1-database=postgres --pg1-path=/home/postgres/pgdata --pg1-port=1622 --pg1-user=sysdba --process-max=4 --repo1-path=/home/postgres/pgbackup --repo1-retention-archive=7 --repo1-retention-diff=6 --repo1-retention-full=1 --stanza=cndb --start-fast --type=diff
2025-12-24 18:48:34.964 P00   INFO: last backup label = 20251224-184825F, version = 2.54.2
2025-12-24 18:48:34.964 P00   INFO: execute non-exclusive backup start: backup begins after the requested immediate checkpoint completes
2025-12-24 18:48:35.066 P00   INFO: backup start archive = 0000000F0000000000000067, lsn = 0/67000028
2025-12-24 18:48:35.066 P00   INFO: check archive for segment 0000000F0000000000000067
2025-12-24 18:48:35.369 P00   WARN: exclude special file '/home/postgres/pgdata/.s.PGSQL.1622' from backup
2025-12-24 18:48:36.455 P00   INFO: execute non-exclusive backup stop and wait for all WAL segments to archive
2025-12-24 18:48:36.556 P00   INFO: backup stop archive = 0000000F0000000000000068, lsn = 0/68000050
2025-12-24 18:48:36.558 P00   INFO: check archive for segment(s) 0000000F0000000000000067:0000000F0000000000000068
2025-12-24 18:48:36.873 P00   INFO: new backup label = 20251224-184825F_20251224-184834D
2025-12-24 18:48:36.897 P00   INFO: diff backup size = 32MB, file total = 978
2025-12-24 18:48:36.897 P00   INFO: backup command end: completed successfully (1950ms)
2025-12-24 18:48:36.897 P00   INFO: expire command begin 2.54.2: --exec-id=11774-aea13688 --log-level-console=info --log-level-file=debug --repo1-path=/home/postgres/pgbackup --repo1-retention-archive=7 --repo1-retention-diff=6 --repo1-retention-full=1 --stanza=cndb
2025-12-24 18:48:36.898 P00   INFO: expire command end: completed successfully (1ms)
查看备份
[postgres@node84 ~]$ pgbackrest info
stanza: cndb
    status: ok
    cipher: none

    db (current)
        wal archive min/max (16): 000000090000000000000050/0000000F0000000000000068

        full backup: 20251224-184825F
            timestamp start/stop: 2025-12-24 18:48:25+08 / 2025-12-24 18:48:26+08
            wal start/stop: 0000000F0000000000000066 / 0000000F0000000000000066
            database size: 38.8MB, database backup size: 38.8MB
            repo1: backup set size: 3.1MB, backup size: 3.1MB

        diff backup: 20251224-184825F_20251224-184834D
            timestamp start/stop: 2025-12-24 18:48:34+08 / 2025-12-24 18:48:36+08
            wal start/stop: 0000000F0000000000000067 / 0000000F0000000000000068
            database size: 54.8MB, database backup size: 32MB
            repo1: backup set size: 3.2MB, backup size: 143.7KB
            backup reference total: 1 full
这里每个备节点需要提前初始化下 pgbackrest,创建 stanza,不然会备节点无法创建 stanza
# 创建 stanza
pgbackrest --stanza=cndb --log-level-console=info stanza-create

# 检查备份配置
pgbackrest --stanza=cndb --log-level-console=info check

pgbackrest默认不支持备库备份,但是好像可以只复制从备用备份复制的文件,然后从主备份复制剩余的少数文件来实现。基于备库pgbackrest备份,回头再研究下把!

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论