博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Barman最佳实践
阅读量:2019 次
发布时间:2019-04-28

本文共 8832 字,大约阅读时间需要 29 分钟。

作者:崔鹏,曾获得中国PostgreSQL数据库管理高级工程师(PGCM),是PostgreSQL官方认证讲师。

Barman介绍

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

Barman的备份方式

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

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

备份功能

增量备份

Barman实现文件级增量备份。增量备份是一种完全定期备份,仅保存特PostgreSQL服务器目录中可用的最新完全备份中的数据更改。请勿将其与通过WAL连续归档实现的差异备份相混淆。

Barman中增量备份的主要目标是:

  1. 减少完整备份过程所需的时间。
  2. 减少几次定期备份(重复数据删除)所占用的磁盘空间。
  3. 此功能在很大程度上依赖于rsync和硬链接,它因此必须通过底层操作系统和文件系统中的备份数据驻留均能够支持。

后续的基本备份将共享自上次备份以来未更改的那些文件,从而节省了磁盘使用量。

Barman通过名为的全局/服务器选项实现reuse_backup了透明备份的透明管理barman backup。它接受三个值:

  • off:标准完全备份(默认)
  • link:增量备份,通过重用服务器的上次备份并创建未更改文件的硬链接(以节省备份空间和时间)
  • copy:增量备份,方法是为服务器重用上次备份并创建未更改文件的副本(仅用于减少备份时间)

可以reuse_backup通过命令的--reuse-backup运行时选项覆盖该选项的设置barman backup。同样,运行选项接受三个值:off,link和copy。例如,您可以按以下方式运行一次性增量备份:

barman backup --reuse-backup=link 

限制带宽使用bandwidth_limit通过指定每秒最大千字节数,可以通过选项(全局/每台服务器)限制I / O带宽的使用。默认情况下,它设置为0,表示没有限制。

如果有多个表空间,并且希望将备份过程的I / O工作量限制在一个或多个表空间上,则可以使用tablespace_band width_limit选项(全局/每服务器):

tablespace_band width_limit = tbname:bwlimit[, tbname:bwlimit, ...]

该选项接受以逗号分隔的成对列表,该对列表由表空间名称和带宽限制(以千字节/秒为单位)组成。

备份服务器时,Barman将尝试在上述选项中找到任何现有的表空间。如果找到,将强制执行指定的带宽限制。否则,将应用该服务器的默认带宽限制。

网络压缩使用压缩可以减少传输数据的大小。可以使用network_compression选项启用(全局/每个服务器):

将此选项设置为true将会在网络传输期间启用数据压缩(用于备份和恢复)。默认情况下,它设置为false。

并发备份和备用数据库备份,通常,在备份操作过程中,Barman使用PostgreSQL本机函数pg_start_backup并pg_stop_backup用于独占备份。只读备用服务器上不允许执行这些操作。

Barman还能够主要通过配置参数以并发方式从9.2或更高版本的数据库服务器执行PostgreSQL备份backup_options。这为Barman引入了一种新的体系结构方案:使用从备用服务器进行备份rsync。

默认情况下,出于向后兼容的原因,backup_options将其透明设置exclusive_backup为。PostgreSQL 9.6及更高版本的用户应设置backup_options为concurrent_backup。

当backup_options设置concurrent_backup为时,Barman会为服务器激活并发备份模式,并遵循以下两个简单规则:

ssh_command必须指向目标Postgres服务器。

conninfo必须指向目标Postgres数据库上的数据库。使用PostgreSQL 9.2、9.3、9.4和9.5,pgespresso必须通过正确安装CREATE EXTENSION。使用9.6或更高版本时,并发备份是通过Postgres本机API执行的(从备份的开始到结束需要有效的连接)。

当前从备用数据库进行备份的限制,Barman当前要求备份数据(基本备份和WAL文件)仅来自一台服务器。

实验环境

两台服务器,下面是一些基本信息。

操作系统: CentOS 7.6

内存:16G
CPU:8个逻辑核

软件:

PostgreSQL 11.4
yum
python 3.7
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地址:172.16.0.189
备份服务器的IP地址:172.16.0.190
ssh 端口:22,默认值
postgresql 的运行端口:5432
postgresql 的 bin 目录 /opt/pgsql/bin/
postgresql 的 data 目录 /opt/pg_root

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

1. Barman 备份环境的搭建

1.1 安装软件

1.1.1 数据库服务器上的操作

1. 安装 rsync

yum install rsyncs

1.1.2 备份服务器上的操作

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

1. 安装 epel 源 (Extra Packages for Enterprise Linux)

yum -y install epel-release

安装 Python3

yum -y install python3

安装 rsync

yum -y install rsync

安装 pgdg 源

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

安装Barman

使用 yum 安装 barman

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

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

python36-argcomplete.x86_64python36-arghpython36-psycopg2python36-dateutil

使用 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/

在操作系统中创建用户 barman

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

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

cd barman-2.11/barmancp barman.conf  /etc/cp -R barman.d  /etc/

2. 配置

2.1.1 数据库服务器上的操作

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

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

su - postgrescd ~ssh-keygen -t rsaecho “”>> ./ssh/authorized_key

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

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

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

在数据库中创建用户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;

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

listen_addresses = '*'wal_level = replicamax_wal_senders = 20archive_mode = off

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

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

host      all           barman                   127.0.0.1/32            md5host      all           barman                   172.16.0.189/32        md5host      all           barman                   172.16.0.190/32        md5host      replication    streaming_barman         127.0.0.1/32            md5host      replication    streaming_barman         172.16.0.189/32        md5host      replication    streaming_barman         172.16.0.190/32        md5

 2.1.2 备份服务器上的操作

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

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

ssh-keygen -t rsa

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

复制用户barman的公钥文件 id_rsa.pub 中的内容,并将它追加到数据库服务器上的用户postgres的主目录中的文件 .ssh/authorized_keys 中。这样做的目的是允许barman以用户postgres的身份免密访问数据库服务器。

如果使用下面的命令,你需要保证数据库服务器上postgres用户已经设置了密码。

ssh-copy-id postgres@172.16.0.189

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

mkdir /var/log/barman

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

System userbarman_user = barmanDirectory of configuration files. Place your sections in separate files with .conf extensionFor example place the 'main' server section in /etc/barman.d/main.confconfiguration_files_directory = /etc/barman.d ; Main directorybarman_home = /var/lib/barman Log locationlog_file = /var/log/barman/barman.log  Log level (see https://docs.python.org/3/library/logging.html#levels)log_level = INFOGlobal retention policy (REDUNDANCY or RECOVERY WINDOW) - default emptyretention_policy = RECOVERY WINDOW OF 4 WEEKSNumber of parallel jobs for backup and recovery via rsync (default 1)parallel_jobs = 3Immediate checkpoint for backup command - default falseimmediate_checkpoint = trueEnable network compression for data transfers - default falsenetwork_compression = false Number of retries of data copy during base backup after an error - default 0basebackup_retry_times = 3 Number of seconds of wait after a failed copy, before retrying - default 30basebackup_retry_sleep = 30Minimum 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 所需的最小备份数量

配置要备份的数据库的信息

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

cd /etc/barman.dcp streaming-server.conf-template pg.conf

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

conninfo = host=172.16.0.189 port=5432 user=barman dbname=postgres password=barman123 streaming_conninfo = host=172.16.0.189 port=5432 dbname=postgres user=streaming_barman password=streaming_barman123backup_method = postgresstreaming_archiver = onslot_name = barmanpath_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 备份

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

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

barman receive-wal --create-slot pg

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

barman receive-wal pg &

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

barman check pg

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

基础备份

barman backup pg

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

设置常规的定时备份方案

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

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

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

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

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

3.2 恢复

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

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

chown -R barman.barman ./data

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

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

barman list-backup pg

结果示例如下:

pg 20210417T120000 - Mon Aug 17 13:00:00 2020 - Size: 260.3 MiB - WAL Size: 64.0 MiB - WAITING_FOR_WALSpg 20210410T120000 - Mon Aug 10 13:00:00 2020 - Size: 258.2 MiB - WAL Size: 128.0 MiB

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

 
3.2 恢复数据库,我们选择用20210417T120000恢复,恢复到2021年4月17日12时。

barman recover --target-time '2021-04-17 12:00:00' pg 20210417T120000 /opt/pg_root

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

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

barman receive-wal --create-slot pg

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

barman receive-wal pg &

参考文档 

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

转载地址:http://rmmxf.baihongyu.com/

你可能感兴趣的文章
Bash String Manipulation Examples – Length, Substring, Find and Replace--reference
查看>>
oracle 查看表属主和表空间sql
查看>>
linux shell执行方式
查看>>
xmemcached user guide --存档
查看>>
linux shell wc 命令
查看>>
Android 4.0 中的安全技术 ASLR 被爆“无效”
查看>>
【动态规划】装配线调度代码
查看>>
【动态规划】矩阵链乘法代码
查看>>
【动态规划】最长递增子序列代码(UVA 10131)
查看>>
【动态规划】多部图问题代码
查看>>
一道腾讯笔试题的思路
查看>>
【动态规划】每对顶点之间的最短路径之Floyd-Warshall算法
查看>>
2020BATJ面试系列:145个技术题高端技术题,助你拿offer!
查看>>
从简历被拒,到斩获字节跳动offer,这份学习集合功不可没!
查看>>
Android自定义view系列:手撸一个带点儿科技感的仪表盘!
查看>>
Flutter 学习路线图!跨平台开发必备,不可错过的Flutter进阶历程!
查看>>
使用libnet, libpcap模拟一个交换机
查看>>
AVR-GCC里定义的API
查看>>
关注地震,关爱灾民......
查看>>
Compiling FlightGear 1.0.0 with Visual C++.net 2005
查看>>