千家信息网

如何使用rsync实现postgres日志传送standby服务器

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,这篇文章主要介绍了如何使用rsync实现postgres日志传送standby服务器,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。日志
千家信息网最后更新 2026年10月31日如何使用rsync实现postgres日志传送standby服务器

这篇文章主要介绍了如何使用rsync实现postgres日志传送standby服务器,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

日志传送standby服务器

连续归档可以配合随时准备取代失效主服务器的一个或多个备份服务器, 用于创建一个高可用性(HA)集群。这个能力通常被称为温备份或日志传送

从一个数据库服务器移动 WAL 到另一个服务器通常被称为日志传送(LogShipping)。PostgreSQL 实现了基于文件的日志传送,意思是 WAL 记录每次移动一个完整的文件(WAL 段)。 也可以基于记录的日志传送,

日志传送是异步的,也就是 WAL 记录在事务提交之后才被传送,可以使用 archive_timeout 来设置日志传送间隔时间(应该是最长间隔时间)

在启动,standby 服务器调用 restore_command 命令开始恢复在 wal 归档位置有效的所有的 WAL, 一旦恢复完可用 WAL,restore_command 就失败, 将尝试从 pg_wal 目录下恢复可用的 WAL。 如果那也失败了,并且已经配置了流复制,则尝试连接到主服务器, 从在归档或 pg_wal 中找到的最后一条有效的记录开始 WAL 流复制。如果那也失败了, 或没有配置流复制,或连接断开,备服务器再次回到步骤 1,尝试从归档里恢复文件。 循环尝试从归档、pg_wal、连续流复制通道,直到服务器停止或通过触发器文件触发失效切换。

实验使用两台主机,都安装postgresql-10.7,已配置流复制

  • 主库:192.168.56.25 m1 使用rsync命令传送wal文件到m7

  • 丛库:192.168.56.5 m7 在此机上配置rsync做为服务运行,接收丛m1传送过来的wal文件

丛库的配置,设置hot_standby使standby库接收连接。省略standby库从主库基础备份还原过程

[postgres@localhost data]$ cat recovery.confstandby_mode = 'on'restore_command = 'cp /usr/local/pg/arch_bak/%f %p'recovery_target_timeline = 'latest'postgres=# show  hot_standby; hot_standby------------- on(1 row)

m1上配置rsync服务器,使用rsync-3.1.3版本,省略rsyn安装过程

[root@localhost ~]# cat /etc/rsyncd.conf uid=postgresgid=postgresuse chroot = nomax connections = 5read only = false   pid file = /var/run/rsyncd.pidlog file = /var/log/rsync.logtransfer logging = yeslog format = %t %a %m %f %btimeout = 300[arch_bak]path = /usr/local/pg/arch_bakauth users = tridge, susansecrets file = /etc/rsyncd.secrets[wal_bak]path = /usr/local/pg/data/pg_walauth users = tridge, susansecrets file = /etc/rsyncd.secrets[root@localhost ~]# cat /etc/rsyncd.secretstridge:mypasssusan:herpass

启动rsync服务器

[root@localhost arch_bak]# /usr/local/bin/rsync --daemon[root@localhost arch_bak]# lltotal 0[root@localhost arch_bak]# pwd/usr/local/pg/arch_bak

从m7同步standby服务器创建之前归档wal日志文件

[root@z_leader rsync-3.1.3]# /usr/local/bin/rsync  -av /usr/local/pg/arch/ tridge@192.168.56.5::arch_bak/Password:sending incremental file list./00000005000000000000000B00000005000000000000000C.partial00000006.history00000006000000000000000C00000006000000000000000D00000006000000000000000E00000006000000000000000F000000060000000000000010sent 117,470,037 bytes  received 179 bytes  18,072,340.92 bytes/sectotal size is 117,440,721  speedup is 1.00

在m1上查看传输过的文件,及standby服务器表记录

[root@localhost arch_bak]# lltotal 114692-rw------- 1 postgres postgres 16777216 Mar  2 09:02 00000005000000000000000B-rw------- 1 postgres postgres 16777216 Mar  2 09:08 00000005000000000000000C.partial-rw------- 1 postgres postgres 16777216 Mar  2 11:15 00000006000000000000000C-rw------- 1 postgres postgres 16777216 Mar 17 09:51 00000006000000000000000D-rw------- 1 postgres postgres 16777216 Mar 17 09:55 00000006000000000000000E-rw------- 1 postgres postgres 16777216 Mar 17 10:13 00000006000000000000000F-rw------- 1 postgres postgres 16777216 Mar 17 20:02 000000060000000000000010-rw------- 1 postgres postgres      209 Mar  2 09:08 00000006.historypostgres=# select * from test; id | e_name |   e_mail    | d_id----+--------+-------------+------  1 | zbs    | 123@126.com |   10  3 | zbs2   | 124@126.com |   10  4 | zbs2   | 124@126.com |   10  2 | zbs1   | 124@126.com |   10  5 | zbs2   | 124@126.com |   10  6 | zbs2   | 124@126.com |   10  7 | zbs2   | 124@126.com |   10  8 | zbs2   | 124@126.com |   10(8 rows)

在主库上插入2两条记录,并归档

postgres=# select * from test; id | e_name |   e_mail    | d_id----+--------+-------------+------  1 | zbs    | 123@126.com |   10  3 | zbs2   | 124@126.com |   10  4 | zbs2   | 124@126.com |   10  2 | zbs1   | 124@126.com |   10  5 | zbs2   | 124@126.com |   10  6 | zbs2   | 124@126.com |   10  7 | zbs2   | 124@126.com |   10  8 | zbs2   | 124@126.com |   10(8 rows)postgres=# insert into test values(9,'zbs3','124@126.com',20);INSERT 0 1postgres=# insert into test values(10,'zbs3','124@126.com',20);INSERT 0 1postgres=# select pg_switch_wal(); pg_switch_wal--------------- 0/11000888(1 row)

在m7上使用rsync命令同步新生成的归档

[root@z_leader rsync-3.1.3]# /usr/local/bin/rsync  -av /usr/local/pg/arch/ tridge@192.168.56.5::arch_bak/Password:sending incremental file list./000000060000000000000011sent 16,781,696 bytes  received 46 bytes  4,794,783.43 bytes/sectotal size is 134,217,937  speedup is 8.00

在m1上查看是表数据是否同步,在主库插入的2条记录成功应用到从库

postgres=# select * from test; id | e_name |   e_mail    | d_id----+--------+-------------+------  1 | zbs    | 123@126.com |   10  3 | zbs2   | 124@126.com |   10  4 | zbs2   | 124@126.com |   10  2 | zbs1   | 124@126.com |   10  5 | zbs2   | 124@126.com |   10  6 | zbs2   | 124@126.com |   10  7 | zbs2   | 124@126.com |   10  8 | zbs2   | 124@126.com |   10  9 | zbs3   | 124@126.com |   20 10 | zbs3   | 124@126.com |   20(10 rows)

到此,使用rsyn传输wal归档文件到standby服务器使用成功,rsync只输出了新的文件000000060000000000000011

下面实验直接同步wal日志文件

m1目前wal目录

[postgres@localhost pg_wal]$ pwd/usr/local/pg/data/pg_wal[postgres@localhost pg_wal]$ lltotal 81948-rw------- 1 postgres postgres       41 Mar  2 09:24 00000002.history-rw------- 1 postgres postgres       83 Mar  2 09:24 00000003.history-rw------- 1 postgres postgres      302 Mar  2 09:24 000000040000000000000009.00000028.backup-rw------- 1 postgres postgres      125 Mar  2 09:24 00000004.history-rw------- 1 postgres postgres      167 Mar  2 09:24 00000005.history-rw------- 1 postgres postgres 16777216 Mar 23 09:38 000000060000000000000010-rw------- 1 postgres postgres 16777216 Mar 23 09:46 000000060000000000000011-rw------- 1 postgres postgres 16777216 Mar  2 09:24 000000060000000000000012-rw------- 1 postgres postgres 16777216 Mar 17 10:05 000000060000000000000013-rw------- 1 postgres postgres 16777216 Mar 17 10:15 000000060000000000000014-rw------- 1 postgres postgres      209 Mar  2 09:24 00000006.historydrwx------ 2 postgres postgres     4096 Mar 23 09:46 archive_status

在m7上查看wal目录,并输出不同的文件

[root@z_leader pg_wal]# /usr/local/bin/rsync  -av /usr/local/pg/data/pg_wal/ tridge@192.168.56.5::wal_bak/Password:sending incremental file list./00000002.history00000003.history00000004.history000000040000000000000009.00000028.backup00000005.history00000006.history000000060000000000000011000000060000000000000012000000060000000000000013000000060000000000000014000000060000000000000015archive_status/archive_status/00000002.history.donearchive_status/00000004.history.donearchive_status/000000040000000000000009.00000028.backup.donearchive_status/00000006.history.donearchive_status/000000060000000000000011.donesent 33,626,454 bytes  received 98,679 bytes  3,967,662.71 bytes/sectotal size is 83,887,007  speedup is 2.49

现在在主库上再插入两条记录

postgres=# insert into test values(11,'zbs3','124@126.com',20);INSERT 0 1postgres=# insert into test values(12,'zbs3','124@126.com',20);INSERT 0 1postgres=# select * from test; id | e_name |   e_mail    | d_id----+--------+-------------+------  1 | zbs    | 123@126.com |   10  3 | zbs2   | 124@126.com |   10  4 | zbs2   | 124@126.com |   10  2 | zbs1   | 124@126.com |   10  5 | zbs2   | 124@126.com |   10  6 | zbs2   | 124@126.com |   10  7 | zbs2   | 124@126.com |   10  8 | zbs2   | 124@126.com |   10  9 | zbs3   | 124@126.com |   20 10 | zbs3   | 124@126.com |   20 11 | zbs3   | 124@126.com |   20 12 | zbs3   | 124@126.com |   20(12 rows)

在m7同步wal日志文件到m1

[root@z_leader pg_wal]# /usr/local/bin/rsync  -av /usr/local/pg/data/pg_wal/ tridge@192.168.56.5::wal_bak/Password:sending incremental file list./000000060000000000000012000000060000000000000016archive_status/sent 16,802,460 bytes  received 24,649 bytes  3,739,357.56 bytes/sectotal size is 83,887,007  speedup is 4.99

在m1上查看是表数据是否同步,在主库插入的2条记录成功应用到从库

postgres=#  select * from test; id | e_name |   e_mail    | d_id----+--------+-------------+------  1 | zbs    | 123@126.com |   10  3 | zbs2   | 124@126.com |   10  4 | zbs2   | 124@126.com |   10  2 | zbs1   | 124@126.com |   10  5 | zbs2   | 124@126.com |   10  6 | zbs2   | 124@126.com |   10  7 | zbs2   | 124@126.com |   10  8 | zbs2   | 124@126.com |   10  9 | zbs3   | 124@126.com |   20 10 | zbs3   | 124@126.com |   20 11 | zbs3   | 124@126.com |   20 12 | zbs3   | 124@126.com |   20(12 rows)

到此,使用rsyn传输wal文件到standby服务器使用成功,rsync会认识发生变化的文件,并同步变化的部分。

感谢你能够认真阅读完这篇文章,希望小编分享的"如何使用rsync实现postgres日志传送standby服务器"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

服务 服务器 文件 日志 同步 配置 成功 篇文章 尝试 命令 备份 数据 目录 传输 有效 时间 过程 变化 实验 应用 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 湖北九亚互联网络科技有限公司 博彦科技互联网金融 互联网科技活动banner 互联网的科技 互联网和科技创新 互联网中国科技 互联网 的科技创新 华中科技大学互联网 有木互联网科技 互联网科技小额贷款 中化能源互联网科技招聘 苏州五指互联网络科技有限 深圳市优智互联网科技有限公司 电子科技大学互联网创业大赛 华中科技大学互联网的牛人 黑科技 体验场景 互联网大会 互联网时代科技型公司的机会 银川新华互联网科技学校原画 马化腾互联网科技教学 株洲百竞科技的互联网营销顾问 圆梦互联网科技 易互联网科技有限公司 互联网科技视频素材 西藏七维空间互联网科技有限公司 互联网教育科技公司vipkid 金融科技与互联网金融相同和不同 银川新华互联网科技学校联系电话 畅思互联网科技 浙南科技城互联网大厦建设工程 工业互联网科技创业
0