Precondition:
postgresql:9.5.7datapath:/postgresql/databackpath:/postgresql/backup
data preparation:
test=# select * from test; id | name ----+---------------------- 1 | qwe 2 | asd (2 rows)
Create a base backup:
( Pg_basebackup needs to specify a super user or a user with REPLICATION privileges )
pg_basebackup -h 192.168.56.117 -U repl -W -p 5432 -v -P -Xs -Fp -D /postgresql/backup/ -l backup_test
[postgres@localhost postgresql]$ pg_basebackup -h 192.168.56.117 -U repl -W -p 5432 -v -P -Xs -Fp -D /postgresql/backup/ -l backup_testPassword: pg_basebackup: initiating base backup, waiting for checkpoint to completepg_basebackup: checkpoint completedtransaction log start point: 0/2000028 on timeline 1pg_basebackup: starting background WAL receiver29983/29983 kB (100%), 1/1 tablespace transaction log end point: 0/2000130pg_basebackup: waiting for background process to finish streaming ...pg_basebackup: base backup completed
After the backup is completed, the original format file (-Fp) will be generated under the backup directory /postgresql/backup, which can be configured as compression mode (-Ft), and the log of the backup period (-Xs) will be recorded, and the backup will be generated under the archive log directory Mark the file:
[postgres@localhost postgresql]$ ll /postgresql/archive/total usage 32772-rw-------. 1 postgres postgres 16777216 September 8 10:02 000000010000000000000001-rw-------. 1 postgres postgres 16777216 September 8 10:02 000000010000000000000002-rw-------. 1 postgres postgres 288 September 8 10:02 000000010000000000000002.00000028.backup
View the backup mark file, you can get the backup completion time, the corresponding log file location and other information:
[postgres@localhost postgresql]$ cat /postgresql/archive/000000010000000000000002.00000028.backup START WAL LOCATION: 0/2000028 (file 000000010000000000000002)STOP WAL LOCATION: 0/2000130 (file 000000010000000000000002)CHECKPOINT LOCATION: 0/2000060BACKUP METHOD: streamedBACKUP FROM: masterSTART TIME: 2020-09-08 10:02:56 CSTLABEL: backup_testSTOP TIME: 2020-09-08 10:02:56 CST
Insert incremental test data:
2020-09-08 10:05:20
psql\c testinsert into test values(3,'zxc');create table test2(id int primary key,age int);
PITR recovery:
postgresql PITR is implemented based on pg_basebackup+wal log.
Stop the pg service and empty the data directory:
su - postgrespg_ctl -D /postgresql/data stoprm -rf /postgresql/data/*
Copy the data files in the backup directory to the target data directory:
cp -r /postgresql/backup/* /postgresql/data
Create recover.conf under the data directory and configure:
restore_command = 'cp /postgresql/archive/%f %p'recovery_target_time = '2020-09-08 10:03:10'
Start the database service:
pg_ctl -D /postgresql/data start
data verification:
test=# select * from test;
id | name
----+----------------------
1 | qwe
2 | asd
(2 rows)
test=# \d test2;
Did not find any relation named "test2".
Verified data has been restored to the time point of 2020-09-08 10:03:10