postgres 数据库物理备份与还原

*备份$PGDATA、归档文件、表空间目录,一般适用于小版本的恢复,不能跨平台。

*数据库需要开启归档模式

本文主要介绍通过远程主机备份与还原postgres数据库

配置环境
主机名    IP地址                 角色    
qxy      192.168.40.170     备份数据库  

qxy1     192.168.40.171    备份主机  

1、数据库开启归档模式(qxy主机)

创建归档目录
mkdir -p /spark/pgsql/data_test/arch

[postgres@qxy ~]$ 
[postgres@qxy ~]$ mkdir -p /spark/pgsql/data_test/arch
[postgres@qxy ~]$ 

配置归档命令
vi $PGDATA/postgresql.conf
archive_mode = on
archive_command = 'DATE=`date +%Y%m%d`; DIR="/spark/pgsql/data_test/arch/$DATE"; (test -d $DIR || mkdir -p $DIR) && cp %p $DIR/%f'


[postgres@qxy data_test]$ cat postgresql.conf  | grep archive_
archive_mode = on               # enables archiving; off, on, or always
archive_command = ''DATE=`date +%Y%m%d`; DIR="/spark/pgsql/data_test/arch/$DATE"; (test -d $DIR || mkdir -p $DIR) && cp %p $DIR/%f''            # command to use to archive a logfile segment


%p 表示xlog文件名$PGDATA的相对路径, 如pg_xlog/000000010000000000000002
%f 表示xlog文件名, 000000010000000000000002


配置日志模
vi $PGDATA/postgresql.conf
wal_level = hot_standby或者archive模式

[postgres@qxy data_test]$ cat postgresql.conf  | grep wal_level
wal_level = hot_standby                 # minimal, replica, or logical
[postgres@qxy data_test]$ 

重启数据库

[postgres@qxy data_test]$ pg_ctl restart -D /spark/pgsql/data_test/ -m fast
waiting for server to shut down..... done
server stopped
server starting
[postgres@qxy data_test]$ LOG:  redirecting log output to logging collector process
HINT:  Future log output will appear in directory "pg_clog".

[postgres@qxy data_test]$ 
[postgres@qxy data_test]$ 
[postgres@qxy data_test]$ 

测试归档是否正常
checkpoint;
select pg_switch_xlog();

[postgres@qxy arch]$ pwd
/spark/pgsql/data_test/arch
[postgres@qxy arch]$ ls -ltr
total 4
drwx------. 2 postgres postgres 4096 Jun  9 22:31 20180609
[postgres@qxy arch]$ cd 20180609/
[postgres@qxy 20180609]$ ls -ltr
total 32768
-rw-------. 1 postgres postgres 16777216 Jun  9 22:31 000000010000000000000002
-rw-------. 1 postgres postgres 16777216 Jun  9 22:31 000000010000000000000003
[postgres@qxy 20180609]$ 

2、创建一个测试表空间(qxy主机)

创建表空间目录
mkdir -p /spark/pgsql/tablespace/tbs01
create tablespace tblsp1 location '/spark/pgsql/tablespace/tbs01';
create tablespace tblsp2 location '/spark/pgsql/tablespace/tbs01';


[postgres@qxy ~]$ 
[postgres@qxy ~]$ mkdir -p /spark/pgsql/tablespace/tbs01
[postgres@qxy ~]$ 
[postgres@qxy ~]$ psql -p 5433
psql.bin (9.6.4)
Type "help" for help.

postgres=# create tablespace tblsp1 location '/spark/pgsql/tablespace/tbs01';
CREATE TABLESPACE
postgres=# create tablespace tblsp2 location '/spark/pgsql/tablespace/tbs01';
ERROR:  directory "/spark/pgsql/tablespace/tbs01/PG_9.6_201608131" already in use as a tablespace  <=====提示已经存在一个目录,如果还需要新建一个表空间,需要在创建一个目录
postgres=# 
postgres=# 

查看创建的表空间
postgres=# \db
                  List of tablespaces
    Name    |  Owner   |           Location            
------------+----------+-------------------------------
 pg_default | postgres | 
 pg_global  | postgres | 
 tblsp1     | postgres | /spark/pgsql/tablespace/tbs01
(3 rows)

postgres=# select * from pg_tablespace; 
  spcname   | spcowner | spcacl | spcoptions 
------------+----------+--------+------------
 pg_default |       10 |        | 
 pg_global  |       10 |        | 
 tblsp1     |       10 |        | 
(3 rows)

postgres=# 

表空间使用
postgres=# 
postgres=# create table test (id int) tablespace tblsp1;
CREATE TABLE
postgres=# 
postgres=# 

设置默认表空间
如果我们不建立自己的表空间,建立表的时候,也不指定表空间,那么表默认会放入缺省表空间里,即pg_default
postgres=# \l+
                                                               List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   |  Size   | Tablespace |                Description                 
-----------+----------+----------+---------+-------+-----------------------+---------+------------+--------------------------------------------
 postgres  | postgres | UTF8     | C       | C     |                       | 7071 kB | pg_default | default administrative connection database
 qxy       | postgres | UTF8     | C       | C     |                       | 6993 kB | pg_default | 
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +| 6953 kB | pg_default | unmodifiable empty database
           |          |          |         |       | postgres=CTc/postgres |         |            | 
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +| 6953 kB | pg_default | default template for new databases
           |          |          |         |       | postgres=CTc/postgres |         |            | 
(4 rows)
postgres=# alter database qxy set tablespace tblsp1;
ALTER DATABASE
postgres=# \c qxy scott
You are now connected to database "qxy" as user "scott".
qxy=# create table test (id int) tablespace tblsp1;
CREATE TABLE
qxy=# 
qxy=# 

表空间对应与文件系统
[postgres@qxy pg_tblspc]$ pwd
/spark/pgsql/data_test/pg_tblspc
[postgres@qxy pg_tblspc]$ ls -ltr
total 0
lrwxrwxrwx. 1 postgres postgres 29 Jun  9 22:36 32773 -> /spark/pgsql/tablespace/tbs01
[postgres@qxy pg_tblspc]$ 

初始化cluster时默认的表空间说明
database cluster初始化时创建两个默认表空间
pg_global  –存放系统字典表
pg_default –template0,template1默认表空间,所以create database不明确指定表空间都使用此表空间

3、使用过pg_basebackup, 流复制协议备份(qxy主机)

创建replication权限的角色, 或者超级用户的角色
create role replica  nosuperuser replication login connection limit 32 encrypted password 'replica';


postgres=# create role replica  nosuperuser replication login connection limit 32 encrypted password 'replica';
CREATE ROLE
postgres=# 


配置pg_hba.conf
添加下面一行
host    replication     replica         192.168.40.1/24         md5    <====其中192.168.40.1/24 代表192.168.40字段的IP都可以访问,replication代表具有流复制权限 
[postgres@qxy data_test]$ cat pg_hba.conf | grep -v '#' | grep -ni 'replication'
8:host    replication     replica         192.168.40.1/24         md5 
[postgres@qxy data_test]$ 
[postgres@qxy data_test]$ pg_ctl reload -D /spark/pgsql/data_test/
server signaled
[postgres@qxy data_test]$ 
[postgres@qxy data_test]$ 

配置 postgresql.conf 
把listen_addresses = 'localhost'修改为listen_addresses = '*'  <=====默认只监听本机,修改为* 表示为监听所有IP的发起的连接

[postgres@qxy data_test]$ cat postgresql.conf  | grep listen
listen_addresses = '*'          # what IP address(es) to listen on;
[postgres@qxy data_test]$ 

重启数据库生效
[postgres@qxy data_test]$ pg_ctl  restart -D /spark/pgsql/data_test/
waiting for server to shut down.... done
server stopped
server starting
[postgres@qxy data_test]$ LOG:  redirecting log output to logging collector process
HINT:  Future log output will appear in directory "pg_clog".

[postgres@qxy data_test]$ 



远端机器连接测试(qxy1主机)
[postgres@QXY1 ~]$ hostname
QXY1.localdomain
[postgres@QXY1 ~]$ 
[postgres@QXY1 ~]$ psql -h 192.168.40.170 -p 5433 -d postgres -U replica --password
Password for user replica: 
psql.bin (9.6.4)
Type "help" for help.

postgres=> 
postgres=> \l+
                                                               List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   |  Size   | Tablespace |                Description                 
-----------+----------+----------+---------+-------+-----------------------+---------+------------+--------------------------------------------
 postgres  | postgres | UTF8     | C       | C     |                       | 7071 kB | pg_default | default administrative connection database
 qxy       | postgres | UTF8     | C       | C     |                       | 7103 kB | tblsp1     | 
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +| 6953 kB | pg_default | unmodifiable empty database
           |          |          |         |       | postgres=CTc/postgres |         |            | 
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +| 6953 kB | pg_default | default template for new databases
           |          |          |         |       | postgres=CTc/postgres |         |            | 
(4 rows)

postgres=> 

4、备份存放主机创建备份目录(QXY1主机)

[postgres@QXY1 pgbak]$ 
[postgres@QXY1 pgbak]$ mkdir -p /home/postgres/pgbak
[postgres@QXY1 pgbak]$ cd /home/postgres/pgbak

pg_basebackup -F t -x -D ./`date +%F` -h 192.168.40.170 -p 5433 -U replica

[postgres@QXY1 pgbak]$ ls -ltr
total 0
[postgres@QXY1 pgbak]$ pwd
/home/postgres/pgbak
[postgres@QXY1 pgbak]$ pg_basebackup -F t -x -D ./`date +%F` -h 192.168.40.170 -p 5433 -U replica
Password: 
pg_basebackup: could not connect to server: FATAL:  number of requested standby connections exceeds max_wal_senders (currently 0)   <====源库重新修改max_wal_senders大于0即可
[postgres@QXY1 pgbak]$ pg_basebackup -F t -x -D ./`date +%F` -h 192.168.40.170 -p 5433 -U replica
Password: 
[postgres@QXY1 pgbak]$ ls
2018-06-10
[postgres@QXY1 pgbak]$ du -sh 2018-06-10/
157M    2018-06-10/
[postgres@QXY1 pgbak]$ 
[postgres@QXY1 2018-06-10]$ ls -ltr
total 160328
-rw-r--r--. 1 postgres postgres   7416320 Jun 10 07:16 32773.tar   <=====代表是表空间
-rw-r--r--. 1 postgres postgres 156755456 Jun 10 07:17 base.tar    <=====就是$PGDATA

postgres@QXY1 2018-06-10]$ tar -tf 32773.tar    <====查看32773.tar里面的内容,其实就是源库的表空间内容
PG_9.6_201608131/
PG_9.6_201608131/12404/
PG_9.6_201608131/12404/32775
PG_9.6_201608131/24576/
PG_9.6_201608131/24576/12244_fsm
PG_9.6_201608131/24576/2609_fsm
PG_9.6_201608131/24576/2839
PG_9.6_201608131/24576/3256_vm
PG_9.6_201608131/24576/2684
PG_9.6_201608131/24576/3468
PG_9.6_201608131/24576/12249_vm
PG_9.6_201608131/24576/549
PG_9.6_201608131/24576/12239
PG_9.6_201608131/24576/3602_fsm
PG_9.6_201608131/24576/3455
PG_9.6_201608131/24576/2835
PG_9.6_201608131/24576/2618
PG_9.6_201608131/24576/2666
PG_9.6_201608131/24576/1255
PG_9.6_201608131/24576/2663
PG_9.6_201608131/24576/3576_vm
PG_9.6_201608131/24576/2650
PG_9.6_201608131/24576/2606_vm
PG_9.6_201608131/24576/2618_fsm
PG_9.6_201608131/24576/3574
PG_9.6_201608131/24576/2667
。
。
。  <=====省略
。


[postgres@QXY1 2018-06-10]$  tar -tf base.tar | more
backup_label                     <======多了一个backup_level,记录了备份时间点的相关信息
tablespace_map
pg_dynshmem/
pg_ident.conf
pg_logical/
pg_logical/snapshots/
pg_logical/mappings/
pg_stat_tmp/
pg_clog/
pg_clog/postgresql-2018-06-09_230730.log
pg_clog/postgresql-2018-06-09_223028.log
pg_clog/postgresql-2018-06-09_231641.log
pg_clog/postgresql-2018-06-09_223107.log
pg_clog/postgresql-2018-06-09_133147.log
pg_clog/postgresql-2018-06-09_230203.log
pg_clog/0000
pg_clog/postgresql-2018-06-09_231200.log
pg_multixact/
pg_multixact/members/
pg_multixact/members/0000
pg_multixact/offsets/
pg_multixact/offsets/0000
postgresql.conf
pg_commit_ts/



源库表空间内容信息,可以看到和 32773.tar里面的内容一样
[postgres@qxy PG_9.6_201608131]$ cd
[postgres@qxy ~]$ cd /spark/pgsql/data_test/
[postgres@qxy data_test]$ cd pg_tblspc/
[postgres@qxy pg_tblspc]$ ls -ltr
total 0
lrwxrwxrwx. 1 postgres postgres 29 Jun  9 22:36 32773 -> /spark/pgsql/tablespace/tbs01
[postgres@qxy pg_tblspc]$ cd /spark/pgsql/tablespace/tbs01
[postgres@qxy tbs01]$ ls -ltr
total 4
drwx------. 4 postgres postgres 4096 Jun  9 22:44 PG_9.6_201608131
[postgres@qxy tbs01]$ cd PG_9.6_201608131/
[postgres@qxy PG_9.6_201608131]$ ls -ltr
total 8
drwx------. 2 postgres postgres 4096 Jun  9 22:40 12404
drwx------. 2 postgres postgres 4096 Jun  9 23:16 24576
[postgres@qxy PG_9.6_201608131]$ 

注:
   因为测试时归档目录存放在了/spark/pgsql/data_test/下面,所以备份的时候已经顺带着把归档备份走了,如果备份目录是另外独立的,需要手动copy。

5、源库模拟删除(qxy主机)

[postgres@qxy ~]$ psql -p 5433
psql.bin (9.6.4)
Type "help" for help.

postgres=# \l+
                                                               List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   |  Size   | Tablespace |                Description                 
-----------+----------+----------+---------+-------+-----------------------+---------+------------+--------------------------------------------
 postgres  | postgres | UTF8     | C       | C     |                       | 7071 kB | pg_default | default administrative connection database
 qxy       | postgres | UTF8     | C       | C     |                       | 7103 kB | tblsp1     | 
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +| 6953 kB | pg_default | unmodifiable empty database
           |          |          |         |       | postgres=CTc/postgres |         |            | 
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +| 6953 kB | pg_default | default template for new databases
           |          |          |         |       | postgres=CTc/postgres |         |            | 
(4 rows)

postgres=# \d t
       Table "public.t"
 Column |  Type   | Modifiers 
--------+---------+-----------
 id     | integer | 

postgres=# select * from t;
 id 
----
 10
 10
 10
 10
 10
 10
(6 rows)

postgres=# insert into t values (10);   <====重新插入一条记录,这条记录备份的时候是不存在的,等下要测试通过备份集加上归档是否可以还原改行数据
INSERT 0 1
postgres=#    
postgres=# checkpoint;
CHECKPOINT
postgres=# select pg_switch_xlog();      
 pg_switch_xlog 
----------------
 0/A0005D8
(1 row)

postgres=# \q
[postgres@qxy ~]$ 

--删除数据库
[postgres@qxy ~]$ rm -rf /spark/pgsql/data_test/*
[postgres@qxy ~]$ rm -rf /spark/pgsql/tablespace/tbs01/*
[postgres@qxy ~]$ 
[postgres@qxy ~]$ 
[postgres@qxy ~]$ 

6、通过备份集还原源端数据库

(从备份库(QXY1)把备份结果集scp到源库)
[postgres@QXY1 2018-06-10]$ scp 32773.tar [email protected]:/spark/pgsql/tablespace/tbs01/
[email protected]'s password: 
32773.tar                                                                                                                  100% 7243KB   7.1MB/s   00:00    
[postgres@QXY1 2018-06-10]$ scp base.tar [email protected]:/spark/pgsql/data_test/
[email protected]'s password: 
base.tar                                                                                                                   100%  149MB 149.5MB/s   00:01    
[postgres@QXY1 2018-06-10]$

源库解压(qxy主机执行)
[postgres@qxy data_test]$ tar -xvf base.tar 
[postgres@qxy data_test]$ ls -ltr
total 153208
-rw-------. 1 postgres postgres        88 Jun  7 15:30 postgresql.auto.conf
drwx------. 2 postgres postgres      4096 Jun  7 15:30 pg_twophase
drwx------. 2 postgres postgres      4096 Jun  7 15:30 pg_subtrans
drwx------. 2 postgres postgres      4096 Jun  7 15:30 pg_snapshots
drwx------. 2 postgres postgres      4096 Jun  7 15:30 pg_serial
drwx------. 2 postgres postgres      4096 Jun  7 15:30 pg_replslot
drwx------. 4 postgres postgres      4096 Jun  7 15:30 pg_multixact
drwx------. 4 postgres postgres      4096 Jun  7 15:30 pg_logical
-rw-------. 1 postgres postgres      1636 Jun  7 15:30 pg_ident.conf
drwx------. 2 postgres postgres      4096 Jun  7 15:30 pg_dynshmem
drwx------. 2 postgres postgres      4096 Jun  7 15:30 pg_commit_ts
-rw-------. 1 postgres postgres         4 Jun  7 15:30 PG_VERSION
drwxrwxr-x. 3 postgres postgres      4096 Jun  9 22:31 arch
drwx------. 2 postgres postgres      4096 Jun  9 22:36 pg_tblspc
drwx------. 5 postgres postgres      4096 Jun  9 22:44 base
-rw-------. 1 postgres postgres      4604 Jun  9 23:09 pg_hba.conf
-rw-------. 1 postgres postgres     22462 Jun  9 23:16 postgresql.conf
drwx------. 2 postgres postgres      4096 Jun  9 23:16 pg_stat
drwx------. 2 postgres postgres      4096 Jun  9 23:16 pg_notify
drwx------. 2 postgres postgres      4096 Jun  9 23:16 pg_clog
drwx------. 2 postgres postgres      4096 Jun  9 23:16 global
-rw-------. 1 postgres postgres        36 Jun  9 23:16 tablespace_map
drwx------. 2 postgres postgres      4096 Jun  9 23:16 pg_stat_tmp
-rw-------. 1 postgres postgres       206 Jun  9 23:16 backup_label    <====记录了备份时检查点相关的信息
-rw-r--r--. 1 postgres postgres 156755456 Jun  9 23:35 base.tar
drwx------. 3 postgres postgres      4096 Jun  9 23:36 pg_xlog
[postgres@qxy data_test]$ cat backup_label 
START WAL LOCATION: 0/9000028 (file 000000010000000000000009)
CHECKPOINT LOCATION: 0/9000060
BACKUP METHOD: streamed
BACKUP FROM: master
START TIME: 2018-06-09 23:16:50 GMT
LABEL: pg_basebackup base backup
[postgres@qxy data_test]$
[postgres@qxy tbs01]$ tar -xvf 32773.tar    <=====解压表空间
[postgres@qxy tbs01]$ cd PG_9.6_201608131/
[postgres@qxy PG_9.6_201608131]$ ls
12404  24576
[postgres@qxy PG_9.6_201608131]$ ls -ltr
total 8
drwx------. 2 postgres postgres 4096 Jun  9 22:40 12404
drwx------. 2 postgres postgres 4096 Jun  9 23:16 24576
[postgres@qxy PG_9.6_201608131]$ pwd
/spark/pgsql/tablespace/tbs01/PG_9.6_201608131
[postgres@qxy PG_9.6_201608131]$

7、源端数据库配置recovery.conf(qxy主机执行)

扫描二维码关注公众号,回复: 1552967 查看本文章

第5步的删除postgres数据库之前,插入了一条记录,所以删除之前时7条数据记录,但是备份postgres数据库时,测试表T里面只有6条记录,现在就是需要通过备份集加上归档来恢复最后一条记录。所以这里需要配置recovery.conf

copy $PGHOME/share/postgres/recovery.conf.sample 到 /spark/pgsql/data_test
mv recovery.conf.sample recovery.conf
配置下面这行
restore_command = 'cp /spark/pgsql/data_test/arch/20180610/%f %p'   

8、还原数据库(qxy主机执行)

重新启动数据库
[postgres@qxy ~]$ pg_ctl start -D /spark/pgsql/data_test/
server starting
[postgres@qxy ~]$ LOG:  redirecting log output to logging collector process
HINT:  Future log output will appear in directory "pg_clog".

[postgres@qxy ~]$ 
[postgres@qxy ~]$ ps -ef | grep postgres
root       3413   3269  0 22:24 pts/2    00:00:00 su - postgres
postgres   3414   3413  0 22:24 pts/2    00:00:00 -bash
root       3679   3294  0 22:36 pts/3    00:00:00 su - postgres
postgres   3680   3679  0 22:36 pts/3    00:00:00 -bash
root       6372   3245  0 23:36 pts/1    00:00:00 su - postgres
postgres   6373   6372  0 23:36 pts/1    00:00:00 -bash
postgres   6714      1  0 23:42 pts/1    00:00:00 /spark/pgsql/pgsql/bin/postgres -D /spark/pgsql/data_test
postgres   6715   6714  0 23:42 ?        00:00:00 postgres: logger process                                 
postgres   6717   6714  0 23:42 ?        00:00:00 postgres: checkpointer process                           
postgres   6718   6714  0 23:42 ?        00:00:00 postgres: writer process                                 
postgres   6719   6714  0 23:42 ?        00:00:00 postgres: wal writer process                             
postgres   6720   6714  0 23:42 ?        00:00:00 postgres: autovacuum launcher process                    
postgres   6721   6714  0 23:42 ?        00:00:00 postgres: archiver process                               
postgres   6722   6714  0 23:42 ?        00:00:00 postgres: stats collector process                        
postgres   6724   6373 10 23:42 pts/1    00:00:00 ps -ef
postgres   6725   6373  0 23:42 pts/1    00:00:00 grep postgres
[postgres@qxy ~]$ psql -p 5432
psql.bin: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
[postgres@qxy ~]$ psql -p 5433
psql.bin (9.6.4)
Type "help" for help.


postgres=# select * from t;
 id 
----
 10
 10
 10
 10
 10
 10
 10            <========可以看到,最后插入的一条记录也还原回来
(7 rows)

恢复时部分日志如下:
LOG:  database system was shut down at 2018-06-10 00:28:16 GMT
LOG:  starting archive recovery
LOG:  restored log file "000000030000000000000010" from archive
LOG:  restored log file "00000003.history" from archive
LOG:  restored log file "00000004.history" from archive
LOG:  redo starts at 0/10000028
LOG:  consistent recovery state reached at 0/100000F8
LOG:  restored log file "000000040000000000000011" from archive
LOG:  restored log file "000000050000000000000012" from archive
LOG:  restored log file "000000050000000000000013" from archive
LOG:  invalid record length at 0/14000098: wanted 24, got 0
LOG:  redo done at 0/14000028
LOG:  last completed transaction was at log time 2018-06-10 00:26:31.331415+00
LOG:  restored log file "00000005.history" from archive
LOG:  archive recovery complete

 [postgres@qxy data_test]$ ls -ltr
total 153224
-rw-------. 1 postgres postgres        88 Jun  7 15:30 postgresql.auto.conf
drwx------. 2 postgres postgres      4096 Jun  7 15:30 pg_twophase
drwx------. 2 postgres postgres      4096 Jun  7 15:30 pg_subtrans
drwx------. 2 postgres postgres      4096 Jun  7 15:30 pg_snapshots
drwx------. 2 postgres postgres      4096 Jun  7 15:30 pg_serial
drwx------. 2 postgres postgres      4096 Jun  7 15:30 pg_replslot
drwx------. 4 postgres postgres      4096 Jun  7 15:30 pg_multixact
drwx------. 4 postgres postgres      4096 Jun  7 15:30 pg_logical
-rw-------. 1 postgres postgres      1636 Jun  7 15:30 pg_ident.conf
drwx------. 2 postgres postgres      4096 Jun  7 15:30 pg_dynshmem
drwx------. 2 postgres postgres      4096 Jun  7 15:30 pg_commit_ts
-rw-------. 1 postgres postgres         4 Jun  7 15:30 PG_VERSION
drwxrwxr-x. 3 postgres postgres      4096 Jun  9 22:31 arch
drwx------. 5 postgres postgres      4096 Jun  9 22:44 base
-rw-------. 1 postgres postgres      4604 Jun  9 23:09 pg_hba.conf
-rw-------. 1 postgres postgres     22462 Jun  9 23:16 postgresql.conf
-rw-------. 1 postgres postgres        36 Jun  9 23:16 tablespace_map.old
-rw-------. 1 postgres postgres       206 Jun  9 23:16 backup_label.old    <========== 恢复完成后,该文件变成了old
-rw-r--r--. 1 postgres postgres 156755456 Jun  9 23:35 base.tar
drwx------. 2 postgres postgres      4096 Jun  9 23:42 pg_tblspc
-rw-r--r--. 1 postgres postgres      5720 Jun  9 23:45 recovery.done       <========== recovery.conf变成了done
-rw-------. 1 postgres postgres        71 Jun  9 23:45 postmaster.pid
drwx------. 2 postgres postgres      4096 Jun  9 23:45 pg_notify
-rw-------. 1 postgres postgres        62 Jun  9 23:45 postmaster.opts
drwx------. 2 postgres postgres      4096 Jun  9 23:45 pg_clog
drwx------. 2 postgres postgres      4096 Jun  9 23:45 pg_stat
drwx------. 3 postgres postgres      4096 Jun  9 23:45 pg_xlog
drwx------. 2 postgres postgres      4096 Jun  9 23:45 global
drwx------. 2 postgres postgres      4096 Jun  9 23:50 pg_stat_tmp
[postgres@qxy data_test]$ 

9、其他方式备份

[postgres@qxy pg_clog]$ psql -p 5433
psql.bin (9.6.4)
Type "help" for help.

postgres=# \df *backup*
                                                                 List of functions
   Schema   |         Name         |     Result data type     |                            Argument data types                             |  Type  
------------+----------------------+--------------------------+----------------------------------------------------------------------------+--------
 pg_catalog | pg_backup_start_time | timestamp with time zone |                                                                            | normal
 pg_catalog | pg_is_in_backup      | boolean                  |                                                                            | normal
 pg_catalog | pg_start_backup      | pg_lsn                   | label text, fast boolean DEFAULT false, exclusive boolean DEFAULT true     | normal
 pg_catalog | pg_stop_backup       | pg_lsn                   |                                                                            | normal
 pg_catalog | pg_stop_backup       | SETOF record             | exclusive boolean, OUT lsn pg_lsn, OUT labelfile text, OUT spcmapfile text | normal
(5 rows)

postgres=# 

首先执行pg_start_backup函数,然后就可以手动copy备份,备份的目录包括$PGDATA、归档、表空间。
如:
postgres=# select pg_start_backup(now()::text);
 pg_start_backup 
-----------------
 0/15000060
(1 row)


postgres=# select pg_is_in_backup();
 pg_is_in_backup 
-----------------
 t
(1 row)


postgres=# 

copy结束之后,执行pg_stop_backup


postgres=# select pg_stop_backup();
NOTICE:  pg_stop_backup complete, all required WAL segments have been archived
 pg_stop_backup 
----------------
 0/15000168
(1 row)


postgres=# select pg_is_in_backup();
 pg_is_in_backup 
-----------------
 f
(1 row)


postgres=# 

注意:
    copy过程中的归档也需要备份。

猜你喜欢

转载自blog.csdn.net/m15217321304/article/details/80639385