PostgreSQL复制管理工具:Barman与OmniPITR全解析
立即解锁
发布时间: 2025-08-21 02:23:06 阅读量: 1 订阅数: 5 


PostgreSQL高可用性架构与优化实践
# PostgreSQL 复制管理工具:Barman 与 OmniPITR 全解析
## 1. 为何是三?
在评估管理 PostgreSQL 关系的能力时,我们常认为“三”是个神奇的数字,原因在于重组。若需使用灾难恢复(DR)环境,最终要重建整个主系统;若提升流式副本,所有依赖主服务器的服务器都要切换到其流。这些操作耗时且因涉及至少三台服务器,需重复多次,而手动调用命令易出错。高可用服务器容不得意外,一次错误的流更改可能导致平台错误、系统停机或正常运行的差异,所以我们要么编写自己的工具,要么利用现有工具。
## 2. 安装和配置 Barman
### 2.1 准备工作
目前 Barman 最新版本是 2.0,它可在 PostgreSQL 包存储库中获取。不同系统添加存储库的方式不同:
- Debian 或基于 Ubuntu 的系统,按此 URL 说明操作:[http://wiki.postgresql.org/wiki/Apt](http://wiki.postgresql.org/wiki/Apt)
- 基于 Red Hat 的系统,安装此 URL 中合适的 RPM:[http://yum.postgresql.org/repopackages.php](http://yum.postgresql.org/repopackages.php)
建议仅使用存储库,因为存储库提供的包除安装软件外,还会执行用户创建等任务。
### 2.2 操作步骤
此过程需两台服务器,备份服务器名为 pg - backup,主 PostgreSQL 服务器名为 pg - primary。要确保 barman 和 postgres 系统用户以及 postgres 数据库用户有密码,数据库位于 /db/pgdata。步骤如下:
1. 以具有 root 权限的用户身份安装 Barman 工具包:
- Red Hat 服务器:`sudo yum install barman`
- Debian 系统:`sudo apt - get install barman`
2. 在 pg - backup 服务器上以 barman 用户身份执行以下命令,以 postgres 用户身份直接 SSH 访问 pg - primary:
```bash
ssh-keygen -t rsa -N ''
ssh-copy-id postgres@pg-primary
```
3. 在 pg - primary 服务器上以 postgres 用户身份执行以下命令,以 barman 用户身份直接 SSH 访问 pg - backup:
```bash
ssh-keygen -t rsa -N ''
ssh-copy-id barman@pg-backup
```
4. 确保 pg - primary 上的 pg_hba.conf 文件中有以下行:
```plaintext
host all postgres pg-backup md5
```
5. 确保 pg - primary 上的 postgresql.conf 中配置了以下设置:
```plaintext
archive_mode = on
archive_command = 'rsync -aq %p \
barman@pg-backup:primary/incoming/%f'
```
6. 在 pg - backup 上为 barman 用户的 .pgpass 文件中输入以下行:
```plaintext
*:*:*:postgres:postgres-password
```
7. 以 postgres 用户身份使用以下命令重启 pg - primary 上的 PostgreSQL 服务:
```bash
pg_ctl -D /db/pgdata restart
```
8. 根据实际情况,在 /etc/barman.conf 或 /etc/barman/barman.conf 末尾添加以下内容:
```plaintext
[primary]
description = "Primary PostgreSQL Server"
conninfo = "host=pg-primary user=postgres"
ssh_command = "ssh postgres@pg-primary"
archiver = on
```
9. 在 pg - backup 上以 barman 用户身份执行以下命令,检查主服务器的配置条目:
```bash
barman check primary
```
### 2.3 工作原理
首先安装 Barman,由于它在 PostgreSQL 存储库中可用,此步骤简单。为使 Barman 正常工作,它需从 pg - primary 服务器检索文件,postgres 用户需通过 rsync 向 pg - backup 传输文件。为此,使用 ssh - keygen 生成 SSH 密钥,设置密钥类型为 RSA 并将密码短语设为空,再用 ssh - copy - id 发送公钥。接着修改 pg - primary 上的 pg_hba.conf 以允许 postgres 数据库用户从 pg - backup 连接,同时启用 archive_mode 并设置 archive_command。重启 PostgreSQL 后,在 barman.conf 文件中添加配置信息,最后使用 barman check primary 测试 Barman 是否正常工作,该命令不仅检查服务器状态,还会创建管理备份所需的目录和跟踪文件。
### 2.4 更多信息
从 2.0 版本起,Barman 支持通过流式复制从主服务器获取备份。若要配置 Barman 使用流式复制而非 rsync,需修改配置条目如下:
```plaintext
[primary]
description = "Primary PostgreSQL Server"
conninfo = "host=pg-primary user=postgres"
streaming_conninfo = "host=pg-primary user=rep_user"
streaming_archiver = on
slot_name = "barman"
backup_method = "postgres"
```
修改后,可从 postgresql.conf 中移除 archive_command 设置。还需以 barman 系统用户身份启动辅助进程以获取稳定的事务日志流用于备份:
```bash
barman receive-wal --create-slot primary
barman receive-wal primary &>/var/log/postgresql/barman.log &
```
但使用 postgres 备份方法时,Barman 会调用 pg_basebackup,每次复制整个数据库,不适合超大型实例。对于超过 1 TB - 2 TB 的 PostgreSQL 数据,可将 backup_method 设置为 rsync 并添加之前配置中的 ssh_command 行,仍使用流式设置获取事务日志。
### 2.5 相关链接
- Barman 官网:[http://www.pgbarman.org/](http://www.pgbarman.org/)
- Barman 文档:[http://docs.pgbarman.org/release/2.0/](http://docs.pgbarman.org/release/2.0/)
## 3. 使用 Barman 备份数据库
### 3.1 准备工作
此操作依赖于 Barman 已安装在备份服务器上,请先完成“安装和配置 Barman”的步骤。
### 3.
0
0
复制全文
相关推荐










