天天看點

PostgreSQL備份工具:pg_rman編譯安裝

概述

pg_rman 是一個專門為pg打造的線上實體備份工具,類似于oracle的rman,今天,我們就來學習一下如何編譯安裝一個pg_rman

準備工作

postgres環境

既然是為了備份pg而存在的工具,我們安裝使用它,首先需要有pg環境,最好是編譯安裝。不管是root還是postgres使用者都要配置好

需要的 packages

zlib-devel

直接 yum install zlib-devel 即可

擷取源碼包

pg_rman是一個開源工具,工程位址為:

https://github.com/ossc-db/pg_rman

可直接使用git使用(需要注意的是:PG的版本不同,可能支援的pg_rman不同,是以需要根據自己資料庫的版本,選擇合适的pg_rman版本)

版本錯誤的報錯

上面說到,如果版本錯誤或不對應。在編譯的時候可能會報錯。如下

[postgres@stephen pg_rman]$ make
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6/include -lm -I. -I./ -I/usr/local/pgsql-9.6/include/server -I/usr/local/pgsql-9.6/include/internal -D_GNU_SOURCE   -c -o backup.o backup.c
backup.c: In function ‘do_backup’:
backup.c:888:2: error: too many arguments to function ‘get_controlfile’
  controlFile = get_controlfile(pgdata, "pg_rman", &crc_ok);
  ^
In file included from backup.c:22:0:
/usr/local/pgsql-9.6/include/server/common/controldata_utils.h:15:25: note: declared here
 extern ControlFileData *get_controlfile(char *DataDir, const char *progname);
                         ^
backup.c: In function ‘init_data_checksum_enabled’:
backup.c:2141:3: error: too many arguments to function ‘get_controlfile’
   controlFile = get_controlfile(pgdata, "pg_rman", &crc_ok);
   ^
In file included from backup.c:22:0:
/usr/local/pgsql-9.6/include/server/common/controldata_utils.h:15:25: note: declared here
 extern ControlFileData *get_controlfile(char *DataDir, const char *progname);
                         ^
make: *** [backup.o] Error 1
           

如果出現以上報錯,需要檢視自己的pg版本與pg_rman 版本是否适配

如我的pg版本為9.6.11 ,pg_rman的版本使用的是REL96

編譯安裝

進入到源碼包中,執行make

[postgres@xl_pg02 pg_rman-REL9_6_STABLE]$ make
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o backup.o backup.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o catalog.o catalog.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o data.o data.c
data.c: In function ‘figure_out_segno’:
data.c:1268:8: warning: variable ‘scanned’ set but not used [-Wunused-but-set-variable]
   int  scanned;
        ^
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o delete.o delete.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o dir.o dir.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o init.o init.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o parray.o parray.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o pg_rman.o pg_rman.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o restore.o restore.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o show.o show.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o util.o util.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o validate.o validate.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o xlog.o xlog.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o pgsql_src/pg_ctl.o pgsql_src/pg_ctl.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o pgut/pgut.o pgut/pgut.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I/usr/local/pgsql-9.6.11/include -lm -I. -I./ -I/usr/local/pgsql-9.6.11/include/server -I/usr/local/pgsql-9.6.11/include/internal -D_GNU_SOURCE   -c -o pgut/pgut-port.o pgut/pgut-port.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 backup.o catalog.o data.o delete.o dir.o init.o parray.o pg_rman.o restore.o show.o util.o validate.o xlog.o pgsql_src/pg_ctl.o pgut/pgut.o pgut/pgut-port.o  -L/usr/local/pgsql-9.6.11/lib   -Wl,--as-needed -Wl,-rpath,'/usr/local/pgsql-9.6.11/lib',--enable-new-dtags -Wl,--build-id  -L/usr/local/pgsql-9.6.11/lib -lpgcommon -lpgport -L/usr/local/pgsql-9.6.11/lib -lpq -lpgcommon -lpgport -lz -lreadline -lrt -lcrypt -ldl -lm -o pg_rman
           

make install

[root@xl_pg02 pg_rman-REL9_6_STABLE]# make install
/usr/bin/mkdir -p '/usr/local/pgsql-9.6.11/bin'
/usr/bin/install -c  pg_rman '/usr/local/pgsql-9.6.11/bin'
           

這樣,pg_rman就編譯安裝完成了

回歸測試

在安裝完成之後呢,如果覺得有問題,可以跑一下回歸測試

make installcheck

[postgres@xl_pg02 pg_rman-REL9_6_STABLE]$ make installcheck
/usr/local/pgsql-9.6.11/lib/pgxs/src/makefiles/../../src/test/regress/pg_regress --inputdir=./ --bindir='/usr/local/pgsql-9.6.11/bin'    --dbname=contrib_regression init option show delete purge backup backup_management restore restore_checksum backup_from_standby arc_srv_log_management
(using postmaster on /tmp, default port)
============== dropping database "contrib_regression" ==============
NOTICE:  database "contrib_regression" does not exist, skipping
DROP DATABASE
============== creating database "contrib_regression" ==============
CREATE DATABASE
ALTER DATABASE
============== running regression test queries        ==============
test init                     ... ok
test option                   ... ok
test show                     ... FAILED
test delete                   ... ok
test purge                    ... ok
test backup                   ... ok
test backup_management        ... ok
test restore                  ... ok
test restore_checksum         ... ok
test backup_from_standby      ... ok
test arc_srv_log_management   ... ok

=======================
 1 of 11 tests failed. 
=======================

The differences that caused some tests to fail can be viewed in the
file "/opt/pg_rman-REL9_6_STABLE/regression.diffs".  A copy of the test summary that you see
above is saved in the file "/opt/pg_rman-REL9_6_STABLE/regression.out".