先用pgbackrest搭建一个备库。
主库为:primary
备库主机为:standby, 已安装postgreSQL 软件 以及 pgbackrest
备份机为:repo
1. 修改备份机器repo上的配置
之前的配置:
#配置stanza
[newpgdb1]
#数据库主机名称/IP
pg1-host=primary
#数据库主机用户
pg1-host-user=postgres
#数据库的PGDATA 目录
pg1-path=/data/ivandb/pgdb1
#pg1-port 数据库端口,默认为5432,可以不配,如果使用默认端口的话。
pg1-port=6432
log-level-console=detail
[global]
#备份存放目录
repo1-path=/backup/pgbackrest
repo1-retention-full=2
start-fast=y
复制
添加备库的信息在[newpgdb1] 下面:
#以下为standby 配置信息
pg2-host=standby
pg2-host-user=postgres
pg2-path=/data/ivandb/pgdb1
pg2-port=6432
#允许从备库发起备份
backup-standby=y
实际上,这里配置了backup-standby=y, 以及备库的相关信息,也就完成了在备库发起备份的配置,当然,配置repo和standby 之间的互信也是必要的,后面搭建备库的时候,会做。
2. 备库配置
2.1 standby 创建目录,授权, root 执行
#pgbackrest 目录
mkdir -p -m 770 /var/log/pgbackrest
chown postgres:postgres /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 postgres:postgres /etc/pgbackrest/pgbackrest.conf
#数据库目录
mkdir -p /data/ivandb/pgdb1
chown -R postgres:postgres /data/ivandb
复制
2.2 standby 配置 /etc/pgbackrest/pgbackrest.conf 用 postgres 用户
参数如下:
[newpgdb1]
pg1-path=/data/ivandb/pgdb1
pg1-port=6432
[global]
log-level-file=detail
repo1-host=repo
repo1-host-user=pgbackrest
复制
2.3 配置互信。
在repo 上用pgbackrest用户执行:
ssh-copy-id postgres@standby
复制
在standby 上用postgres 用户执行:
ssh-copy-id pgbackrest@repo
复制
稍微检查一下配置成功否,在备库上执行以下命令,应能准确输出备份信息:
pgbackrest --stanza=newpgdb1 info
复制
2.4 创建实时同步的备库
1 在主库创建同步用户 replicator
create user replicator password 'jw8s0F4' replication;
复制
2 配置主库pg_hba.conf文件,以允许备库通过 replicator 用户同步数据,直接配置了trust,因为是写死了standby ip 10.86.xx.xxx
host replication replicator 10.86.xx.xxx/32 trust复制
重新加载pg_hba.conf
pg_ctl -D $PGDATA reload
复制
3 在备库的/etc/pgbackrest/pgbackrest.conf 多加一行:
recovery-option=primary_conninfo=host=primary port=6432 user=replicator
复制
4 在备库发起恢复,创建备库
pgbackrest --stanza=newpgdb1 --delta --type=standby restore --log-level-console=detail
复制
输入大致如下:
2024-12-31 14:37:47.940 P00 INFO: restore command begin 2.51: --delta --exec-id=26027-d22cf430 --log-level-console=detail --log-level-file=detail --pg1-path=/data/ivandb/pgdb1 --recovery-option="primary_conninfo=host=primary port=6432 user=replicator" --repo1-host=repo --repo1-host-user=pgbackrest --stanza=newpgdb1 --type=standby
WARN: --delta or --force specified but unable to find 'PG_VERSION' or 'backup.manifest' in '/data/ivandb/pgdb1' to confirm that this is a valid $PGDATA directory. --delta and --force have been disabled and if any files exist in the destination directories the restore will be aborted.
2024-12-31 14:37:48.330 P00 INFO: repo1: restore backup set 20241229-195856F_20241231-133811D, recovery will start at 2024-12-31 13:38:11
2024-12-31 14:37:48.330 P00 DETAIL: check '/data/ivandb/pgdb1' exists
。。。。。。
2024-12-31 14:38:22.440 P00 INFO: restore global/pg_control (performed last to ensure aborted restores cannot be started)
2024-12-31 14:38:22.440 P00 DETAIL: sync path '/data/ivandb/pgdb1/global'
2024-12-31 14:38:22.441 P00 INFO: restore size = 3GB, file total = 1477
2024-12-31 14:38:22.442 P00 INFO: restore command end: completed successfully (34502ms)
复制
恢复完以后,检查 postgresql.auto.conf,里面应有:
primary_conninfo = 'host=primary port=6432 user=replicator'
restore_command = 'pgbackrest --stanza=newpgdb1 archive-get %f "%p"'
复制
同时检查一下 postgresql.conf 里面的参数,并做好调整,就可以准备起数据库了。
pg_ctl start -D /data/ivandb/pgdb1
起完以后,检查一下数据库是否在正常同步。
主库:
select * from pg_stat_replication;
复制
备库的搭建,到此完毕。
3. 备份机repo 发起备份
pgbackrest --stanza=newpgdb1 --log-level-console=detail backup
复制
会看到类似一下的信息:
2024-12-31 15:29:03.531 P00 INFO: backup command begin 2.51: --backup-standby --exec-id=680926-33532f9c --log-level-console=detail --pg1-host=primary --pg2-host=standby --pg1-host-user=postgres --pg2-host-user=postgres --pg1-path=/data/ivandb/pgdb1 --pg2-path=/data/ivandb/pgdb1 --pg1-port=6432 --pg2-port=6432 --repo1-path=/backup/pgbackrest --repo1-retention-full=2 --stanza=newpgdb1 --start-fast
2024-12-31 15:29:05.204 P00 INFO: last backup label = 20241229-195856F_20241231-133811D, version = 2.51
2024-12-31 15:29:05.204 P00 INFO: execute non-exclusive backup start: backup begins after the requested immediate checkpoint completes
2024-12-31 15:29:05.913 P00 INFO: backup start archive = 0000000400000002000000F5, lsn = 2/F5000028
2024-12-31 15:29:05.913 P00 INFO: wait for replay on the standby to reach 2/F5000028
2024-12-31 15:29:06.217 P00 INFO: replay on the standby reached 2/F5000028
2024-12-31 15:29:06.217 P00 INFO: check archive for prior segment 0000000400000002000000F4
复制
当在repo 中配置多个备库的信息时候,pgbackrest 发现的第一个运行的备库做用来做备份。 在配置文件中的主备数据库的信息的顺序不重要,因为pgbackrest会自动的去找哪个是备库,哪个是主库。
通过配置备库备份,在发起备份的时候,大部分的文件都会从备库去拉取备份,在主库只会复制一小部分的文件作为备份。可以减轻主库的负载。