使用 barman的备份和归档PostgreSQL

 

1 前言

1.1 Barman简介

barman(备份和恢复管理器)是用于PostgreSQL服务器进行灾难恢复的开源管理工具,是以Python编写的。它支持对多台服务器执行远程备份,以降低风险并帮助DBA进行数据库恢复。

1.2 Barman的备份方式

本文假定读者熟悉理论上的灾难恢复概念,并且具有在PostgreSQL物理备份和灾难恢复方面的基础知识。

我们知道 PostgreSQL 的连续备份包含一个或多个基础备份和连续归档的WAL日志。Barman 支持两种方法实现这样的备份。我们讨论的情况是数据库服务和备份文件在不同服务器上的情况。

1.2.1 基于流协议的备份

基于流协议的备份方法是barman 提供的独特的方法。它适用于PostgreSQL 9.4或更高版本。它使用pg_basebackup进行基础备份,使用pg_receivewal ( PostgreSQL 10 以下是 pg_receivexlog)归档WAL。其结构如下图所示:

在这种情况下,您将需要配置:

  1. 与PostgreSQL的标准连接,用于管理,协调和监视
  2. 供pg_basebackup(用于基本备份操作)和pg_receivewal(用于WAL流归档)使用的流复制连接

 

 

用Barman 的术语来说,此设置称为 streaming-only设置,因为它不需要任何SSH连接即可进行备份和归档操作。Barman 也支持基于基于流协议备份与基于SSH 的WAL 归档结合,下图描绘了这种实现:

 

这种方案要求:

  1. 额外的SSH连接,以允许用户postgres 在PostgreSQL服务器以barman的用户身份连接到Barman服务器上。
  2. 在PostgreSQL的配置文件postgresql.conf 中配置archive_command,内容是将WAL文件归档到Barman的归档目录。具体格式可参考官方手册

1.2.2 基于rsync/ SSH 的备份

基于rsync/ SSH 的备份是一种传统的基于文件的备份方式。它一般适用于下面的情形。

  1. PostgreSQL服务器版本是8.3、8.4、9.0或9.1
  2. 使用表空间的PostgreSQL服务器版本是9.2或9.3
  3. 增量备份,并行备份和重复数据删除
  4. 备份期间的网络压缩
  5. 更好地控制带宽使用,包括在表空间的基础上

它的体系结构如下图所示:

 

 

在这种情况下,您将需要配置:

  1. 与PostgreSQL的标准连接,用于管理,协调和监视
  2. 用于基础备份操作的SSH连接,rsync会使用它,以允许barman用户在Barman服务器上以用户postgres的身份连接到PostgreSQL服务器
  3. 用于WAL归档的SSH连接,archive_command会使用它,以允许用户postgres 在PostgreSQL服务器以用户barman的身份连接到Barman服务器。

从PostgreSQL 9.2开始,您可以添加用于WAL流式传输的流复制连接。下图描绘了这种实现:

 

1.3 实验环境

实验需要两台服务器,下面是它们的一些基本信息。

 

操作系统: CentOS 7.5

内存: 32G

CPU: 8个逻辑核

软件:

PostgreSQL 11.4

yum

python 3.6

barman 2.11

需要的python 模块:

argcomplete-1.12.0

argh-0.26.2

psycopg2-2.8.5

python-dateutil-2.8.1

setuptools-49.6.0

      

数据库服务器的IP地址:10.40.239.228

备份服务器的IP地址:10.40.239.229

ssh 端口:22,默认值

postgresql 的运行端口:5432

postgresql 的bin目录 /opt/postgresql/bin/

postgresql 的data 目录 /opt/postgresql/data

    注意,barman 要求特定版本操作系统和所依赖的软件。具体要求如附录1所示。

2    Barman备份环境的搭建

2.1 安装软件

2.1.1 数据库服务器上的操作

   1. 安装 rsync

yum install rsync

2.1.2 备份服务器上的操作

下面的操作都使用用户 root 完成。

1.    安装 epel源  (Extra Packages for Enterprise Linux)
yum -y install epel-release 
 
  1. 安装 Python 3.6

yum -y install python3-3.6.8-13.el7.x86_64

 

注意,如果你不想指定Python 3的版本,可以换成执行:

yum -y install python3

 

在Python 3安装过程中,setuptools 和 pip 也会被安装。

 

  1. 安装rsync

yum -y install rsync

 

  1. 安装 pgdg 源

rpm -ivh https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

 

  1. 安装Barman

方法1:使用yum 安装barman

yum –y install barman-2.11-1.rhel7.noarch

 

使用yum安装的过程中,会自动安装下面的模块

python36-argcomplete.x86_64

python36-argh

python36-psycopg2

python36-dateutil

 

用 yum 安装完barman后,程序会创建一个操作系统用户 barman,并创建一个文件 /etc/barman.conf,内容是barman的一些全局配置项,一个目录 /etc/barman.d,存放与数据库服务器有关的配置信息。

 

 

方法2:使用源码安装。

  1. SourceForge 上下载barman源码,这里我们下载 barman-2.11.tar.gz。
  2. 解压文件后,进入解压后的目录,使用python3安装它:

tar -zxvf barman-2.11.tar.gz

cd ./barman-2.11

python3 ./setup.py build

python3 ./setup.py install

 

  1. 使用pip安装需要的python模块。进入 python3 的安装目录,执行命令如下:

./pip install argcomplete -i https://pypi.tuna.tsinghua.edu.cn/simple/

./pip install argh -i https://pypi.tuna.tsinghua.edu.cn/simple/

./pip install psycopg2 -i https://pypi.tuna.tsinghua.edu.cn/simple/

./pip install python-dateutil -i https://pypi.tuna.tsinghua.edu.cn/simple/

   

  1. 在操作系统中创建用户barman

useradd -m barman -d /var/lib/barman

 

  1. 将barman 安装包解压后的barman-2.11/barman, 将 文件barman.conf  和 目录barman.d 拷贝到 /etc/ 中。

cd barman-2.11/barman

cp barman.conf  /etc/

cp -R barman.d  /etc/

2.2 配置

2.2.1  数据库服务器上的操作

下面的操作都使用用户 root 完成。

  1.  为用户postgres创建ssh密钥以及授权文件,如果它们不存在。

su postgres

cd ~

ssh-keygen -t rsa

echo “”>> ./ssh/authorized_key

 

随后在barman用户的主目录 /var/lib/pgsql/ 下的目录 .ssh中,会生成私钥文件 id_rsa和 公钥文件 id_rsa.pub。

 

  1. 把PostgreSQL 的 bin 目录复制到备份服务器的目录 /etc/barman.d/中。可以使用scp。例如,PostgreSQL的bin 的位置是 /opt/postgresql/bin,则可以执行如下命令:

 

scp -r /opt/postgresql/bin root@10.40.239.229:/etc/barman.d/

 

  1. 在数据库中创建用户 barman 和 streaming_barman,其中barman 要是superuser, streaming_barman 有复制权限。具体的sql如下。

 

CREATE ROLE barman with LOGIN PASSWORD 'barman123' SUPERUSER;

CREATE ROLE streaming_barman with LOGIN PASSWORD 'streaming_barman123' REPLICATION;

 

  1. 数据库的配置文件 postgresql.conf 应该做如下配置:

listen_addresses = '*'

wal_level = replica

max_wal_senders = 10

archive_mode = off

 

小贴士:你可能注意到,这里的配置和开启PostgreSQL原生的连续归档时的配置不同。他们的区别是,对于基于流协议的归档,我们需要保证 archive_mode = off,且不必配置archive_command;而对于PostgreSQL原生的连续归档,我们需要设置archive_mode = on,并配置archive_command。

 

  1. 在 pg_hba.conf 中,添加如下内容,允许用户barman和streaming_barman访问。

host      all           barman                   127.0.0.1/32            md5

host      all           barman                   10.40.239.228/32        md5

host      all           barman                   10.40.239.229/32        md5

host      replication    streaming_barman         127.0.0.1/32            md5

host      replication    streaming_barman         10.40.239.228/32        md5

host      replication    streaming_barman         10.40.239.229/32        md5

 

2.2.2  备份服务器上的操作

下面的操作都使用用户 barman完成。

  1. 为用户barman创建ssh密钥。

ssh-keygen -t rsa

 

随后在barman用户的主目录 /var/lib/pgsql/ 下的目录 .ssh中,会生成私钥文件 id_rsa和 公钥文件 id_rsa.pub。

 

  1. 复制用户barman 的公钥文件 id_rsa.pub 中的内容,并将它追加到数据库服务器上

的用户 postgres 的主目录中的文件 .ssh/authorized_keys 中。这样做的目的是允许barman以用户postgres的身份免密访问数据库服务器

 

你可以手动复制此内容,并将它粘贴到数据库服务器上的用户 postgres 的主目录中的文件 .ssh/authorized_keys 末尾。

你也可以通过下面的命令完成复制。如果使用下面的命令,你需要保证数据库服务器上 postgres用户已经设置了密码。

ssh-copy-id postgres@10.40.239.228

  

  1. 创建 barman 的日志目录 /var/log/barman

mkdir /var/log/barman

 

  1. 编辑  /etc/barman.conf,在 “[barman]” 之下修改这些配置项,以设置全局的备份参数:

 

; System user

barman_user = barman

 

; Directory of configuration files. Place your sections in separate files with .conf extension

; For example place the 'main' server section in /etc/barman.d/main.conf

configuration_files_directory = /etc/barman.d

 

; Main directory

barman_home = /var/lib/barman

 

; Log location

log_file = /var/log/barman/barman.log

 

; Log level (see https://docs.python.org/3/library/logging.html#levels)

log_level = INFO

 

 

; Global retention policy (REDUNDANCY or RECOVERY WINDOW) - default empty

retention_policy = RECOVERY WINDOW OF 4 WEEKS

 

; Number of parallel jobs for backup and recovery via rsync (default 1)

parallel_jobs = 3

 

; Immediate checkpoint for backup command - default false

immediate_checkpoint = true

 

; Enable network compression for data transfers - default false

network_compression = false

 

; Number of retries of data copy during base backup after an error - default 0

basebackup_retry_times = 3

 

; Number of seconds of wait after a failed copy, before retrying - default 30

basebackup_retry_sleep = 30

 

 

; Minimum number of required backups (redundancy)

minimum_redundancy = 2

 

 

这里我们用中文解释一下这些参数的含义:

barman_user 运行barman 的用户

configuration_files_directory 配置文件所在目录。 将您的备份放在扩展名为.conf的单独文件中

barman_home barmn的主目录

log_file barman 日志文件的位置

log_level 日志级别

retention_policy 备份的保留策略。空表示禁用;REDUNDANCY 2 表示保留两份基础备份;RECOVERY WINDOW OF 4 WEEKS 表示保留4星期之内的备份。

parallel_jobs 通过rsync备份和恢复的并行作业数

immediate_checkpoint 备份命令是否执行立即检查点

network_compression 启用网络压缩以进行数据传输。对于流备份,这个参数设置为false。

basebackup_retry_times 在基础备份期间发生错误后重新尝试的次数

basebackup_retry_sleep 复制失败后,重试之前等待的秒数

minimum_redundancy 所需的最小备份数量

 

  1. 配置要备份的数据库的信息。

5.1  进入 /etc/barman.d, 将 streaming-server.conf-template 复制为 pg.conf,文件名中的“pg”也是此备份任务的名称。

cd /etc/barman.d

cp streaming-server.conf-template pg.conf

 

5.2 编辑 pg.conf,将 [streaming] 修改为 [pg],这是备份任务的名称,并配置如下参数:

conninfo =  host=10.40.239.228 port=5432 user=barman dbname=postgres password=barman123

 

streaming_conninfo = host=10.40.239.228 port=5432 dbname=postgres user=streaming_barman password=streaming_barman123

backup_method = postgres

streaming_archiver = on

slot_name = barman

path_prefix = "/etc/barman.d/bin"

   

下面是这些参数的含义:

conninfo 基础备份的连接信息。

streaming_conninfo 流归档的连接信息。

backup_method 基础备份的方式。“postgres”表示使用 pg_basebackup 进行备份;rsync 表示使用rsync备份。

streaming_archiver 是否启用流归档。on 表示是。

slot_name 复制槽的名称

path_prefix 客户端的postgresql 的bin 的路径。

 

3    使用barman备份和恢复

3.1 备份

下面的操作都在备份服务器上进行。

1.执行barman的命令,创建名为 pg的复制槽

barman receive-wal --create-slot pg

 

2. 在后台不间断地从数据库服务端接收wal日志:

barman receive-wal pg &

    注意,& 表示在后台执行前面的命令。

 

3.检查备份任务pg 的运行状态

barman check pg

 

 

如果各项结果均为 OK,则表示状态正常。

 

4. 做一次基础备份

        barman backup pg

基础备份文件位于 /var/lib/barman/pg/base 中。

 

5. 设置常规的定时备份方案

 

设置每5分钟检查一次barman 服务的状态,进行一次维护操作:

echo "*/5 * * * * barman barman cron" >> /etc/crontab

 

其中,barman cron这条命令还可以维护barman后台的“稳态”。例如,它可以检查备份文件是否缺失,清理过期的备份文件。

 

设置 7天做一次基础备份:

echo "* * */7 * * barman barman backup pg" /etc/crontab

 

3.2 恢复

  1. 在数据库服务器上停止数据库。

 

  1. 在数据库服务器,将数据库的data目录的属主修改为barman

chown -R barman.barman ./data

 

  1. 在barman 服务器上,执行命令恢复数据库。

3.1 首先查看有哪些基础备份:

    barman list-backup pg

结果示例如下:

pg 20200817T120000 - Mon Aug 17 12:00:00 2020 - Size: 515.6 MiB - WAL Size: 64.0 MiB - WAITING_FOR_WALS

pg 20200810T120000 - Mon Aug 10 12:00:00 2020 - Size: 412.5 MiB - WAL Size: 128.0 MiB

 

可以看到,pg有2个备份,分别是20200824T120000和20200817T120000。

 

3.2 恢复数据库,我们选择用20200810T120000恢复,恢复到 2020年8月23日 12时。

barman recover --target-time '2020-08-23 12:00:00' pg 20200810T120000 /opt/postgresql/data/

 

可以使用四个互斥选项之一指定恢复目标:

--target-time TARGET_TIME:指定时间戳

--target-xid TARGET_XID:指定交易ID

--target-name TARGET_NAME:指定先前使用pg_create_restore_point(name)函数创建的命名还原点

--target-immediate:达到一致状态(即基本备份过程的结束)时,恢复将结束

恢复成功后,会收到提示:

Your PostgreSQL server has been successfully prepared for recovery!

 

  1. 在PostgreSQL服务器上,将data的属主改回原来的用户。如果以前的用户是 postgres,那么命令就是:

chown -R postgres.postgres ./data

 

  1. 重新启动 PostgreSQL,检查服务是否正常。

 

  1. 数据库服务器上的postgresql正常后,在备份服务器上

barman receive-wal --create-slot pg

 

  1. 在备份服务器上,在从数据库服务端接收wal日志

barman receive-wal pg &

 

 

附录

1 不同版本barman 对系统和软件的要求

 

Barman 2.4 ~ 2.7

  • Linux/Unix
  • Python 2.6 or 2.7
  • Python modules:
    • argcomplete
    • argh >= 0.21.2 <= 0.26.2
    • argparse (Python 2.6 only)
    • psycopg2 >= 2.4.2
    • python-dateutil <> 2.0
    • setuptools
    • PostgreSQL >= 8.3
    • rsync >= 3.0.4 (对 PostgreSQL >= 9.2 是可选的)

 

Barman 2.8 ~ 2.11

  • Linux/Unix
  • Python >= 3.4
  • Python modules:
    • argcomplete
    • argh >= 0.21.2
    • psycopg2 >= 2.4.2
    • python-dateutil
    • setuptools
    • PostgreSQL >= 8.3
    • rsync >= 3.0.4 (对于 PostgreSQL >= 9.2 是可选的)

2 barman 用户手册

http://docs.pgbarman.org/release/2.11/

 

posted @ 2020-09-23 11:09  草色青青送马蹄  阅读(957)  评论(0编辑  收藏  举报