PG内建的pg_basebackup在ARM平台的表现

作者: bzhaoopenstack

PG内建的热备工具pg_basebackup在ARM上能用吗?看看便知。

PG内建的pg_basebackup在ARM平台的表现

pg_basebackup是postgresql提供的一个方便基础备份的工具(9.1开始提供),这个工具会把整个数据库实例的数据都拷贝出来,而不只是把实例中的部分(如某个数据库或表)单独备份出来,该工具使用replication协议连接到数据库实例上,所以主数据库中的pg_hba.conf必须允许replication连接,类似如下:
host replication replica trust
在9.2之后支持级连复制,所以在之后的版本中,pg_basebackup也可以从另外一个standby库上做基础备份,都需注意如下几方面:
1、备份中没有备份历史文件;
2、不确保所有需要的WAL文件都备份了,如果想确保,需要加命令行参数 ”-x”;
3、如果在备份过程中standby被提升为主库,则备份会失败;
4、要求主库中打开了“full_page_writes”参数,WAL文件不能被类似pg_compresslog的工具去掉full_page_writes信息。

对于这个工具,我们可以在官方文档中查看工具的全部参数说明。

  1. 概念梳理

    对于postgresql常见的备份方式:

    a. 文件系统级别的冷备份。

    ​ 该备份方式需要关闭主数据库,然后拷贝数据文件的完整目录导备机。恢复数据库时,只需将数据目录复制到原来的位置。该方式实际工作中很少使用。

    b.SQL转储

    ​ 对于PG中,常用的工具为pg_dump和pg_dumpall。

    ​ 这种方式可以在数据库正在使用的时候进行完整一致的备份,并不阻塞其它用户对数据库的访问。它会产生一个脚本文件,里面包含备份开始时,已创建的各种数据库对象的SQL语句和每个表中的数据。可以使用数据库提供的工具pg_dumpall和pg_dump来进行备份。pg_dump只备份数据库集群中的某个数据库的数据,它不会导出角色和表空间相关的信息,因为这些信息是整个数据库集群共用的,不属于某个单独的数据库。pg_dumpall,对集簇中的每个数据库调用pg_dump来完成该工作,还会还转储对所有数据库公用的全局对象(pg_dump不保存这些对象)。 目前这包括适数据库用户和组、表空间以及适合所有数据库的访问权限等属性。

    ​ 例如,使用如下命令对名为dbname的数据库进行备份:

    ​ pg_dump –h 127.0.0.1 -p 5432 -U postgres -c -C –f dbname.sql dbname

    ​ 使用如下命令可对全部pg数据库进行备份。

    ​ pg_dumpall –h 127.0.0.1 –p 5432 -U postgres –c -C –f db_bak.sql

    ​ 恢复方式很简单。执行恢复命令即可:

    ​ psql –h 127.0.0.1 -p 5432 -U postgres –f db_bak.sql

    c.连续归档

    这种方式的策略是把一个文件系统级别的全量备份和WAL(预写式日志)级别的增量备份结合起来。当需要恢复时,我们先恢复文件系统级别的备份,然后重放备份的WAL文件,把系统恢复到之前的某个状态。这种备份有显著的优点:

    1. 不需要一个完美的一致的文件系统备份作为开始点。备份中的任何内部不一致性将通过日志重放来修正。
    2. 可以结合一个无穷长的WAL文件序列用于重放,可以通过简单地归档WAL文件来达到连续备份。
    3. 不需要重放WAL项一直到最后。可以在任何点停止重放,并使数据库恢复到当时的一致状态。
    4. 可以连续地将一系列WAL文件输送给另一台已经载入了相同基础备份文件的机器,得到一个实时的热备份系统。

    好了,对于本文中pg_basebackup工具更倾向于第一种备份方式,但是又不完全是,因为pg_basebackup是用作热备份的,不需要停止主库服务,主库仍然能够接收请求。

  2. 测试内容

本文中,我们会在ARM机器上测试pg_basebackup工具的可用性。

环境信息如下:

1
2
3
4
5
pg@pg-test:~/postgres/src/bin/pg_basebackup$ uname -a
Linux pg-test 4.15.0-70-generic #79-Ubuntu SMP Tue Nov 12 10:36:10 UTC 2019 aarch64 aarch64 aarch64 GNU/Linux

OS: Ubuntu bionic
ARCH: aarch64

首先,我们看看如何在ARM上执行pg_basebackup的in-tree tests。可以参照如下脚本来进行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 构建perl环境,包括perlbrew版本管理,cpanm perl module安装工具
echo "yes" | sudo cpan App::perlbrew ;
perlbrew init ;
source ~/perl5/perlbrew/etc/bashrc ;
cat ~/perl5/perlbrew/etc/bashrc >> ~/.bashrc ;
perlbrew install-cpanm ;
# 安装perl-local-lib命名空间
cpanm --local-lib=~/perl5 local::lib && eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib)
# 安装测试依赖的perl IPR::Run模块
cpanm IPC::Run -n
export LD_LIBRARY_PATH=$HOME/perl5/lib/perl5/
# 安装 PG 在with-perl选项下依赖的libperl
sudo apt install libperl-dev -y

# 安装编译
git clone https://github.com/postgres/postgres
cd postgres
# 因为pg_basebackup测试是tap tests,所以需要加上--enable-tap-tests 这个option
./configure --prefix=$HOME/pgsql-install --enable-tap-tests --with-perl
make -j
make install

# 安装测试依赖的test_decoding.so文件
make -C contrib/test_decoding/ install

# 开始跑测试
make -C src/bin/pg_basebackup installcheck

# 当然你也可以单跑其中的某个测试,如
TESTDIR='/home/pg/postgres/src/bin/pg_basebackup' PATH="/home/pg/pgsql-install/bin:$PATH" PGPORT='65432' top_builddir='/home/pg/postgres/src/bin/pg_basebackup/../../..' PG_REGRESS='/home/pg/postgres/src/bin/pg_basebackup/../../../src/test/regress/pg_regress' REGRESS_SHLIB='/home/pg/postgres/src/test/regress/regress.so' /usr/bin/prove -I ../../../src/test/perl/ -I . t/010_pg_basebackup.pl -v

在ARM上测试pg_basebackup目录下的in-tree testcase结果均为PASS。说明在ARM上基本功能是可用的。然而我们仍然需要更深入的测试。

创建测试用的目录、用户、表空间,表和索引等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 创建tablespace 真实路径
mkdir ~/tablespace
mkdir ~/tablespace/testa_ts
mkdir ~/tablespace/testb_ts

# 创建用户
psql -c "create user testa with encrypted password 'pgonarm';"
psql -c "create user testb with encrypted password 'pgonarm';"

# 创建对应用户的tablespace
psql -c "create tablespace testats owner testa location '/home/pg/tablespace/testa_ts';"
psql -c "create tablespace testbts owner testb location '/home/pg/tablespace/testb_ts';"

# 在tablespace中创建db
psql -c "create database testadb with owner testa template template1 encoding 'UTF8' tablespace=testats;"
psql -c "create database testbdb with owner testb template template1 encoding 'UTF8' tablespace=testbts;"

# 创建测试表及插入测试数据
psql -U testa -d testadb -c "create table test1(id numeric); insert into test1 values (1);"
psql -U testb -d testbdb -c "create table test1(id numeric); insert into test1 values (1);"

# 在各自tablespace为双方设置权限
psql -d testbdb -c "grant create on tablespace testa_ts to testb; grant create on tablespace testb_ts to testa;"

# 创建索引
psql -U testa -d testadb -c "create index test1_index on test1(id) tablespace testb_ts;"
psql -U testb -d testbdb -c "create index test1_index on test1(id) tablespace testa_ts;"

OK,数据准备好后,需要在pg_hba.conf中设置replication访问权限,如下:

1
host  replication   postgres    172.17.0.4/32 trust

表示为replication connection, 允许流复制链接。后面的32位IP地址可以跟一个网段,或者任意IP地址(4个0)。对应的允许用户当前为postgres,也可以根据需要进行设置,比如创建一个rep user等等。

好了,现在我们开始一步步测试。

TEST 1 本地备份

1
pg_basebackup -D testbk -Ft -z -P

备份到本地testbk路径,-Ft选用的输出格式为tar. -z并启用gzip压缩,最后-P启用进度显示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
pg@pg-test:~$ pg_basebackup -D testbk -Ft -z -P
46955/46955 kB (100%), 3/3 tablespaces
pg@pg-test:~$ ls testbk/
16389.tar.gz 16390.tar.gz backup_manifest base.tar.gz pg_wal.tar.gz
pg@pg-test:~$
pg@pg-test:~$ psql
psql (14devel)
Type "help" for help.

pg=# select oid, spcname from pg_tablespace;
oid | spcname
-------+------------
1663 | pg_default
1664 | pg_global
16389 | testats
16390 | testbts
(4 rows)

pg=#

可以看到有3个表空间备份了,分别是16389 testats, 16390 testbts 还有 base pg_default三个表空间。

TEST 2 远程备份

使用另一台机器执行

1
pg_basebackup -h HOST_TARGET -D BACKUP_DIR -Ft -z -P

结果本地备份一致。

TEST 3 单tablespace本地数据库备份

执行备份命令

1
2
3
4
pg@pg-test:~$ pg_basebackup -D --Ft -X fetch | bzip2 > bac.tar.bz2
pg_basebackup: error: directory "/home/pg/tablespace/testb_ts" exists but is not empty
pg_basebackup: removing data directory "--Ft"
pg@pg-test:~$

失败了,因为本地数据库中包含有多个tablespace,所以失败,通常pg_basebackup对于tablespace备份时是单对单的,这样才能成功。

TEST 4 tablespace重定向

1
2
3
4
pg@pg-test:~$ pg_basebackup -D bacdata_test -T /home/pg/tablespace/testa_ts=/home/pg/tablespace2/testa_new_ts -T /home/pg/tablespace/testb_ts=/home/pg/tablespace2/testb_new_ts -Ft -P
46955/46955 kB (100%), 3/3 tablespaces
pg@pg-test:~$ ls bacdata_test
16389.tar 16390.tar backup_manifest base.tar pg_wal.tar

这种会将老的tablespace下的testa_ts、testb_ts重定向到tablespace2目录并且重命名。比如在新备机上,对存储路径有需求就可以这么玩。

至此,所有的测试项目均完成,可以看到pg_basebackup工具是能够在ARM平台上正常工作的。所以我们要对这个硬件平台新成员有足够的信心。

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×