Barman 是一种生产级工具,用于管理 PostgreSQL 数据库的备份和恢复过程。它不仅可以处理物理备份,还可以自动管理保留策略、压缩、接近零的恢复点目标 (RPO),并在恢复窗口内及时实现任何所需点恢复 (PITR)。
Barman 的 Stream Archiving 功能是实现零恢复点目标 (RPO) 的关键组件。这是通过使用 pg_recievewal 实用程序实现的,该实用程序将预写日志记录 (WAL) 文件实时备份到指定的 Barman 服务器。此功能对于即使是最小数据丢失也不可接受的应用程序尤为重要。
在某些情况下,配置 Barman 涉及设置与 PostgreSQL 的标准连接以进行管理、协调和监控,以及 pg_basebackup 和 pg_receivewal 分别用于备份和 WAL 流的流复制连接。此配置在 Barman 的术语中称为 Streaming-Only setup,不需要 SSH 连接即可执行备份和存档作。
问题:什么是 pg_recievewal,为什么我们使用它而不是本机存档?
如果我们谈论 PostgreSQL 中的 archiving(Archive_command),它只会在 wal 文件已满(16MB)时存档它。如果发生灾难,我们有可能丢失高达 16MB 的已提交事务。
pg_receivewal 实用程序建立与 PostgreSQL 的复制连接,使其能够在磁盘上实时流式传输预写日志数据。当 pg_receivewal 正在积极编写当前的 wal 文件时,它会附加“.partial”扩展名以将其与已完成的 WAL 存档区分开来。区段完全写入后,pg_receivewal 会继续相应地重命名它。
问题:pg_receivewal 的复制模式是什么?
pg_receivewal 使用默认情况下是异步的流复制连接,因此丢失已提交事务的可能性很小。在无法接受此类风险的情况下,可以将同步复制与 pg_receivewal 一起使用。
但是,这种方法需要一定的权衡。
- 使用同步复制时,每次提交都需要往返到运行 pg_receivewal 的服务器,这可能会降低系统的整体吞吐量。
- 如果备份服务器(pg_receivewal 在同步模式下运行)遇到中断,PostgreSQL 将停止提交事务,直到备份服务器联机。
如果要将同步模式与 pg_receivewal 或物理/逻辑复制一起使用,请确保浏览 postgressql.conf 文件中的 synchronous_standby_names 和 synchronous_commit 参数。
酒保在行动
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">We are going to use 2 servers in this example
10.0.1.1 → PostgreSQL Server
10.0.1.2 → Remote Backup Server for Barman复制到剪贴板</span></span></span></span>
我们在 AWS EC2 上使用 Ubuntu 计算机。
注意:建议将 Barman 部署在 Remote 服务器上。
我们将执行以下步骤:
- 配置 Streaming 备份
- 使用 pg_receivewal 配置 wal 存档
- 进行完整备份
- 从 Barman 恢复完整备份
- 使用 Barman 的 PITR 恢复
PostgreSQL 服务器配置:
1:创建用于备份的数据库用户
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">Create user barman REPLICATION ENCRYPTED PASSWORD ‘barman’;
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 ;
GRANT CONNECT on DATABASE postgres to barman;复制到剪贴板</span></span></span></span>
注意:我们也可以为 barman 用户提供超级用户权限,但上述权限对于备份用户来说已经足够了。
2:编辑 PostgreSQL 配置文件
PostgreSQL.conf 文件
配置 barman 的设置
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">wal_level = replica or logical
max_wal_senders > 3
max_replication_Slots > 3
listen_addresses='*'复制到剪贴板</span></span></span></span>
我们至少需要 2 个连接(1 个用于备份,另一个用于 wal 流)
请注意,更改上述参数需要重新启动数据库。
pg_hba.conf
在 pg_hba.conf 中添加以下行,以允许 barman 用户使用复制连接获取 wal 文件和备份。将此连接限制为仅 Barman 备份服务器的 IP 地址。
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">host replication barman 10.0.1.2/32 md5复制到剪贴板</span></span></span></span>
为了保留上述更改,请使用以下命令重新加载 postgresql 服务器:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">select pg_reload_conf();复制到剪贴板</span></span></span></span>
Barman 服务器配置
1:安装 Barman Utility
在 Barman Backup 服务器 (10.0.1.2) 上以 root 用户运行以下命令。
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">sudo su - (Switch to root user)
apt-get update
apt-get install -y curl ca-certificates gnupg
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
apt-get update
apt-get -y install barman复制到剪贴板</span></span></span></span>
注意:在这里,您需要确保 pg_basebackup 实用程序与源 PostgreSQL 服务器具有相同的主要版本。Barman 将安装具有最新稳定版本的 postgreSQL 二进制文件,如下所示:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>psql --version</strong>
psql (PostgreSQL) 16.2 (Debian 16.2-1.pgdg120+1)复制到剪贴板</span></span></span></span>
对于我们的系统,我们需要备份 PostgreSQL v13。运行以下命令以识别包。
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>sudo apt list postgres* --installed</strong>
Listing... Done
postgresql-client-16/bookworm-pgdg,now 16.2-1.pgdg120+1 arm64 [installed,automatic]
postgresql-client-common/bookworm-pgdg,now 257.pgdg120+1 all [installed,automatic]
postgresql-client/bookworm-pgdg,now 16+257.pgdg120+1 all [installed]复制到剪贴板</span></span></span></span>
删除 postgresql-client-16 包并安装 postgresql 13
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">sudo apt remove postgresql-client-16
sudo apt install postgresql-client-13复制到剪贴板</span></span></span></span>
安装成功后,现在检查 psql
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>psql --version</strong>
psql (PostgreSQL) 13.14 (Debian 13.14-1.pgdg120+1)复制到剪贴板</span></span></span></span>
它还会在 OS 上创建一个 barman 用户。我们将使用 barman 用户运行所有备份作。
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>barman --version</strong>
3.9.0 Barman by EnterpriseDB (www.enterprisedb.com)复制到剪贴板</span></span></span></span>
2:配置 Barman
Barman 使用两种类型的配置文件
global/general configuration 是主要配置文件,通常默认设置为 /etc/barman.conf,包含常规选项,例如备份目录、日志记录、保留策略等。
服务器配置,其中 Barman 使用位于 /etc/barman.d 目录中的单个配置文件,每个配置文件都以 .conf 为后缀。我们可以定义与流式备份和存档方法相关的连接和设置。
注意: 将所有权授予 barman 用户,以便它可以轻松编辑文件。(可选)
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">sudo chown -R barman:barman /etc/barman*复制到剪贴板</span></span></span></span>
Barman 服务器配置
为数据库服务器创建配置文件,您将在其中提供连接详细信息和备份模式。我们正在创建一个 pg_server.conf 文件。
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">sudo su - barman
vi /etc/barman.d/pg_server.conf复制到剪贴板</span></span></span></span>
添加以下详细信息
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">[pg_server]
description = "Streaming Backups using pg_basebackup and pg_recievewal for archiving wal files"
conninfo = host=10.0.1.1 user=barman dbname=postgres port=5432
streaming_conninfo = host=10.0.1.1 user=barman dbname=postgres port=5432
backup_method = postgres
streaming_archiver = on
slot_name = barman_slot
create_slot = auto复制到剪贴板</span></span></span></span>
哪里
[pg_server] 是需要备份的 PostgreSQL 服务器的唯一标识符。
提供 Conninfo 是为了连接 postgres 数据库。
提供Streaming_conninfo用于为流式备份和pg_receivewal创建复制连接。
backup_method=postgres 显示我们将使用 pg_basebackup 进行流式备份。
另一种备份方法是 rsync,它需要在 barman 和 PostgreSQL 服务器之间建立无密码的 SSH 连接。这是支持增量备份功能的唯一选项。
Streaming_archiver = on 表示我们将使用 pg_recievewal 实用程序存档 WAL 日志,该实用程序将由 Barman 服务器调用。
slot_name 显示了需要创建的复制槽。
create_slot=auto 表示 Barman 将自动处理插槽的创建。
注意:backup_method=postgres 不提供增量/差异备份功能。
Barman 全局配置
该文件位于 /etc/barman.conf 中,您可以在其中指定备份参数,例如日志记录、保留策略、超时等。
我们将在 [barman] identifier 下设置以下参数。
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">vi /etc/barman.conf
barman_home = /mnt/backups
immediate_checkpoint = true
retention_policy = RECOVERY WINDOW OF 1 WEEK
log_level = INFO
compression = gzip
basebackup_retry_times = 2
basebackup_retry_sleep = 30复制到剪贴板</span></span></span></span>
其中 barman_home 是您的备份目录
3:PostgreSQL 连接密码文件
为了让 Barman 通过 barman 用户进行连接,我们需要在 barman 服务器上创建一个 pgpass 文件以进行无密码身份验证。
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">vi .pgpass复制到剪贴板</span></span></span></span>
通用条目将是
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">ServerIP:database:port:user:password复制到剪贴板</span></span></span></span>
我们将添加以下条目
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">10.0.1.1:5432:*:barman:barman复制到剪贴板</span></span></span></span>
其中 * 表示所有(所有数据连接 + 复制)
设置 pgpass 文件后,提供以下模式
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">chmod 0600 ~/.pgpass复制到剪贴板</span></span></span></span>
测试无密码连接
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">psql -h 10.0.1.1 -U barman -d postgres -p 5432复制到剪贴板</span></span></span></span>
4:验证
要验证 barman 与 PostgreSQL 服务器的连接,请运行以下命令
要检查服务器配置是否有效,您可以使用 barman check 命令:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>barman check pg_server</strong>
Server pg_server:
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: 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: FAILED (have 0 backups, expected at least 1)
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
archiver errors: OK复制到剪贴板</span></span></span></span>
对于 wal-archive 错误,我们需要启动 pg_recievewal 实用程序来流式传输存档
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">barman cron复制到剪贴板</span></span></span></span>
我们现在可以忽略最小冗余错误,因为没有进行备份。
问题:barman cron command 的职责是什么?
barman cron 执行各种任务,例如在必要时启动 WAL 接收器、处理累积的 WAL 并将其存档。它还会根据保留策略删除备份及其关联的存档。此过程在前台运行,建议安排 barman cron 每分钟执行一次。
请记住,如果 wals 没有通过存档程序或 streaming_archiver 正确存档到 Barman,则无法开始备份。
确保使用以下命令监控 barman 日志文件
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">tail -100f /var/log/barman/barman.log复制到剪贴板</span></span></span></span>
有关这些命令及其选项的更多详细信息,请参阅 Barman 手册。
问题:如何使用 Barman 设置保留策略?
Barman 支持备份保留策略,允许用户建立有关备份持续时间和相关存档日志(预写日志段)的准则,以满足未来的恢复需求。用户可以灵活地根据备份冗余(定期备份的数量)或恢复窗口(特定时间范围)来定义保留策略。
在基于冗余的保留策略中,用户指示要保留的所需定期备份数。例如,如果我们需要保留 8 个完整备份及其 wal 文件,我们可以像这样定义保留期:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">retention_policy: REDUNDANCY 8复制到剪贴板</span></span></span></span>
另一个选项是定义一个 recovery window(Mostly used),用于指定一个时间段。Barman 确保将时间点恢复所需的备份和存档 WAL 文件保留到指定窗口内的任何时刻。
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">retention_policy: RECOVERY WINDOW OF 1 WEEK复制到剪贴板</span></span></span></span>
此配置表示 Barman 将备份和存档保留一周,从而能够恢复到上周内的任何时间点。
我们还可以为备份定义最低冗余级别,确保即使保留期到期也不会删除备份。这可以通过配置以下参数来实现。
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">minimum_redundancy = 2复制到剪贴板</span></span></span></span>
使用 Barman 进行备份
现在您已经准备好了 Barman,让我们手动创建备份。
在 barman 备份服务器上以 barman 用户身份运行以下命令,以创建您的第一个备份:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">sudo su - barman
barman backup pg_server复制到剪贴板</span></span></span></span>
Barman 目录
Barman 根据服务器唯一名称创建一个目录,如下所示。
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>ls -ltr /mnt/backups/</strong>
total 24
drwxrwxr-x 7 barman barman 4096 Feb 8 22:20 pg_server复制到剪贴板</span></span></span></span>
让我们看一下pg_server文件夹内部
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>ls -ltr /mnt/backups/pg_server</strong>
total 20
drwxr-xr-x 2 barman barman 4096 Feb 11 20:37 incoming
drwxr-xr-x 2 barman barman 4096 Feb 11 20:37 errors
drwxr-xr-x 2 barman barman 4096 Feb 11 20:37 base
drwxr-xr-x 3 barman barman 4096 Feb 11 20:41 wals
drwxr-xr-x 2 barman barman 4096 Feb 11 20:46 streaming复制到剪贴板</span></span></span></span>
当使用 rsync 方法进行流式处理时,传入目录将发挥作用。
所有事务 WAL 文件将首先定向到流式处理文件夹。
当手动或通过 cron job 执行 barman cron 命令时,streaming 文件夹中的 wal 文件将被压缩并传输/存档到 wals 文件夹。
基目录用作物理备份的存储库,可以在其中找到多个备份时间戳。
计划备份
确保安排 cron 和备份。
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>crontab -e</strong>
# m h dom mon dow command
* * * * * /usr/bin/barman cron
00 01 * * fri /usr/bin/barman backup pg_server复制到剪贴板</span></span></span></span>
Barman cron 命令将每分钟运行一次。
根据 1 周的保留策略,我们将备份安排为每周运行 2 次。
酒保命令
1:检查
要检查服务器配置是否有效,您可以使用 barman check 命令:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>barman check pg_server</strong>
Server postgres_server:
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 (603.5 MiB)
wal maximum age: OK (no last_wal_maximum_age provided)
wal size: OK (210.9 MiB)
compression settings: OK
failed backups: OK (there are 0 failed backups)
minimum redundancy requirements: OK (have 1 backups, expected at least 1)
pg_basebackup: OK
pg_basebackup compatible: OK
pg_basebackup supports tablespaces mapping: OK
systemid coherence: OK
pg_receivexlog: OK
pg_receivexlog compatible: OK
receive-wal running: OK
archiver errors: OK复制到剪贴板</span></span></span></span>
2:列表备份
您可以通过以下方式列出给定服务器的可用备份目录:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>barman list-backup pg_server</strong>
pg_server 20240215T182843 - Thu Feb 15 18:28:53 2024 - Size: 603.5 MiB - WAL Size: 210.9 MiB复制到剪贴板</span></span></span></span>
哪里
20240215T182843 是唯一的备份 ID(可以在 base 文件夹中找到)
603.5 MiB 是备份的总大小
210.9MB 是在备份期间/之后创建的 wal 文件大小。Wal 文件大小将根据 PostgreSQL 服务器上的活动而不断增加
3:显示备份
您可以使用以下方法检索给定服务器的特定备份的所有可用信息:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>barman show-backup </strong>servername Backup-ID
<strong>barman show-backup pg_server 20240215T182843 </strong>
Backup 20240215T182843:
Server Name : pg_server
System Id : 7335180256203400928
Status : DONE
PostgreSQL Version : 130014
PGDATA directory : /mnt/data/database
Base backup information:
Disk usage : 603.5 MiB (603.5 MiB with WALs)
Incremental size : 603.5 MiB (-0.00%)
Timeline : 3
Begin WAL : 00000003000000000000004D
End WAL : 00000003000000000000004D
WAL number : 1
WAL compression ratio: 99.90%
Begin time : 2024-02-15 18:28:43.742794+00:00
End time : 2024-02-15 18:28:53.111891+00:00
Copy time : 9 seconds
Estimated throughput : 64.5 MiB/s
Begin Offset : 40
End Offset : 0
Begin LSN : 0/4D000028
End LSN : 0/4E000000
WAL information:
No of files : 65
Disk usage : 210.9 MiB
WAL rate : 247.27/hour
Compression ratio : 79.72%
Last available : 00000003000000000000008E
Reachable timelines : 4
Catalog information:
Retention Policy : VALID
Previous Backup : - (this is the oldest base backup)
Next Backup : - (this is the latest base backup)复制到剪贴板</span></span></span></span>
4:显示服务器
您可以通过以下方式显示给定服务器的配置参数:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">barman show-servers <server_name>
barman show-servers pg_server
Server pg_server:
active: True
archive_timeout: 0
archiver: False
archiver_batch_size: 0
autogenerate_manifest: False
aws_profile: None
aws_region: None
azure_credential: None
azure_resource_group: None
azure_subscription_id: None
.
.
.
wal_level: replica
wal_retention_policy: MAIN
wals_directory: /backups/cluster/wals
xlog_segment_size: 16777216
xlogpos: 4/B1C09F8复制到剪贴板</span></span></span></span>
5:复制状态
replication-status 命令报告当前连接到 PostgreSQL 服务器的任何流客户端的状态,包括 Barman 服务器的 receive-wal 进程(如果已配置)。
您可以按如下方式执行该命令:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">barman replication-status <server_name>
<strong>barman replication-status pg_server</strong>
Status of streaming clients for server 'cluster':
Current LSN on master: 4/B1C09F8
Number of streaming clients: 2复制到剪贴板</span></span></span></span>
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">1: Async WAL streamer
<strong>Application name: barman_receive_wal</strong>
Sync stage : 3/3 Remote write
Communication : TCP/IP
IP Address : 172.31.85.163 / Port: 60992 / Host: -
User name : barman
Current state : streaming (async)
Replication slot: barman_repslot
WAL sender PID : 1241
Started at : 2024-02-19 21:38:02.854754+00:00
Sent LSN : 4/B1C09F8 (diff: 0 B)
Write LSN : 4/B1C09F8 (diff: 0 B)
Flush LSN : 4/B000000 (diff: -1.8 MiB)
2: Async standby
<strong>Application name: pgreplica1</strong>
Sync stage : 5/5 Hot standby (max)
Communication : TCP/IP
IP Address : 172.31.90.152 / Port: 58064 / Host: -
User name : repmgr
Current state : streaming (async)
Replication slot: slot_2
WAL sender PID : 1243
Started at : 2024-02-19 21:38:25.812454+00:00
Sent LSN : 4/B1C09F8 (diff: 0 B)
Write LSN : 4/B1C09F8 (diff: 0 B)
Flush LSN : 4/B1C09F8 (diff: 0 B)
Replay LSN : 4/B1C09F8 (diff: 0 B)复制到剪贴板</span></span></span></span>
6:删除
您可以使用以下命令手动删除给定的备份:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>barman delete <server_name> <backup_id></strong>复制到剪贴板</span></span></span></span>
使用 Barman 进行恢复
远程恢复绝对是使用 Barman 恢复 PostgreSQL 服务器的最常见方法。
先决条件
- 两个系统应具有相同的硬件体系结构。
- 两个系统上的 PostgreSQL 主要版本应匹配。
- 同步 Barman 和 PostgreSQL 服务器之间的时间。
- 在 Barman 和 PostgreSQL 服务器之间建立双向无密码 SSH 连接,以便进行还原。
- 建议在所有 PostgreSQL 服务器上安装 barman-cli 软件包,无论是主服务器还是备用服务器。这可以使用以下命令来实现:
<span style="background-color:#f5f5f5"><span style="color:#666666">apt-get install barman-cli复制到剪贴板</span></span> - Barman 和远程主机之间的 SSH 连接应使用公钥交换身份验证。
- 远程用户需要权限才能在指定目标中创建备份目录结构,并确保应有空间用于存储基本备份和必要的 WAL 文件,以便在远程服务器上进行恢复。
问题:为什么需要 barman-cli?
barman-cli 包含 barman-wal-restore 实用程序,可用于
- 备用恢复(如果不同步)。
- 时间点恢复。
完全恢复(仅限备份)
首先,选择要还原的备份
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>barman list-backup pg_server</strong>
postgres_server 20240210T000102 - Sat Feb 10 00:07:59 2024 - Size: 23.9 GiB - WAL Size: 0 B
postgres_server 20240209T232959 - Fri Feb 9 23:33:57 2024 - Size: 23.0 GiB - WAL Size: 578.5 MiB
postgres_server 20240209T203252 - Fri Feb 9 20:34:24 2024 - Size: 7.2 GiB - WAL Size: 9.4 GiB
postgres_server 20240208T223050 - Thu Feb 8 22:31:27 2024 - Size: 3.6 GiB - WAL Size: 2.7 GiB复制到剪贴板</span></span></span></span>
我们将恢复最新的备份
pg_server 20240210T000102
恢复命令:
您可以执行以下命令将备份复制到远程服务器
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>barman recover --remote-ssh-command "ssh postgres@10.0.1.1" postgres_server 20240210T000102 /mnt/data/postgresql/13/main</strong>
Starting remote restore for server pg_server using backup 20240210T000102
Destination directory: /mnt/data/postgresql/13/main
Remote command: ssh postgres@10.0.1.1
Copying the base backup.复制到剪贴板</span></span></span></span>
恢复后,请务必检查配置文件,尤其是 postgresql.auto.conf 并重新启动服务器。请注意,这只会恢复备份期间创建的 backup 和 wal 文件。
完整恢复(备份 + wal 文件末尾)
如果您想恢复到 wal 文件的终点,您需要运行以下命令
恢复命令:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>barman recover --remote-ssh-command "ssh postgres@172.31.30.185" pg_server 20240208T223050 /mnt/data/postgresql/13/main --get-wal </strong>
WARNING: 'get-wal' is in the specified 'recovery_options'.
Before you start up the PostgreSQL server, please review the postgresql.auto.conf file
inside the target directory. Make sure that 'restore_command' can be executed by the PostgreSQL user.
Recovery completed (start time: 2024-02-10 21:20:36.786116+00:00, elapsed time: 49 seconds)
Your PostgreSQL server has been successfully prepared for recovery!复制到剪贴板</span></span></span></span>
–get-wal 选项使用户能够从 Barman 位置请求任何 WAL 文件。Barman 通过使用 barman-wal-restore 脚本建立restore_command来配置恢复。
barman-wal-restore 提供了各种有用的功能,包括 WAL 文件的自动压缩和解压缩以及 peek 功能,该功能允许在 PostgreSQL 应用一个 WAL 文件时检索后续 WAL 文件。这优化了 PostgreSQL 和 Barman 之间的带宽使用。
在 postgresql.auto.conf 中检查restore_command
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">restore_command = 'barman-wal-restore -P -U barman ip-172-31-85-163.ec2.internal postgres_server %f %p'复制到剪贴板</span></span></span></span>
通过使用 -P 选项,可以检索当前 .partial WAL 文件的内容。
正确设置 restore 命令:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">restore_command = 'barman-wal-restore -P -U barman 172.31.85.163 pg_server %f %p'复制到剪贴板</span></span></span></span>
现在启动 postgresql 服务器
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">/usr/lib/postgresql/13/bin/pg_ctl -D /mnt/data/postgresql/13/main start复制到剪贴板</span></span></span></span>
它将恢复到一致的点,然后应用 barman 提供的所有可用文件。
时间点恢复
对于 PITR,上述所有步骤都是相同的,但我们需要更改恢复命令并添加目标时间标志
恢复命令:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>barman recover --remote-ssh-command "ssh postgres@10.0.1.1" --target-time="2024-02-10 21:16:17" postgres_server 20240208T223050 /mnt/data/postgresql/14/main --get-wal</strong>
Starting remote restore for server postgres_server using backup 20240208T223050
Destination directory: /mnt/data/postgresql/14/main
Remote command: ssh postgres@172.31.30.185
Doing PITR. Recovery target time: '2024-02-10 21:16:17+00:00'
Using safe horizon time for smart rsync copy: 2024-02-08 22:30:50.190930+00:00
Copying the base backup.复制到剪贴板</span></span></span></span>
检查 postgresql.auto.conf 中的 restore_command 和 recovery_target_time
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">restore_command = 'barman-wal-restore -P -U barman ip-172-31-85-163.ec2.internal postgres_server %f %p
Recovery_target_time = ‘2024-02-10 21:16:17’复制到剪贴板</span></span></span></span>
配置 restore 命令并启动服务器。它将继续应用所有 WAL 文件,直到 2024-02-10 21:16:17。恢复过程完成后,它将暂停恢复,我们需要运行以下命令来启用写入:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">Select pg_wal_replay_resume();复制到剪贴板</span></span></span></span>
注意: barman-wal-restore 工具也提供 -p(parallel) 选项来加速 wal fetch 进程。使用 barman-wal-restore –-help 查看其他选项
问题: 如何在故障转移的情况下将 barman 重定向到新服务器?
在集群设置和故障转移发生的情况下,将 Barman 重定向到新服务器的过程涉及更新 /etc/barman.d/pg_server.conf 文件中的 IP 地址。使用新服务器的详细信息修改 conninfo 和 streaming_conninfo 参数,然后执行命令 barman cron 以从新主服务器启动pg_receivewal进程。建议在升级或恢复后执行完整基本备份。
Barman v3.10(最新版) 为集群配置提供了 config-switch 选项。副本信息应按如下方式提供
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">[pg_server]
description = "Streaming Backups with pg-recievewal for archiving"
conninfo = host=10.0.1.1 user=barman dbname=postgres port=5432
streaming_conninfo = host=10.0.1.1 user=barman dbname=postgres port=5432
backup_method = postgres
streaming_archiver = on
slot_name = barman_repslot
create_slot = auto
[pg_server:pgreplica1]
description = "Streaming Backups with pg-recievewal for archiving"
conninfo = host=10.0.1.3 user=barman dbname=postgres port=5432
streaming_conninfo = host=10.0.1.3 user=barman dbname=postgres port=5432
model=true
backup_method = postgres
streaming_archiver = on
slot_name = barman_repslot
create_slot = auto复制到剪贴板</span></span></span></span>
你可以看到主服务器名称是这样附加到 pgreplica1 服务器的
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">[pg_server:pgreplica1]复制到剪贴板</span></span></span></span>
现在,如果发生故障转移,您需要通过运行以下命令来切换服务器:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">barman config-switch pg_server pg_server:pgreplica2复制到剪贴板</span></span></span></span>
这将使用所选模型中定义的值覆盖集群配置选项。
问题:如何停止 barman 服务员?
1:从 crontab 中删除所有条目。Barman 安装还会自动将 crontab 条目放在这里:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa"><strong>cat /etc/cron.d/barman</strong>
# /etc/cron.d/barman: crontab entries for the barman package
MAILTO=root
* * * * * barman [ -x /usr/bin/barman ] && /usr/bin/barman -q cron复制到剪贴板</span></span></span></span>
2:运行 pg_receivewal stop 停止存档。这可以使用以下命令完成:
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">barman receive-wal --stop pg_server复制到剪贴板</span></span></span></span>
3:删除复制槽
<span style="background-color:#f5f5f5"><span style="color:#666666"><span style="color:#000000"><span style="background-color:#f4f7fa">barman receive-wal --drop-slot pg_server复制到剪贴板</span></span></span></span>
结论
关键是要设置一个定期备份计划,以满足组织恢复数据的速度 (RTO) 和可以接受丢失的数据量 (RPO) 的需求。Barman 为备份的保留时间提供了灵活的选项。将备份数据存储在不同的地方并定期测试以防止灾难也很重要。

5411

被折叠的 条评论
为什么被折叠?



