Barman备份方案介绍

article/2025/9/23 18:59:39

Barman 是一款开源的基于Python开发的集备份和恢复为一体的优秀PG数据库备份工具,它可以对单个/多个PG数据库进行远程/本地备份,用以增强业务数据的安全性,为DBA提供恢复数据库提供可靠的帮助。

Barman 优点

  1. 支持对多版本PG的备份, 已知支持PG 8.2及之后的所有版本
  2. 基于数据库全量的备份和WAL的实时备份,有效的满足RPO≈0的需求
  3. 支持对数据库文件级的增量备份,依赖于rysnc和hard link
  4. 从PG 9.2之后,支持并发(一致性)模式的备份方式,避免排他模式下数据文件中数据不一致的情况。
  5. 提供流复制方式的WAL传输备份,减少WAL备份的延迟,保证WAL冗余备份的同时,避免数据库节点上WAL丢失无法还原的情况。
  6. 支持压缩传输和带宽限制
  7. 提供可靠的监控信息。用于收集Barman备份及服务状态的报告。
  8. 可集中化管理的备份集目录,用户便于获取备份数据文件和WAL文件等相应的信息。
  9. 自定义的备份集保留策略,以满足业务部门的需求。
  10. 从2.2版本开始支持并行的备份和恢复,加速备份和恢复的过程。
  11. 支持本地及远程的恢复

Barman部署

Barman 安装
目前barman被集成到PG的yum源中,可以通过PG官网提供的yum源获取安装包进行安装:
https://www.postgresql.org/download/linux/redhat/
以版本PG版本11为例
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
可以获取到yum源中barman各个版本的信息
yum provides barman
yum install -y barman

Barman配置

以流复制为例(该方式Barman和PostgreSQL 之间不依赖SSH通讯):
在这里插入图片描述
目标数据库:
创建barman需要的用户

psql=# create user barman password '<encrypt_password>' superuser;
psql=# create user streaming_barman password '<encrypt_password>' superuser;

数据参数与访问规则配置
Postgresql.conf

# WAL级别设置至少保证archive(v9.5为replica)以上的
wal_level = replica
# 开启归档模式
archive_mode = on
# 如果使用流复制的方式传输WAL到barman,archive_command则不是必须项
archive_command = ' '
# 最大WAL sender进程的数量,总数 > wal archive + streaming/logical replication
max_wal_senders = 5
# 最少保留WAL文件的数量,防止WAL被服务节点过早移除
wal_keep_segments = 64

Pg_hba.conf

           # 访问控制,允许barman的用户分别以superuser和replication的权限访问数据库
host    all    barman            0.0.0.0/0               md5
host    replication     streaming_barman  0.0.0.0/0               md5

可以通过以下命令验证流复制是否成功

psql "dbname=postgres port=5432 replication=database user=streaming_barman password=<encrypt_password>" -c "IDENTIFY_SYSTEM;"

正常可以获得如下信息:

   systemid               | timeline |     xlogpos    |  dbname  
---------------------------------+-----------+-----------------+----------6722702146844265873 |        3    | A/A4001AE8 | postgres
(1 row)

Barman服务配置
Global 配置信息

[barman]
# barman 备份集存放的目录
barman_home = barman
barman_user = barman
# barman 服务日志输出的位置
log_file = var/log/barman/barman.log
# 服务日志输出的级别
log_level = INFO
# 对于PG9.6之后的建议使用一致性备份方式
backup_options = concurrent_backup
# 开启备份压缩
compression = gzip
minimum_redundancy = 0
# 备份集,备份集中数据,WAL文件的保留策略
retention_policy = RECOVERY WINDOW OF 1 WEEK
retention_policy_mode = auto
wal_retention_policy = main
# 指定目标数据库备份配置的所在目录
configuration_files_directory = etc/barman.conf.d
check_timeout = 5

Individual(目标数据库备份) 配置信息

[<target_database>]
description =  "<target_database> master database(Streaming Only)"
# Barman通过conninfo连接字符
conninfo = host=<target_database> port=5432 user=barman dbname=postgres password=<encrypt_password>
# 建立流复制的连接字符串和WAL流复制归档方式
streaming_conninfo = host=target_database port=5432 user=streaming_barman dbname=postgres password=<encrypt_password>
# 开启流复制方式的归档
streaming_archiver = on
# 通过插槽的方式管控WAL的流传输
slot_name = streaming_barman
# 建议针对数据库的备份方式
backup_method = postgres
backup_options = concurrent_backup
# 通过流复制传输的WAL存放的位置
streaming_wals_directory=/barman/<target_database>/streaming
minimum_redundancy = 2
retention_policy = RECOVERY WINDOW OF 1 DAYS
# 指定Barman服务器客户端的安装位置
path_prefix=/usr/pgsql-10/bin

备份目标库
备份前验证

#验证备份服务配置的状态,确保所有状态OK后,才可以开启备份barman check <target_database>Server <target_database>:PostgreSQL: OKis_superuser: OKPostgreSQL streaming: OKwal_level: OKreplication slot: OKdirectories: OKretention policy settings: OKbackup maximum age: OK (no last_backup_maximum_age provided)compression settings: OKfailed backups: OK (there are 0 failed backups)minimum redundancy requirements: OK (have 3 backups, expected at least 3)pg_basebackup: OKpg_basebackup compatible: OKpg_basebackup supports tablespaces mapping: OKarchive_mode: OKarchive_command: OKcontinuous archiving: OKpg_receivexlog: OKpg_receivexlog compatible: OKreceive-wal running: OKarchiver errors: OK

发起备份

$ barman backup <target_database>
# 加入到crontab
crontab -e
0 23 * * * usr/bin/barman backup <target_database> > barman/<target_database>/base/barman.log 2>&1

恢复目标库
恢复前最好保留目标数据库的数据文件目录,避免恢复失败时无法复原。

基于全量恢复

$ barman recover <target_database> 20191127T220002 ${PGDATA}

基于PITR的恢复

$ barman recover --target-time "2019-11-27 15:00:00" <target_database> 20191127T220002 ${PGDATA}

远程恢复目标机

$ barman recover --remote-ssh-command "ssh postgres@<target_host>" --target-time "2019-11-27 15:00:00" <target_database> 20191127T220002 ${PGDATA}

日常维护命令

#列出所有的备份服务信息(--minimal 参数仅显示服务名称,不包含描述信息)
$ barman list-server
#列出单个备份服务的所有配置信息
$ barman show-server <target_database>
#列出单个备份服务的备份集信息
$ barman list-backup <target_database># 查看单个备份集的信息
$ barman show-backup <target_database> <backup_id>检查备份服务配置状态
$ barman check <target_database>
检查备份服务的实时信息
$ barman status <target_database>以Json格式列出所有备份服务及备份集的详细信息
$ barman diagnose

更多信息可以参考barman --help

故障排错

PostgreSQL: Failed
未给barman配置具有superuser权限的用户访问PG服务器

psql=# create user barman password '<encrypt_password>' superuser;

或者

$ createuser -s -p 5432 -Upostgres -d postgres barman

continuous archiving:Failed
检查目标WAL归档是否正常

receive-wal running: Failed
可以尝试通过重建replication slot刷新WAL的接收状态

barman receive-wal --stop <target_database>
barman receive-wal --reset <target_database>
barman receive-wal --create-slot <target_database>
barman switch-wal <target_database>

WAL archive: FAILED (please make sure WAL shipping is setup)
强行切换WAL使其归档到Barman服务器

$ barman switch-wal --force --archive <target_database>
The WAL file 000000020000001400000096 has been closed on server '<target_database>'
Waiting for the WAL file 000000020000001400000096 from server '<target_database>' (max: 30 seconds)
Processing xlog segments from streaming for fcden1pgs01_master
000000020000001400000096

参考:

第二象限官方文档 http://docs.pgbarman.org/release/2.11/index.html


http://chatgpt.dhexx.cn/article/5PpyepH1.shtml

相关文章

PostgreSQL 备份恢复工具之 Barman

文章目录 Barman 简介Barman 与 pg_dump 对比 Barman 简介 Barman&#xff08;Backup and Recovery Manager&#xff0c;备份与恢复管理器&#xff09;是一个用于 PostgreSQL 数据库灾难恢复的开源管理工具&#xff0c;使用 Python 编写。Barman 使得企业能够执行多个关键业务…

使用 barman的备份和归档PostgreSQL

1 前言 1.1 Barman简介 barman&#xff08;备份和恢复管理器&#xff09;是用于PostgreSQL服务器进行灾难恢复的开源管理工具&#xff0c;是以Python编写的。它支持对多台服务器执行远程备份&#xff0c;以降低风险并帮助DBA进行数据库恢复。 1.2 Barman的备份方式 本文假定…

github删除某个项目流程

1.进入网站 2.找到要删的项目&#xff0c;点进去 3.进去之后往下拉&#xff0c;拉到这里 接下来就点它按提示操作就删除了。

GitLab删除项目操作(亲测)

刚开始找了半天没找到删除按钮在哪&#xff0c;现在记录一下&#xff0c;分享。 第一步&#xff1a;点进入项目 第二步&#xff1a;进入项目Settings 第三步&#xff1a;往下拉&#xff0c;找到Remove,删除即可。 所有内容皆为个人总结或转载别人的文章&#xff0c;只为学习技…

删除gitlab上的分支

好久没有更新了, 今天记录一下删除gitlab上的分支的操作 登录仓库后, 依次点击: project --> home --> Readme --> repository --> branches 会看到所有master和所有分支, 点击右侧删除即可; 看到右边的红色垃圾桶了没, 删除按钮, 想要删除那个就点那个即可; 最近很…

GitLab如何删除已有项目

一。点击进入你需要删除的项目&#xff1a; 二。选择Settings&#xff0c;点击General&#xff0c;找到Advanced settings&#xff0c;点击Expand&#xff1a; 三。往下翻&#xff0c;找到Remove Project&#xff0c;点击&#xff1a; 四。根据提示&#xff0c;输入test&#x…

gitlab中如何删除Group,删除项目

删除自己创建的组&#xff1a; 一、首先查看自己的组&#xff0c;然后edit group,点击进入&#xff1b; 二、点击Settings中的General&#xff0c;然后点击Path, transfer, remove的Expend按钮&#xff1b; 3、滑到最下边&#xff0c;点击“Remove group”; 4、在弹出框中输入…

删除git、gitlab的分支

1.使用命令 删除本地分支 git branch -d dev 【git branch -参数 本地分支名称】删除远程分支 git push origin --delete dev 【git push origin --参数 远程分支名称】 2.在gitlab上删除分支 进入某个项目&#xff0c;例bootdo这个项目&#xff0c;bootdo->左右菜单Repos…

gitlab删除仓库文件夹

如图&#xff1a; 要删除router 首先进入项目master文件夹下, Git Bash Here ,打开命令窗口 $ git pull origin master # 将远程仓库里面的项目拉下来 $ dir # 查看有哪些文件夹 $ git rm -r --cached target # 删除target文件夹 $ git commit -m ‘删除了target’ # 提交…

gitlab 创建、修改、删除项目

一、创建用户组 1、setting --> Groups --> New group 2、填写用户组信息&#xff0c;点击 "Create group"。 3、选择需要加入改组的 用户和角色&#xff0c;点击 "Add users to group" 二、创建项目 1、点击 "New project" 2、填写项…

github上如何删除一个项目(仓库)

最近在学习使用git来管理项目&#xff0c;在学习管理远程库时&#xff0c;使用github来管理&#xff0c;创建的项目后来发现不需要这个项目了&#xff0c;想删除&#xff0c;找了好久都没找到在哪删除&#xff0c;在这里特意记录下来 首先创建一个Repositories 点击New reposi…

GitLab删除项目的操作步骤

1.进入项目首页 2.选择Settings-General 3.划至底部找到Advanced,点击右侧"Expand" 4.往下划可看到删除项目的按钮&#xff0c;点击后按操作提示即可

gitlab上删除项目

最近公司打算把不用svn而是git来托管代码. 然后我自己在公司的gitlab上建立了一个测试项目,这时候删除很容易直接在本页面上.一旦我上传了一些东西之后再删除就隐藏的比较深了.然后上网查的百度教程,发现不行.原因可能是git版本更新完之后,位置变了. 摸索了一通之后终于找到了…

如何删除gitlab上的文件夹

参考&#xff1a;如何删除gitlab上的文件夹 1.在本地新建一个文件夹&#xff0c;进入文件夹&#xff0c;右键git bash here。 2.在命令行窗口输入拉去gitlab仓库的命令&#xff0c;git clone "仓库地址"&#xff0c;然后查看是否拉取成功。 3.在本地把需要删除的文件…

gitlab怎么删除项目

首先点击setting–general–advanced-expand展开 滑到最下面找到remove project 将要删除的项目名称输入到框里&#xff0c;删除

怎样删除Github中的项目

我们在GitHub上创建项目的时候&#xff0c;如果想要删除当前项目&#xff0c;怎样进行操作呢&#xff0c;下面就简单介绍一下怎样去删除GitHub中的项目 1、选择要删除的项目 2、进入项目中的setting&#xff08;设置&#xff09;中 3、一直下拉&#xff0c;看到有红色字体出现&…

如何在GitLab与GitHub中删除项目(仓库)

文章目录 一、GitLab中删除项目1、进入Projects找到要删除的项目2、选中左下角Settings中的General3、点击Advanced栏下的Expand4、然后滑到最下面的Remove project5、输入要删除项目的名字&#xff0c;确认删除 二、GitHub中删除仓库1、找到要删除的仓库&#xff0c;点击右上方…

删除Github中项目步骤

这里写自定义目录标题 删除Github中项目步骤1、选择要删除项目2、点击“Setting”3、一直拉到最后&#xff0c;选择“Delete this repository”4、位置①就是你的项目名称&#xff0c;直接复制到位置②&#xff0c;点击位置③&#xff0c;即完成删除Github项目。 删除Github中项…

删除gitlab上仓库的分支

方法一&#xff1a;使用git命令来删除分支 1、进入相应的仓库&#xff0c;然后使用 git branch -a 命令查看该仓库所有的分支 2、删除相应的分支&#xff0c;这里以删除 “Redefine-PinDir-for-MoroccoA” 为例&#xff0c;执行git push origin --delete Redefine-PinDir-for…