pg_basebackup
检查归档是否打开
test=# select pg_switch_xlog();
pg_switch_xlog
----------------
0/2E40BB10
(1 row)
test=# checkpoint;
CHECKPOINT
test=#
-bash-4.1$ ll
total 0
-bash-4.1$ ll
total 16384
-rw-------. 1 postgres postgres 16777216 Jun 9 14:44 00000001000000000000002E
-bash-4.1$ ll
total 16384
-rw-------. 1 postgres postgres 16777216 Jun 9 14:44 00000001000000000000002E
-bash-4.1$ pwd
/opt/postgresql/pg95/backup/arch
该工具使用流复制协议来备份数据库,所以需要创建一个replication来进行备份,当然超级用户也是可以使用的。
postgres=# create role replica nosuperuser replication login connection limit 32 encrypted password 'replica';
CREATE ROLE
postgres=#
-bash-4.1$ vi pg_hba.conf
host replication replica 192.168.10.60/32 md5
"pg_hba.conf" 95L, 4421C written
-bash-4.1$ pg_ctl reload
server signaled
-bash-4.1$ pg_basebackup -D /opt/postgresql/pg95/backup/bak -F t -h 192.168.10.60 -U replica -p 5432
Password:
NOTICE: pg_stop_backup complete, all required WAL segments have been archived
-bash-4.1$ ll
total 85508
-rw-rw-r--. 1 postgres postgres 7909888 Jun 9 14:54 32780.tar
-rw-rw-r--. 1 postgres postgres 79644672 Jun 9 14:54 base.tar
-bash-4.1$
查看tar包备份的文件
-bash-4.1$ tar -tf base.tar |more
backup_label
tablespace_map
pg_commit_ts/
pg_replslot/
pg_xlog/
./pg_xlog/archive_status/
postgresql.conf.hot_standby
pg_logical/
pg_logical/snapshots/
pg_logical/mappings/
1.4恢复
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 | normal
pg_catalog | pg_stop_backup | pg_lsn | | normal
(4 rows)
postgres=# select pg_is_in_backup();
pg_is_in_backup
-----------------
f
(1 row)
恢复过程
-bash-4.1$ cd pg_tblspc/
-bash-4.1$ ll
total 0
lrwxrwxrwx. 1 postgres postgres 24 May 30 06:25 32780 -> /opt/postgresql/pg95/tab
需要把tar包解压到相关的目录下
然后配置recovery.conf文件
-bash-4.1$cp /opt/postgresql/pg95/share/postgresql/recovery.conf.sample ./
-bash-4.1$ mv recovery.conf.sample recovery.conf
-bash-4.1$ vi recovery.conf
restore_command = 'cp /opt/postgresql/pg95/backup/arch/%p %f'
打开数据库
Pg_ctl start