暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

部署独立的pgBackRest 备份主机

原创 墨香溪 2024-12-29
76

在前面简单的实验中,将pgbackrest 放在数据库主机primary上,下面部署到另外一台机器 repo 上去。

安装

安装和之前的大同小异。值得注意的是,数据库主机上的pgbackrest版本和备份机上的pgbackrest的版本应该一样。

1. 添加用户pgbackrest, 虽然也可以用postgres 用户,但是不推荐,因子容易混淆。

groupadd pgbackrest
adduser -gpgbackrest -n pgbackrest
复制

2. 安装依赖

yum install postgresql-libs libssh2  libxml2-devel lz4-devel libzstd-devel bzip2-devel libyaml-devel libssh2-devel compat-openssl10.x86_64
rpm -ivh pgbackrest-2.51-2PGDG.rhel7.x86_64.rpm
复制

3. 创建相关目录

mkdir -p -m 770 /var/log/pgbackrest
chown pgbackrest:pgbackrest /var/log/pgbackrest
mkdir -p /etc/pgbackrest
mkdir -p /etc/pgbackrest/conf.d
touch /etc/pgbackrest/pgbackrest.conf
chmod 640 /etc/pgbackrest/pgbackrest.conf
chown pgbackrest:pgbackrest /etc/pgbackrest/pgbackrest.conf
##备份目录
mkdir -p /backup/pgbackrest
chmod 750 /backup/pgbackrest
chown pgbackrest:pgbackrest /backup/pgbackrest
复制

配置

pgbackrest 可以使用SSH 免密,也可以使用TLS配置。

1. 配置免密

repo 机器上以pgbackrest 用户执行 ssh-keygen, 生成密钥。

数据库主机 primary 的postgres 用户同样执行ssh-keygen 生成密钥。

然后相互用ssh-copy-id 交换密钥,配置免密登陆

repo 用pgbackrest 用户执行:

ssh-copy-id postgres@primary
复制

数据库主机primary 用postgres 用户执行:

ssh-copy-id pgbackrest@repo
复制

2. 参数配置

repo 的/etc/pgbackrest/pgbackrest.conf 配置 pg1-host/pg1-host-user and pg1-path

#配置stanza
[newpgdb1]
#数据库主机名称/IP
pg1-host=primary
#数据库主机用户
pg1-host-user=postgres
#数据库的PGDATA 目录
pg1-path=/data/ivandb/pgdb1
#pg1-port 数据库端口,默认为5432,可以不配,如果使用默认端口的话。
pg1-port=6432

[global]
#备份存放目录
repo1-path=/backup/pgbackrest
repo1-retention-full=2
start-fast=y
复制

数据库主机的配置:/etc/pgbackrest/pgbackrest.conf  配置 repo1-host/repo1-host-user

[newpgdb1]
 #对应$PGDATA
pg1-path=/data/ivandb/pgdb1
#数据库端口,默认为5432,可以不配,如果使用默认端口的话。
pg1-port=6432

[global]
log-level-file=detail
repo1-host=repo
repo1-host-user=pgbackrest
复制

数据库的归档命令中要加入:

pgbackrest --stanza=newpgdb1 archive-push %p

3. 创建并检查

repo 上以pgbackrest用户执行:

pgbackrest --stanza=newpgdb1 stanza-create
复制

数据库主机primary上用postgres 用户检查:

pgbackrest --stanza=newpgdb1 check
复制

repo 主机上的检查:

pgbackrest --stanza=newpgdb1 check
复制

输出大概如下:

2024-12-29 20:07:07.485 P00   INFO: check command begin 2.51: --exec-id=639767-4ab86028 --log-level-console=detail --pg1-host=primary --pg1-host-user=postgres --pg1-path=/data/ivandb/pgdb1 --pg1-port=6432 --repo1-path=/backup/pgbackrest --stanza=newpgdb1
2024-12-29 20:07:08.248 P00   INFO: check repo1 configuration (primary)
2024-12-29 20:07:08.451 P00   INFO: check repo1 archive for WAL (primary)
2024-12-29 20:07:09.053 P00   INFO: WAL segment 0000000400000002000000ED successfully archived to '/backup/pgbackrest/archive/newpgdb1/14-1/0000000400000002/0000000400000002000000ED-67908f3610e1f90860cb02244ae4d2e5887d3123.gz' on repo1
2024-12-29 20:07:09.156 P00   INFO: check command end: completed successfully (1672ms)
复制

4. 执行备份

在repo 主机上以pgbackrest用户执行:

pgbackrest --stanza=newpgdb1 backup
复制

输出大概如下:

2024-12-29 19:59:00.016 P00   INFO: backup command begin 2.51: --exec-id=639138-202cf974 --log-level-console=detail --pg1-host=primary --pg1-host-user=postgres --pg1-path=/data/ivandb/pgdb1 --pg1-port=6432 --repo1-path=/backup/pgbackrest --repo1-retention-full=2 --stanza=newpgdb1 --start-fast
WARN: no prior backup exists, incr backup has been changed to full
2024-12-29 19:59:00.891 P00   INFO: execute non-exclusive backup start: backup begins after the requested immediate checkpoint completes
2024-12-29 19:59:01.595 P00   INFO: backup start archive = 0000000400000002000000EA, lsn = 2/EA000028
2024-12-29 19:59:01.595 P00   INFO: check archive for prior segment 0000000400000002000000E9
。。。
2024-12-29 20:00:40.937 P00   INFO: execute non-exclusive backup stop and wait for all WAL segments to archive
2024-12-29 20:00:41.139 P00   INFO: backup stop archive = 0000000400000002000000EA, lsn = 2/EA004F70
2024-12-29 20:00:41.153 P00 DETAIL: wrote 'backup_label' file returned from backup stop function
2024-12-29 20:00:41.154 P00   INFO: check archive for segment(s) 0000000400000002000000EA:0000000400000002000000EA
2024-12-29 20:00:41.877 P00   INFO: new backup label = 20241229-195856F
2024-12-29 20:00:42.066 P00   INFO: full backup size = 3GB, file total = 1472
2024-12-29 20:00:42.066 P00   INFO: backup command end: completed successfully (102050ms)
2024-12-29 20:00:42.067 P00   INFO: expire command begin 2.51: --exec-id=639138-202cf974 --log-level-console=detail --repo1-path=/backup/pgbackrest --repo1-retention-full=2 --stanza=newpgdb1
2024-12-29 20:00:42.105 P00   INFO: expire command end: completed successfully (38ms)
复制












「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论