admin 发表于 2023-8-28 14:04:18

PolarDB for PostgreSQL 备份并恢复一个 Standby 节点

PolarDB for PostgreSQL 备份并恢复一个 Standby 节点基础备份也可以用于搭建一个新的 Standby 节点。如下图所示,Standby 节点与 Primary / Replica 节点各自使用独立的共享存储,与 Primary 节点使用物理复制保持同步。Standby 节点可用于作为主共享存储的灾备。http://www.cuug.com.cn/uploadfile/2023-08/34892317677.jpgPFS 文件系统格式化和挂载假设此时用于部署 Standby 计算节点的机器已经准备好用于后备的共享存储 nvme2n1:$ lsblkNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTnvme0n1 259:1 0 40G 0 disk└─nvme0n1p1 259:2 0 40G 0 part /etc/hostsnvme2n1 259:3 0 70G 0 disknvme1n1 259:0 0 60G 0 disk将这个共享存储格式化为 PFS 格式,并启动 PFSD 守护进程挂载到 PFS 文件系统:sudo pfs -C disk mkfs nvme2n1sudo /usr/local/polarstore/pfsd/bin/start_pfsd.sh -p nvme2n1 -w 2备份数据到本地存储和共享存储在用于部署 Standby 节点的机器上执行备份,以 ~/standby 作为本地数据目录,以 /nvme2n1/shared_data 作为共享存储目录:polar_basebackup \--host= \--port= \-D /home/postgres/standby \--polardata=/nvme2n1/shared_data/ \--polar_storage_cluster_name=disk \--polar_disk_name=nvme2n1 \--polar_host_id=3 \-X stream --progress --write-recovery-conf -v将会看到如下输出。其中,除了 polar_basebackup 的输出以外,还有 PFS 的输出日志:pfs_mount_prepare 103: begin prepare mount cluster(disk), PBD(nvme2n1), hostid(3),flags(0x13)pfs_mount_prepare 165: pfs_mount_prepare success for nvme2n1 hostid 3chnl_connection_poll_shm 1238: ack data update s_mount_epoch 1chnl_connection_poll_shm 1266: connect and got ack data from svr, err = 0, mntid 0pfsd_sdk_init 191: pfsd_chnl_connect successpfs_mount_post 208: pfs_mount_post err : 0pfsd_opendir 1437: opendir /nvme2n1/shared_data/ error: No such file or directorypfsd_mkdir 1320: mkdir /nvme2n1/shared_datapolar_basebackup: initiating base backup, waiting for checkpoint to completeWARNING: a labelfile "/nvme1n1/shared_data//polar_non_exclusive_backup_label" is already on diskHINT: POLAR: we overwrite itpolar_basebackup: checkpoint completedpolar_basebackup: write-ahead log start point: 0/16C91F8 on timeline 1polar_basebackup: starting background WAL receiverpolar_basebackup: created temporary replication slot "pg_basebackup_373"...pfsd_open 539: open /nvme2n1/shared_data/polar_non_exclusive_backup_label with inode 6325, fd 0pfsd_open 539: open /nvme2n1/shared_data/global/pg_control with inode 8373, fd 0851396/851396 kB (100%), 2/2 tablespacespolar_basebackup: write-ahead log end point: 0/16C9300polar_basebackup: waiting for background process to finish streaming ...polar_basebackup: base backup completedpfsd_umount_force 247: pbdname nvme2n1pfs_umount_prepare 269: pfs_umount_prepare. pbdname:nvme2n1chnl_connection_release_shm 1164: client umount return : deleted /var/run/pfsd//nvme2n1/99.pidpfs_umount_post 281: pfs_umount_post. pbdname:nvme2n1pfsd_umount_force 261: umount success for nvme2n1上述命令会在当前机器的本地存储上备份 Primary 节点的本地数据目录,在参数指定的共享存储目录上备份共享数据目录。重新配置 Standby 节点重新编辑 Standby 节点的配置文件 ~/standby/postgresql.conf:-polar_hostid=1+polar_hostid=3-polar_disk_name='nvme1n1'-polar_datadir='/nvme1n1/shared_data/'+polar_disk_name='nvme2n1'+polar_datadir='/nvme2n1/shared_data/'-synchronous_standby_names='replica1'在 Standby 节点的复制配置文件 ~/standby/recovery.conf 中添加:+recovery_target_timeline = 'latest'+primary_slot_name = 'standby1'Standby 节点启动在 Primary 节点上创建用于与 Standby 进行物理复制的复制槽:$ psql \--host= --port=5432 \-d postgres \-c "SELECT * FROM pg_create_physical_replication_slot('standby1');"slot_name | lsn-----------+-----standby1 |(1 row)启动 Standby 节点:pg_ctl -D $HOME/standby startStandby 节点验证在 Primary 节点上创建表并插入数据,在 Standby 节点上可以查询到数据:$ psql -q \-h \-p 5432 \-d postgres \-c "CREATE TABLE t (t1 INT PRIMARY KEY, t2 INT); INSERT INTO t VALUES (1, 1),(2, 3),(3, 3);"$ psql -q \-h \-p 5432 \-d postgres \-c "SELECT * FROM t;"t1 | t2----+----1 | 12 | 33 | 3(3 rows)
页: [1]
查看完整版本: PolarDB for PostgreSQL 备份并恢复一个 Standby 节点