pg_filedump 之一 setup

版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/ctypyb2002/article/details/89395832

os: ubuntu 16.04
db: postgresql 9.6.8

Overview:

pg_filedump is a utility to format PostgreSQL heap/index/control files
into a human-readable form. You can format/dump the files several ways,
as listed in the Invocation section, as well as dumping straight binary.

The type of file (heap/index) can usually be determined automatically
by the content of the blocks within the file. However, to format a
pg_control file you must use the -c option.

The default is to format the entire file using the block size listed in
block 0 and display block relative addresses. These defaults can be
modified using run-time options.

Some options may seem strange but they’re there for a reason. For
example, block size. It’s there because if the header of block 0 is
corrupt, you need a method of forcing a block size.

pg_filedump 有点类似 pg_xlogdump ,不需要开启数据库,可以直接从数据文件中将数据dump出来.

版本

# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.5 LTS
Release:	16.04
Codename:	xenial
#
# su - postgres
$ psql -c "select version();"
                                                                   version                                                                    
----------------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 9.6.8 on x86_64-pc-linux-gnu (Ubuntu 9.6.8-1.pgdg16.04+1), compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609, 64-bit
(1 row)

下载安装

这些软件,如非必须,请不要在root用户下安装.
github.com 上搜索 pg_filedump 有很多个项目,并且 star 都很少.只好到 wiki.postgresql.orggit.postgresql.org 去搜索

# su - postgres
$ git clone https://git.postgresql.org/git/pg_filedump.git
$ git branch -a
$ git tag -l
$ git checkout REL9_6_0

注意仔细阅读 README.pg_filedump

$ make -f Makefile.contrib USE_PGXS=1

gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fPIC -pie -fno-omit-frame-pointer -I. -I./ -I/usr/include/postgresql/9.6/server -I/usr/include/postgresql/internal -Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2  -I/usr/include/mit-krb5  -c -o pg_filedump.o pg_filedump.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fPIC -pie -fno-omit-frame-pointer pg_filedump.o  -L/usr/lib/x86_64-linux-gnu -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now  -L/usr/lib/x86_64-linux-gnu/mit-krb5 -Wl,--as-needed   -lpgcommon -lpgport -lselinux -lxslt -lxml2 -lpam -lssl -lcrypto -lgssapi_krb5 -lz -ledit -lrt -lcrypt -ldl -lm  -o pg_filedump
/usr/bin/ld: cannot find -lpgcommon
/usr/bin/ld: cannot find -lpgport
/usr/bin/ld: cannot find -lselinux
/usr/bin/ld: cannot find -ledit
collect2: error: ld returned 1 exit status
/usr/lib/postgresql/9.6/lib/pgxs/src/makefiles/pgxs.mk:304: recipe for target 'pg_filedump' failed
make: *** [pg_filedump] Error 1

补充一下,使用的是 .deb 的安装方式,

# dpkg -l |grep -i post
ii  libdbd-pg-perl                        3.5.3-1build1                              amd64        Perl DBI driver for the PostgreSQL database server
ii  libpq-dev                             10.5-1.pgdg16.04+1                         amd64        header files for libpq5 (PostgreSQL library)
ii  libpq5:amd64                          10.5-1.pgdg16.04+1                         amd64        PostgreSQL C client library
ii  pgdg-keyring                          2017.3                                     all          keyring for apt.postgresql.org
ii  postgresql-9.6                        9.6.8-1.pgdg16.04+1                        amd64        object-relational SQL database, version 9.6 server
ii  postgresql-client-9.6                 9.6.8-1.pgdg16.04+1                        amd64        front-end programs for PostgreSQL 9.6
ii  postgresql-client-common              190.pgdg16.04+1                            all          manager for multiple PostgreSQL client versions
ii  postgresql-common                     190.pgdg16.04+1                            all          PostgreSQL database-cluster manager
ii  postgresql-contrib-9.6                9.6.8-1.pgdg16.04+1                        amd64        additional facilities for PostgreSQL
ii  postgresql-doc-9.6                    9.6.10-1.pgdg16.04+1                       all          documentation for the PostgreSQL database management system
ii  postgresql-server-dev-9.6             9.6.10-1.pgdg16.04+1                       amd64        development files for PostgreSQL 9.6 server-side programming

看报错信息提示是ld 加载不到指定的 LIBRARY
有两种方法解决
1.环境变量

# export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/lib/postgresql/9.6/lib

修改变量 LD_LIBRARY_PATH 不奏效,尝试修改变量 LIBRARY_PATH

# export LIBRARY_PATH=${LIBRARY_PATH}:/usr/lib/postgresql/9.6/lib

2.配置文件

# cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf

# ls -l /etc/ld.so.conf.d
total 12
-rw-rw-r-- 1 root root 38 Nov 24  2014 fakeroot-x86_64-linux-gnu.conf
-rw-r--r-- 1 root root 44 Jan 27  2016 libc.conf
-rw-r--r-- 1 root root 68 Apr 15  2016 x86_64-linux-gnu.conf

# echo '/usr/lib/postgresql/9.6/lib' >> /etc/ld.so.conf
# ldconfig

使用第一种方法修改 LIBRARY_PATH 变量后,不再提示 -lpgcommon -lpgport 这两个错误.

-ledit 报错解决

# apt install libedit2 libedit-dev
# ls -l /usr/lib/x86_64-linux-gnu/ |grep -i libedit
-rw-r--r--  1 root root   342988 Feb  7  2016 libedit.a
lrwxrwxrwx  1 root root       17 Feb  7  2016 libedit.so -> libedit.so.2.0.53
lrwxrwxrwx  1 root root       17 Feb  7  2016 libedit.so.2 -> libedit.so.2.0.53
-rw-r--r--  1 root root   212168 Feb  7  2016 libedit.so.2.0.53

-lselinux 报错解决

# apt install selinux
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 selinux : Conflicts: apparmor but 2.10.95-0ubuntu2.10 is to be installed
E: Unable to correct problems, you have held broken packages.

和 apparmor 冲突了,先卸载 apparmor

# dpkg -l |grep -i apparmor
ii  apparmor                              2.10.95-0ubuntu2.9                         amd64        user-space parser utility for AppArmor
ii  libapparmor-perl                      2.10.95-0ubuntu2.9                         amd64        AppArmor library Perl bindings
ii  libapparmor1:amd64                    2.10.95-0ubuntu2.9                         amd64        changehat AppArmor library

# apt remove --purge apparmor
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  apparmor* liblxc1* lxc-common* lxd* snapd*
0 upgraded, 0 newly installed, 5 to remove and 105 not upgraded.
After this operation, 101 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 82252 files and directories currently installed.)

再次安装 selinux

# apt install selinux
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  checkpolicy libapol4 libauparse0 libqpol1 policycoreutils python-audit python-ipy python-selinux python-semanage
  python-sepolgen python-sepolicy python-setools selinux-policy-dev selinux-policy-ubuntu selinux-utils setools
Suggested packages:
  setools-gui
The following NEW packages will be installed:
  checkpolicy libapol4 libauparse0 libqpol1 policycoreutils python-audit python-ipy python-selinux python-semanage
  python-sepolgen python-sepolicy python-setools selinux selinux-policy-dev selinux-policy-ubuntu selinux-utils
  setools
0 upgraded, 17 newly installed, 0 to remove and 105 not upgraded.
Need to get 6,818 kB of archives.
After this operation, 60.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

中间居然提示要重启,吓一跳

# getenforce
Disabled

# vi /etc/selinux/config
SELINUX=disabled

# find / -name "*selinux*" |more
/lib/x86_64-linux-gnu/libselinux.so.1

# ln -s /lib/x86_64-linux-gnu/libselinux.so.1 /lib/x86_64-linux-gnu/libselinux.so

pg_filedump 源码再次 make

# su - postgres
$ cd pg_filedump
$ make -f Makefile.contrib USE_PGXS=1
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fPIC -pie -fno-omit-frame-pointer -I. -I./ -I/usr/include/postgresql/9.6/server -I/usr/include/postgresql/internal -Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2  -I/usr/include/mit-krb5  -c -o pg_filedump.o pg_filedump.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fPIC -pie -fno-omit-frame-pointer pg_filedump.o  -L/usr/lib/x86_64-linux-gnu -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now  -L/usr/lib/x86_64-linux-gnu/mit-krb5 -Wl,--as-needed   -lpgcommon -lpgport -lselinux -lxslt -lxml2 -lpam -lssl -lcrypto -lgssapi_krb5 -lz -ledit -lrt -lcrypt -ldl -lm  -o pg_filedump

真是折腾.

拷贝 pg_filedump

# which psql
/usr/bin/psql

# cp /var/lib/postgresql/pg_filedump/pg_filedump /usr/bin/
# which pg_filedump
/usr/bin/pg_filedump
# ls -l /usr/bin/pg_filedump
-rwxr-xr-x 1 root root 113760 Apr 19 11:07 /usr/bin/pg_filedump

参考:
https://wiki.postgresql.org/wiki/Pg_filedump
https://git.postgresql.org/gitweb/?p=pg_filedump.git

$ which pg_config
/usr/bin/pg_config

$ pg_config
BINDIR = /usr/lib/postgresql/9.6/bin
DOCDIR = /usr/share/doc/postgresql-doc-9.6
HTMLDIR = /usr/share/doc/postgresql-doc-9.6
INCLUDEDIR = /usr/include/postgresql
PKGINCLUDEDIR = /usr/include/postgresql
INCLUDEDIR-SERVER = /usr/include/postgresql/9.6/server
LIBDIR = /usr/lib/x86_64-linux-gnu
PKGLIBDIR = /usr/lib/postgresql/9.6/lib
LOCALEDIR = /usr/share/locale
MANDIR = /usr/share/postgresql/9.6/man
SHAREDIR = /usr/share/postgresql/9.6
SYSCONFDIR = /etc/postgresql-common
PGXS = /usr/lib/postgresql/9.6/lib/pgxs/src/makefiles/pgxs.mk
CONFIGURE = '--build=x86_64-linux-gnu' '--prefix=/usr' 
'--includedir=/usr/include' '--mandir=/usr/share/man' '--infodir=/usr/share/info' 
'--sysconfdir=/etc' '--localstatedir=/var' '--disable-silent-rules' 
'--libdir=/usr/lib/x86_64-linux-gnu' '--libexecdir=/usr/lib/x86_64-linux-gnu' '--disable-maintainer-mode' 
'--disable-dependency-tracking' '--with-tcl' '--with-perl' '--with-python' '--with-pam' '--with-openssl' 
'--with-libxml' '--with-libxslt' '--with-tclconfig=/usr/lib/x86_64-linux-gnu/tcl8.6' '--with-includes=/usr/include/tcl8.6' 
'PYTHON=/usr/bin/python' '--mandir=/usr/share/postgresql/9.6/man' '--docdir=/usr/share/doc/postgresql-doc-9.6' 
'--sysconfdir=/etc/postgresql-common' '--datarootdir=/usr/share/' '--datadir=/usr/share/postgresql/9.6' 
'--bindir=/usr/lib/postgresql/9.6/bin' '--libdir=/usr/lib/x86_64-linux-gnu/' '--libexecdir=/usr/lib/postgresql/' 
'--includedir=/usr/include/postgresql/' '--enable-nls' '--enable-integer-datetimes' '--enable-thread-safety' 
'--enable-tap-tests' '--enable-debug' '--disable-rpath' '--with-uuid=e2fs' '--with-gnu-ld' '--with-pgport=5432' 
'--with-system-tzdata=/usr/share/zoneinfo' '--with-systemd' '--with-selinux' 
'CFLAGS=-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fPIC -pie -fno-omit-frame-pointer' 
'LDFLAGS=-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now' 'DPKG_VERSION=9.6.10-1.pgdg16.04+1' 'DPKG_VENDOR=Ubuntu' 
'--with-gssapi' '--with-ldap' '--with-includes=/usr/include/mit-krb5' '--with-libs=/usr/lib/mit-krb5' 
'--with-libs=/usr/lib/x86_64-linux-gnu/mit-krb5' 'build_alias=x86_64-linux-gnu' 'CPPFLAGS=-Wdate-time -D_FORTIFY_SOURCE=2'
CC = gcc
CPPFLAGS = -Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2 -I/usr/include/mit-krb5
CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fPIC -pie -fno-omit-frame-pointer
CFLAGS_SL = -fPIC
LDFLAGS = -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -L/usr/lib/x86_64-linux-gnu/mit-krb5 -Wl,--as-needed
LDFLAGS_EX = 
LDFLAGS_SL = 
LIBS = -lpgcommon -lpgport -lselinux -lxslt -lxml2 -lpam -lssl -lcrypto -lgssapi_krb5 -lz -ledit -lrt -lcrypt -ldl -lm 
VERSION = PostgreSQL 9.6.8

猜你喜欢

转载自blog.csdn.net/ctypyb2002/article/details/89395832
今日推荐