如果并且您应该对您的 PostgreSQL 实例进行基本备份,您可能已经使用过pg_basebackup。pg_basebackup 的另一个用例是在设置流复制时为副本创建起点。到目前为止,pg_basebackup 将始终将结果发送到您启动它的节点/机器。这意味着:如果您在客户端或备份机器上启动 pg_basebackup,那么生成的基本备份将在那里。在最新版本的 PostgreSQL 中没有其他选项可用,这将随着 PostgreSQL 15 而改变。
实现这一点的提交是这样的:
commit 3500ccc39b0dadd1068a03938e4b8ff562587ccc (HEAD -> master, origin/master, origin/HEAD)
Author: Robert Haas
Date: Tue Nov 16 15:20:50 2021 -0500
Support base backup targets.
pg_basebackup now has a --target=TARGET[:DETAIL] option. If specfied,
it is sent to the server as the value of the TARGET option to the
BASE_BACKUP command. If DETAIL is included, it is sent as the value of
the new TARGET_DETAIL option to the BASE_BACKUP command. If the
target is anything other than 'client', pg_basebackup assumes that it
will now be the server's job to write the backup in a location somehow
defined by the target, and that it therefore needs to write nothing
locally. However, the server will still send messages to the client
for progress reporting purposes.
On the server side, we now support two additional types of backup
targets. There is a 'blackhole' target, which just throws away the
backup data without doing anything at all with it. Naturally, this
should only be used for testing and debugging purposes, since you will
not actually have a backup when it finishes running. More usefully,
there is also a 'server' target, so you can now use something like
'pg_basebackup -Xnone -t server:/SOME/PATH' to write a backup to some
location on the server. We can extend this to more types of targets
in the future, and might even want to create an extensibility
mechanism for adding new target types.
Since WAL fetching is handled with separate client-side logic, it's
not part of this mechanism; thus, backups with non-default targets
must use -Xnone or -Xfetch.
Patch by me, with a bug fix by Jeevan Ladhe. The patch set of which
this is a part has also had review and/or testing from Tushar Ahuja,
Suraj Kharage, Dipesh Pandit, and Mark Dilger.
Discussion: http://postgr.es/m/CA+TgmoaYZbz0=Yk797aOJwkGJC-LK3iXn+wzzMx7KdwNpZhS5g@mail.gmail.com
为了演示该功能,我设置了两个节点。这是我的客户:
postgres@debian11latest:/u01/app/postgres/product/DEV/db_0/bin$ ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp1s0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:ca:ce:73 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.240/24 brd 192.168.100.255 scope global dynamic enp1s0
valid_lft 3155sec preferred_lft 3155sec
inet6 fe80::5054:ff:feca:ce73/64 scope link
valid_lft forever preferred_lft forever
这是我的服务器:
postgres@debian11pg:/u01/app/postgres/product/DEV/db_0/ [pgdev] ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp1s0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:c7:93:6c brd ff:ff:ff:ff:ff:ff
inet 192.168.100.241/24 brd 192.168.100.255 scope global dynamic enp1s0
valid_lft 2278sec preferred_lft 2278sec
inet6 fe80::5054:ff:fec7:936c/64 scope link
valid_lft forever preferred_lft forever
如果我在客户端上启动基本备份会发生什么,数据从服务器发送到我的客户端,我将在客户端本地存储基本备份:
postgres@debian11latest:/u01/app/postgres/product/DEV/db_0/bin$ mkdir /var/tmp/backup
postgres@debian11latest:/u01/app/postgres/product/DEV/db_0/bin$ ./pg_basebackup -h 192.168.100.241 -F t -D /var/tmp/backup/
postgres@debian11latest:/u01/app/postgres/product/DEV/db_0/bin$ ls -l /var/tmp/backup/
total 39624
-rw------- 1 postgres postgres 138469 Jan 21 07:41 backup_manifest
-rw------- 1 postgres postgres 23652864 Jan 21 07:41 base.tar
-rw------- 1 postgres postgres 16778752 Jan 21 07:41 pg_wal.tar
上面介绍的是告诉服务器存储备份。这个选项是这个:
postgres@debian11latest:/u01/app/postgres/product/DEV/db_0/bin$ ./pg_basebackup --help | grep -A 1 TARGET
-t, --target=TARGET[:DETAIL]
backup target (if other than client)
所以现在,再次从客户端执行 pg_basebackup,我们可以这样做:
postgres@debian11latest:/u01/app/postgres/product/DEV/db_0/bin$ ssh 192.168.100.241 'mkdir /var/tmp/bb'
postgres@debian11latest:/u01/app/postgres/product/DEV/db_0/bin$ ./pg_basebackup -h 192.168.100.241 -X none -t server:/var/tmp/bb/
postgres@debian11latest:/u01/app/postgres/product/DEV/db_0/bin$ ssh 192.168.100.241 'ls -l /var/tmp/bb/'
total 23236
-rw------- 1 postgres postgres 138469 Jan 21 07:57 backup_manifest
-rw------- 1 postgres postgres 23654400 Jan 21 07:57 base.tar
现在备份在服务器上生成而不发送到客户端。出于测试目的,还有一个“黑洞”目标,它只是丢弃了备份:
postgres@debian11latest:/u01/app/postgres/product/DEV/db_0/bin$ ./pg_basebackup -h 192.168.100.241 -X none -t blackhole
postgres@debian11latest:/u01/app/postgres/product/DEV/db_0/bin$
为什么这个功能很酷?因为现在有实现其他目标的基础设施,可能是S3或其他。
刚刚提交的另一个特性是:扩展 pg_basebackup 的选项以控制压缩。
这为您提供了更多压缩选项:
postgres@debian11pg:/u01/app/postgres/product/DEV/db_0/bin/ [pgdev] ./pg_basebackup --help | grep -A 1 LEVEL
-Z, --compress={gzip,none}[:LEVEL] or [LEVEL]
compress tar output with given compression method or level
目前只有“none”和“gzip”:
postgres@debian11pg:/u01/app/postgres/product/DEV/db_0/bin/ [pgdev] mkdir /var/tmp/aa
postgres@debian11pg:/u01/app/postgres/product/DEV/db_0/bin/ [pgdev] ./pg_basebackup -D /var/tmp/aa/ -Z none
postgres@debian11pg:/u01/app/postgres/product/DEV/db_0/bin/ [pgdev] ls /var/tmp/aa/
backup_label base global pg_dynshmem pg_ident.conf pg_logical pg_notify pg_serial pg_stat pg_subtrans pg_twophase pg_wal postgresql.auto.conf
backup_manifest current_logfiles pg_commit_ts pg_hba.conf pg_log pg_multixact pg_replslot pg_snapshots pg_stat_tmp pg_tblspc PG_VERSION pg_xact postgresql.conf
postgres@debian11pg:/u01/app/postgres/product/DEV/db_0/bin/ [pgdev] rm -rf /var/tmp/aa/*
postgres@debian11pg:/u01/app/postgres/product/DEV/db_0/bin/ [pgdev] ./pg_basebackup -D /var/tmp/aa/ -F t -Z gzip:4
postgres@debian11pg:/u01/app/postgres/product/DEV/db_0/bin/ [pgdev] ls -l /var/tmp/aa/
total 3316
-rw------- 1 postgres postgres 138314 Jan 21 08:39 backup_manifest
-rw------- 1 postgres postgres 3235329 Jan 21 08:39 base.tar.gz
-rw------- 1 postgres postgres 17075 Jan 21 08:39 pg_wal.tar.gz
这同样适用于这里:基础设施现已到位,并且可以添加其他选项。不错的功能,感谢所有相关人员。
作者:Daniel Westermann
文章来源:
https://blog.dbi-services.com/new-options-for-pg_basebackup-in-postgresql-15/
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




