PostgreSQL 10 基于日志的备份与还原

PostgreSQL 10 基于日志的备份与还原

wal,即预写式日志,是日志的标准实现方式,简单而言就是将对数据库的变动先记录到日志中,而后在将具体的新数据刷新到磁盘。PostgreSQL将该日志维护在数据文件夹下的子文件夹pg_wal中。当数据库崩溃后,可以通过“重放(replay)”日志中的“动作”,将数据库恢复。也就是说,只要拥有一个基础备份和完整的日志文件,理论上可以将数据库库恢复到任意基础备份以来的任意时刻点。不仅如此,如果在另一个实例上将这些日志不停的“重放”,那么就拥有了一个完整的在线备份,也就是“复制”。

pg_wal下日志文件不会无限制增多,也就是说并不用担心日志的增多使得磁盘空间捉襟见肘。默认每个日志文件为16M大小,即当增长到16M时,就会切换到别的文件,并复用之前的文件 。因此,为了保证有个完整的日志链,必须将写满的文件复制保存到一个特定的文件 夹。对于最后一个还未满16M的日志文件,可以手动触发一次切换。

备份 (操作均使用postgres用户完成)

1. 登录数据库创建测试数据库

[postgres@localhost ~]$ psql 
psql (10.10)
Type "help" for help.

postgres=# create database test;
CREATE DATABASE
postgres=# 

2. 修改配置文件, 开启日志备份, 将写满的文件复制到archivedir目录下

注: PG9.x 中 wal_level 归档的的取值为archive, PG10.x 中修改为了 replica . replica代表了"流复制"或"归档".

wal_level = replica            # minimal, replica, or logical

archive_mode = on

archive_command = 'test ! -f /home/postgres/archivedir/%f && cp %p /home/postgres/archivedir/%f'       # command to use to archive a logfile segment
220                 # placeholders: %p = path of file to archive
221                 #               %f = file name only
222                 # e.g. 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f'

3. 创建archivedir目录, 并重启数据库服务

[postgres@localhost ~]$ mkdir archivedir
[postgres@localhost ~]$ pg_ctl -D db1 restart
waiting for server to shut down....2020-02-17 20:18:53.755 CST [50766] LOG:  received fast shutdown request
2020-02-17 20:18:53.756 CST [50766] LOG:  aborting any active transactions
2020-02-17 20:18:53.757 CST [50766] LOG:  worker process: logical replication launcher (PID 50773) exited with exit code 1
2020-02-17 20:18:53.757 CST [50768] LOG:  shutting down
2020-02-17 20:18:53.769 CST [50766] LOG:  database system is shut down
 done
server stopped
waiting for server to start....2020-02-17 20:18:53.867 CST [50861] LOG:  listening on IPv6 address "::1", port 5432
2020-02-17 20:18:53.867 CST [50861] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-02-17 20:18:53.871 CST [50861] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-02-17 20:18:53.888 CST [50862] LOG:  database system was shut down at 2020-02-17 20:18:53 CST
2020-02-17 20:18:53.891 CST [50861] LOG:  database system is ready to accept connections
 done
server started
[postgres@localhost ~]$ 

4. 创建测试表

[postgres@localhost ~]$ psql
psql (10.10)
Type "help" for help.

postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# CREATE TABLE testPITR1 AS SELECT * FROM pg_class, pg_description;
SELECT 1338084
test=# 
[postgres@localhost ~]$ ll -h archivedir/
total 336M
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000001
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000002
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000003
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000004
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000005
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000006
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000007
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000008
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000009
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000A
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000B
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000C
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000D
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000E
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000F
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000010
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000011
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000012
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000013
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000014
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000015
[postgres@localhost ~]$ 

5. 创建基础备份

[postgres@localhost ~]$ psql -c "SELECT pg_start_backup('base', true)"
 pg_start_backup 
-----------------
 0/17000028
(1 row)


[postgres@localhost ~]$ tar -cvf db1.tar db1
......
[postgres@localhost ~]$ psql -c "SELECT pg_stop_backup()"
NOTICE:  pg_stop_backup complete, all required WAL segments have been archived
 pg_stop_backup 
----------------
 0/17000168
(1 row)
[postgres@localhost ~]$ 
[postgres@localhost ~]$ ll -h archivedir/
total 369M
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000001
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000002
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000003
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000004
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000005
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000006
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000007
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000008
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000009
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000A
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000B
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000C
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000D
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000E
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000F
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000010
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000011
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000012
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000013
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000014
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000015
-rw------- 1 postgres postgres 16M Feb 17 20:20 000000010000000000000016
-rw------- 1 postgres postgres 16M Feb 17 20:21 000000010000000000000017
-rw------- 1 postgres postgres 291 Feb 17 20:21 000000010000000000000017.00000028.backup
[postgres@localhost ~]$

6. 继续创建测试表, 切换日志

注: pg9.x 中 为 select pg_switch_xlog(); pg10.x中为 select pg_switch_wal();

[postgres@localhost ~]$ psql
psql (10.10)
Type "help" for help.

postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# CREATE TABLE testPITR2 AS SELECT * FROM pg_class, pg_description;
SELECT 1349856
test=# select * from current_timestamp;
       current_timestamp       
-------------------------------
 2020-02-17 20:22:46.40963+08
(1 row)

test=# CREATE TABLE testPITR3 AS SELECT * FROM pg_class, pg_description;
SELECT 1361628
test=# select * from current_timestamp;
       current_timestamp       
-------------------------------
 2020-02-17 20:23:08.669018+08
(1 row)

test=# select pg_switch_wal();
 pg_switch_wal 
---------------
 0/431112D0
(1 row)

test=# 

7. 恢复

关闭数据库,模拟数据库宕机,此时,数据库test中应该有3张表,其中1张表在基础备份前,也就是恢复完数据文件即可找回,而另2张表则需恢复相应的日志文件。模拟恢复到testPITR2创建时刻点。

test=# \d
           List of relations
 Schema |   Name    | Type  |  Owner   
--------+-----------+-------+----------
 public | testpitr1 | table | postgres
 public | testpitr2 | table | postgres
 public | testpitr3 | table | postgres
(3 rows)

test=# 

看看archivedir的情况:

[postgres@localhost ~]$ ll -h archivedir/
total 1.1G
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000001
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000002
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000003
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000004
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000005
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000006
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000007
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000008
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000009
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000A
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000B
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000C
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000D
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000E
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000F
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000010
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000011
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000012
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000013
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000014
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000015
-rw------- 1 postgres postgres 16M Feb 17 20:20 000000010000000000000016
-rw------- 1 postgres postgres 16M Feb 17 20:21 000000010000000000000017
-rw------- 1 postgres postgres 291 Feb 17 20:21 000000010000000000000017.00000028.backup
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000018
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000019
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000001A
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000001B
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000001C
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000001D
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000001E
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000001F
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000020
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000021
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000022
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000023
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000024
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000025
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000026
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000027
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000028
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000029
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000002A
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000002B
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000002C
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000002D
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000002E
-rw------- 1 postgres postgres 16M Feb 17 20:23 00000001000000000000002F
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000030
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000031
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000032
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000033
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000034
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000035
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000036
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000037
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000038
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000039
-rw------- 1 postgres postgres 16M Feb 17 20:23 00000001000000000000003A
-rw------- 1 postgres postgres 16M Feb 17 20:23 00000001000000000000003B
-rw------- 1 postgres postgres 16M Feb 17 20:23 00000001000000000000003C
-rw------- 1 postgres postgres 16M Feb 17 20:23 00000001000000000000003D
-rw------- 1 postgres postgres 16M Feb 17 20:23 00000001000000000000003E
-rw------- 1 postgres postgres 16M Feb 17 20:23 00000001000000000000003F
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000040
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000041
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000042
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000043
[postgres@localhost ~]$ 

7.1 关闭数据库服务, 重命名数据目录

[postgres@localhost ~]$ pg_ctl -D db1 stop
waiting for server to shut down....2020-02-17 20:25:17.173 CST [50861] LOG:  received fast shutdown request
2020-02-17 20:25:17.177 CST [50861] LOG:  aborting any active transactions
2020-02-17 20:25:17.178 CST [51099] FATAL:  terminating autovacuum process due to administrator command
2020-02-17 20:25:17.179 CST [50861] LOG:  worker process: logical replication launcher (PID 50869) exited with exit code 1
2020-02-17 20:25:17.180 CST [50863] LOG:  shutting down
2020-02-17 20:25:17.494 CST [50861] LOG:  database system is shut down
 done
server stopped
[postgres@localhost ~]$ 
[postgres@localhost ~]$ mv db1 db1.old

7.2 解压备份数据文件, 启动服务, 验证此时只有基础备份前的testpitr1

[postgres@localhost ~]$ tar -xvf db1.tar
......
[postgres@localhost ~]$ pg_ctl -D db1 start
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start....2020-02-17 20:26:28.315 CST [51138] LOG:  listening on IPv6 address "::1", port 5432
2020-02-17 20:26:28.315 CST [51138] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-02-17 20:26:28.324 CST [51138] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-02-17 20:26:28.343 CST [51139] LOG:  database system was interrupted; last known up at 2020-02-17 20:20:36 CST
2020-02-17 20:26:28.370 CST [51139] LOG:  redo starts at 0/17000028
2020-02-17 20:26:28.370 CST [51139] LOG:  invalid record length at 0/17000140: wanted 24, got 0
2020-02-17 20:26:28.370 CST [51139] LOG:  redo done at 0/17000108
2020-02-17 20:26:28.439 CST [51138] LOG:  database system is ready to accept connections
 done
server started
[postgres@localhost ~]$ 
[postgres@localhost ~]$ psql
psql (10.10)
Type "help" for help.

postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# \d
           List of relations
 Schema |   Name    | Type  |  Owner   
--------+-----------+-------+----------
 public | testpitr1 | table | postgres
(1 row)

test=# 

7.3 停掉数据库, 删除目录, 重新解压基础备份, 创建recovery.conf, 将数据库恢复到testpitr2时刻

[postgres@localhost ~]$ pg_ctl -D db1 stop
waiting for server to shut down....2020-02-17 20:27:36.615 CST [51138] LOG:  received fast shutdown request
2020-02-17 20:27:36.616 CST [51138] LOG:  aborting any active transactions
2020-02-17 20:27:36.618 CST [51138] LOG:  worker process: logical replication launcher (PID 51146) exited with exit code 1
2020-02-17 20:27:36.618 CST [51140] LOG:  shutting down
2020-02-17 20:27:36.655 CST [51144] LOG:  archive command failed with exit code 1
2020-02-17 20:27:36.655 CST [51144] DETAIL:  The failed archive command was: test ! -f /home/postgres/archivedir/000000010000000000000017 && cp pg_wal/000000010000000000000017 /home/postgres/archivedir/000000010000000000000017
2020-02-17 20:27:36.663 CST [51144] LOG:  archive command failed with exit code 1
2020-02-17 20:27:36.663 CST [51144] DETAIL:  The failed archive command was: test ! -f /home/postgres/archivedir/000000010000000000000017 && cp pg_wal/000000010000000000000017 /home/postgres/archivedir/000000010000000000000017
.2020-02-17 20:27:37.670 CST [51144] LOG:  archive command failed with exit code 1
2020-02-17 20:27:37.670 CST [51144] DETAIL:  The failed archive command was: test ! -f /home/postgres/archivedir/000000010000000000000017 && cp pg_wal/000000010000000000000017 /home/postgres/archivedir/000000010000000000000017
2020-02-17 20:27:37.670 CST [51144] WARNING:  archiving write-ahead log file "000000010000000000000017" failed too many times, will try again later
2020-02-17 20:27:37.673 CST [51144] LOG:  archive command failed with exit code 1
2020-02-17 20:27:37.673 CST [51144] DETAIL:  The failed archive command was: test ! -f /home/postgres/archivedir/000000010000000000000017 && cp pg_wal/000000010000000000000017 /home/postgres/archivedir/000000010000000000000017
.2020-02-17 20:27:38.678 CST [51144] LOG:  archive command failed with exit code 1
2020-02-17 20:27:38.678 CST [51144] DETAIL:  The failed archive command was: test ! -f /home/postgres/archivedir/000000010000000000000017 && cp pg_wal/000000010000000000000017 /home/postgres/archivedir/000000010000000000000017
.2020-02-17 20:27:39.684 CST [51144] LOG:  archive command failed with exit code 1
2020-02-17 20:27:39.684 CST [51144] DETAIL:  The failed archive command was: test ! -f /home/postgres/archivedir/000000010000000000000017 && cp pg_wal/000000010000000000000017 /home/postgres/archivedir/000000010000000000000017
2020-02-17 20:27:39.684 CST [51144] WARNING:  archiving write-ahead log file "000000010000000000000017" failed too many times, will try again later
2020-02-17 20:27:39.687 CST [51138] LOG:  database system is shut down
 done
server stopped
[postgres@localhost ~]$
[postgres@localhost ~]$ rm -rf db1
[postgres@localhost ~]$ tar -xvf db1.tar 
......
[postgres@localhost ~/db1]$ vi recovery.conf
[postgres@localhost ~/db1]$ cat recovery.conf 
restore_command = 'cp /home/postgres/archivedir/%f %p'
recovery_target_time = '2020-02-17 20:22:46'

[postgres@localhost ~/db1]$ 

启动数据库

[postgres@localhost ~]$ pg_ctl -D db1 start
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start....2020-02-17 20:31:31.804 CST [51224] LOG:  listening on IPv6 address "::1", port 5432
2020-02-17 20:31:31.804 CST [51224] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-02-17 20:31:31.806 CST [51224] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-02-17 20:31:31.821 CST [51225] LOG:  database system was interrupted; last known up at 2020-02-17 20:20:36 CST
2020-02-17 20:31:31.838 CST [51225] LOG:  starting point-in-time recovery to 2020-02-17 20:22:46+08
2020-02-17 20:31:31.880 CST [51225] LOG:  restored log file "000000010000000000000017" from archive
2020-02-17 20:31:31.910 CST [51225] LOG:  redo starts at 0/17000028
2020-02-17 20:31:31.911 CST [51225] LOG:  consistent recovery state reached at 0/17000168
2020-02-17 20:31:31.912 CST [51224] LOG:  database system is ready to accept read only connections
2020-02-17 20:31:31.949 CST [51225] LOG:  restored log file "000000010000000000000018" from archive
 done
server started
[postgres@localhost ~]$ 2020-02-17 20:31:32.244 CST [51225] LOG:  restored log file "000000010000000000000019" from archive
2020-02-17 20:31:32.540 CST [51225] LOG:  restored log file "00000001000000000000001A" from archive
2020-02-17 20:31:32.816 CST [51225] LOG:  restored log file "00000001000000000000001B" from archive
2020-02-17 20:31:33.100 CST [51225] LOG:  restored log file "00000001000000000000001C" from archive
2020-02-17 20:31:33.400 CST [51225] LOG:  restored log file "00000001000000000000001D" from archive
2020-02-17 20:31:33.664 CST [51225] LOG:  restored log file "00000001000000000000001E" from archive
2020-02-17 20:31:33.944 CST [51225] LOG:  restored log file "00000001000000000000001F" from archive
2020-02-17 20:31:34.236 CST [51225] LOG:  restored log file "000000010000000000000020" from archive
2020-02-17 20:31:34.528 CST [51225] LOG:  restored log file "000000010000000000000021" from archive
2020-02-17 20:31:34.827 CST [51225] LOG:  restored log file "000000010000000000000022" from archive
2020-02-17 20:31:35.125 CST [51225] LOG:  restored log file "000000010000000000000023" from archive
2020-02-17 20:31:35.418 CST [51225] LOG:  restored log file "000000010000000000000024" from archive
2020-02-17 20:31:35.718 CST [51225] LOG:  restored log file "000000010000000000000025" from archive
2020-02-17 20:31:36.021 CST [51225] LOG:  restored log file "000000010000000000000026" from archive
2020-02-17 20:31:36.321 CST [51225] LOG:  restored log file "000000010000000000000027" from archive
2020-02-17 20:31:36.644 CST [51225] LOG:  restored log file "000000010000000000000028" from archive
2020-02-17 20:31:36.950 CST [51225] LOG:  restored log file "000000010000000000000029" from archive
2020-02-17 20:31:37.249 CST [51225] LOG:  restored log file "00000001000000000000002A" from archive
2020-02-17 20:31:37.594 CST [51225] LOG:  restored log file "00000001000000000000002B" from archive
2020-02-17 20:31:37.891 CST [51225] LOG:  restored log file "00000001000000000000002C" from archive
2020-02-17 20:31:38.673 CST [51225] LOG:  restored log file "00000001000000000000002D" from archive
2020-02-17 20:31:38.967 CST [51225] LOG:  restored log file "00000001000000000000002E" from archive
2020-02-17 20:31:39.279 CST [51225] LOG:  restored log file "00000001000000000000002F" from archive
2020-02-17 20:31:39.558 CST [51225] LOG:  restored log file "000000010000000000000030" from archive
2020-02-17 20:31:39.878 CST [51225] LOG:  restored log file "000000010000000000000031" from archive
2020-02-17 20:31:40.187 CST [51225] LOG:  restored log file "000000010000000000000032" from archive
2020-02-17 20:31:40.488 CST [51225] LOG:  restored log file "000000010000000000000033" from archive
2020-02-17 20:31:40.835 CST [51225] LOG:  restored log file "000000010000000000000034" from archive
2020-02-17 20:31:41.160 CST [51225] LOG:  restored log file "000000010000000000000035" from archive
2020-02-17 20:31:41.482 CST [51225] LOG:  restored log file "000000010000000000000036" from archive
2020-02-17 20:31:41.797 CST [51225] LOG:  restored log file "000000010000000000000037" from archive
2020-02-17 20:31:42.883 CST [51225] LOG:  restored log file "000000010000000000000038" from archive
2020-02-17 20:31:43.168 CST [51225] LOG:  restored log file "000000010000000000000039" from archive
2020-02-17 20:31:43.470 CST [51225] LOG:  restored log file "00000001000000000000003A" from archive
2020-02-17 20:31:43.804 CST [51225] LOG:  restored log file "00000001000000000000003B" from archive
2020-02-17 20:31:44.099 CST [51225] LOG:  restored log file "00000001000000000000003C" from archive
2020-02-17 20:31:44.387 CST [51225] LOG:  restored log file "00000001000000000000003D" from archive
2020-02-17 20:31:44.850 CST [51225] LOG:  restored log file "00000001000000000000003E" from archive
2020-02-17 20:31:45.129 CST [51225] LOG:  restored log file "00000001000000000000003F" from archive
2020-02-17 20:31:45.416 CST [51225] LOG:  restored log file "000000010000000000000040" from archive
2020-02-17 20:31:45.703 CST [51225] LOG:  restored log file "000000010000000000000041" from archive
2020-02-17 20:31:46.049 CST [51225] LOG:  restored log file "000000010000000000000042" from archive
2020-02-17 20:31:46.922 CST [51225] LOG:  restored log file "000000010000000000000043" from archive
2020-02-17 20:31:46.962 CST [51225] LOG:  recovery stopping before commit of transaction 560, time 2020-02-17 20:23:05.430435+08
2020-02-17 20:31:46.962 CST [51225] LOG:  recovery has paused
2020-02-17 20:31:46.962 CST [51225] HINT:  Execute pg_wal_replay_resume() to continue.

[postgres@localhost ~]$  

可以在恢复日志中看到这么一句话:

2020-02-17 20:31:46.922 CST [51225] LOG:  restored log file "000000010000000000000043" from archive
2020-02-17 20:31:46.962 CST [51225] LOG:  recovery stopping before commit of transaction 560, time 2020-02-17 20:23:05.430435+08
2020-02-17 20:31:46.962 CST [51225] LOG:  recovery has paused
2020-02-17 20:31:46.962 CST [51225] HINT:  Execute pg_wal_replay_resume() to continue.

7.4 验证,进入数据库,发现testpitr2已经恢复

[postgres@localhost ~]$ psql
psql (10.10)
Type "help" for help.

postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# \d
           List of relations
 Schema |   Name    | Type  |  Owner   
--------+-----------+-------+----------
 public | testpitr1 | table | postgres
 public | testpitr2 | table | postgres
(2 rows)
test=# select count(*) from testpitr1;
  count  
---------
 1338084
(1 row)

test=# select count(*) from testpitr2;
  count  
---------
 1349856
(1 row)

test=# 

如果需要恢复table3,则必须再次删除数据文件夹,建立recovery.conf。

8. 恢复table3

8.1 停掉数据库,删除数据文件夹,重新解压基础备份,创建recovery.conf,将数据库恢复到testpitr3时刻

[postgres@localhost ~]$ pg_ctl -D db1 stop
waiting for server to shut down....2020-02-17 20:34:07.018 CST [51224] LOG:  received fast shutdown request
2020-02-17 20:34:07.019 CST [51224] LOG:  aborting any active transactions
2020-02-17 20:34:07.062 CST [51227] LOG:  shutting down
2020-02-17 20:34:07.083 CST [51224] LOG:  database system is shut down
 done
server stopped
[postgres@localhost ~]$ 
[postgres@localhost ~]$ rm -rf db1
[postgres@localhost ~]$ tar xvf db1.tar
[postgres@localhost ~]$ cd db1
[postgres@localhost ~/db1]$ vi recovery.conf
[postgres@localhost ~/db1]$ cat recovery.conf 
restore_command = 'cp /home/postgres/archivedir/%f %p'
recovery_target_time = '2020-02-17 20:23:08'

8.2 启动数据库

[postgres@localhost ~]$ pg_ctl -D db1 start
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start....2020-02-17 20:38:22.153 CST [51380] LOG:  listening on IPv6 address "::1", port 5432
2020-02-17 20:38:22.153 CST [51380] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-02-17 20:38:22.155 CST [51380] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-02-17 20:38:22.174 CST [51381] LOG:  database system was interrupted; last known up at 2020-02-17 20:20:36 CST
2020-02-17 20:38:22.193 CST [51381] LOG:  starting point-in-time recovery to 2020-02-17 20:23:08+08
2020-02-17 20:38:22.227 CST [51381] LOG:  restored log file "000000010000000000000017" from archive
2020-02-17 20:38:22.253 CST [51381] LOG:  redo starts at 0/17000028
2020-02-17 20:38:22.254 CST [51381] LOG:  consistent recovery state reached at 0/17000168
2020-02-17 20:38:22.255 CST [51380] LOG:  database system is ready to accept read only connections
2020-02-17 20:38:22.293 CST [51381] LOG:  restored log file "000000010000000000000018" from archive
 done
server started
[postgres@localhost ~]$ 2020-02-17 20:38:22.577 CST [51381] LOG:  restored log file "000000010000000000000019" from archive
2020-02-17 20:38:22.857 CST [51381] LOG:  restored log file "00000001000000000000001A" from archive
2020-02-17 20:38:23.135 CST [51381] LOG:  restored log file "00000001000000000000001B" from archive
2020-02-17 20:38:23.412 CST [51381] LOG:  restored log file "00000001000000000000001C" from archive
2020-02-17 20:38:23.684 CST [51381] LOG:  restored log file "00000001000000000000001D" from archive
2020-02-17 20:38:23.946 CST [51381] LOG:  restored log file "00000001000000000000001E" from archive
2020-02-17 20:38:24.206 CST [51381] LOG:  restored log file "00000001000000000000001F" from archive
2020-02-17 20:38:24.469 CST [51381] LOG:  restored log file "000000010000000000000020" from archive
2020-02-17 20:38:24.739 CST [51381] LOG:  restored log file "000000010000000000000021" from archive
2020-02-17 20:38:25.008 CST [51381] LOG:  restored log file "000000010000000000000022" from archive
2020-02-17 20:38:25.282 CST [51381] LOG:  restored log file "000000010000000000000023" from archive
2020-02-17 20:38:25.553 CST [51381] LOG:  restored log file "000000010000000000000024" from archive
2020-02-17 20:38:25.840 CST [51381] LOG:  restored log file "000000010000000000000025" from archive
2020-02-17 20:38:26.143 CST [51381] LOG:  restored log file "000000010000000000000026" from archive
2020-02-17 20:38:26.411 CST [51381] LOG:  restored log file "000000010000000000000027" from archive
2020-02-17 20:38:26.690 CST [51381] LOG:  restored log file "000000010000000000000028" from archive
2020-02-17 20:38:26.962 CST [51381] LOG:  restored log file "000000010000000000000029" from archive
2020-02-17 20:38:27.258 CST [51381] LOG:  restored log file "00000001000000000000002A" from archive
2020-02-17 20:38:27.675 CST [51381] LOG:  restored log file "00000001000000000000002B" from archive
2020-02-17 20:38:27.971 CST [51381] LOG:  restored log file "00000001000000000000002C" from archive
2020-02-17 20:38:28.251 CST [51381] LOG:  restored log file "00000001000000000000002D" from archive
2020-02-17 20:38:28.708 CST [51381] LOG:  restored log file "00000001000000000000002E" from archive
2020-02-17 20:38:29.298 CST [51381] LOG:  restored log file "00000001000000000000002F" from archive
2020-02-17 20:38:29.746 CST [51381] LOG:  restored log file "000000010000000000000030" from archive
2020-02-17 20:38:30.008 CST [51381] LOG:  restored log file "000000010000000000000031" from archive
2020-02-17 20:38:30.307 CST [51381] LOG:  restored log file "000000010000000000000032" from archive
2020-02-17 20:38:30.625 CST [51381] LOG:  restored log file "000000010000000000000033" from archive
2020-02-17 20:38:30.954 CST [51381] LOG:  restored log file "000000010000000000000034" from archive
2020-02-17 20:38:31.259 CST [51381] LOG:  restored log file "000000010000000000000035" from archive
2020-02-17 20:38:31.565 CST [51381] LOG:  restored log file "000000010000000000000036" from archive
2020-02-17 20:38:31.855 CST [51381] LOG:  restored log file "000000010000000000000037" from archive
2020-02-17 20:38:32.338 CST [51381] LOG:  restored log file "000000010000000000000038" from archive
2020-02-17 20:38:32.726 CST [51381] LOG:  restored log file "000000010000000000000039" from archive
2020-02-17 20:38:33.057 CST [51381] LOG:  restored log file "00000001000000000000003A" from archive
2020-02-17 20:38:33.360 CST [51381] LOG:  restored log file "00000001000000000000003B" from archive
2020-02-17 20:38:33.758 CST [51381] LOG:  restored log file "00000001000000000000003C" from archive
2020-02-17 20:38:34.166 CST [51381] LOG:  restored log file "00000001000000000000003D" from archive
2020-02-17 20:38:35.764 CST [51381] LOG:  restored log file "00000001000000000000003E" from archive
2020-02-17 20:38:36.050 CST [51381] LOG:  restored log file "00000001000000000000003F" from archive
2020-02-17 20:38:36.316 CST [51381] LOG:  restored log file "000000010000000000000040" from archive
2020-02-17 20:38:36.605 CST [51381] LOG:  restored log file "000000010000000000000041" from archive
2020-02-17 20:38:36.879 CST [51381] LOG:  restored log file "000000010000000000000042" from archive
2020-02-17 20:38:37.180 CST [51381] LOG:  restored log file "000000010000000000000043" from archive
2020-02-17 20:38:37.252 CST [51381] LOG:  restored log file "000000010000000000000044" from archive
2020-02-17 20:38:37.273 CST [51381] LOG:  recovery stopping before commit of transaction 561, time 2020-02-17 20:25:00.622538+08
2020-02-17 20:38:37.273 CST [51381] LOG:  recovery has paused
2020-02-17 20:38:37.273 CST [51381] HINT:  Execute pg_wal_replay_resume() to continue.

[postgres@localhost ~]$ 

8.3 验证,进入数据库,发现testpitr3已经恢复

[postgres@localhost ~]$ psql
psql (10.10)
Type "help" for help.

postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# \d
           List of relations
 Schema |   Name    | Type  |  Owner   
--------+-----------+-------+----------
 public | testpitr1 | table | postgres
 public | testpitr2 | table | postgres
 public | testpitr3 | table | postgres
(3 rows)

test=# select count(*) from testpitr1; select count(*) from testpitr2; select count(*) from testpitr3;
  count  
---------
 1338084
(1 row)

  count  
---------
 1349856
(1 row)

  count  
---------
 1361628
(1 row)

test=# 

8.4. 1 查看一下 archivedir 的情况

[postgres@localhost ~]$ ll -h archivedir/
total 1.1G
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000001
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000002
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000003
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000004
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000005
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000006
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000007
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000008
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000009
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000A
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000B
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000C
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000D
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000E
-rw------- 1 postgres postgres 16M Feb 17 20:19 00000001000000000000000F
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000010
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000011
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000012
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000013
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000014
-rw------- 1 postgres postgres 16M Feb 17 20:19 000000010000000000000015
-rw------- 1 postgres postgres 16M Feb 17 20:20 000000010000000000000016
-rw------- 1 postgres postgres 16M Feb 17 20:21 000000010000000000000017
-rw------- 1 postgres postgres 291 Feb 17 20:21 000000010000000000000017.00000028.backup
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000018
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000019
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000001A
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000001B
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000001C
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000001D
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000001E
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000001F
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000020
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000021
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000022
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000023
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000024
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000025
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000026
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000027
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000028
-rw------- 1 postgres postgres 16M Feb 17 20:22 000000010000000000000029
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000002A
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000002B
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000002C
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000002D
-rw------- 1 postgres postgres 16M Feb 17 20:22 00000001000000000000002E
-rw------- 1 postgres postgres 16M Feb 17 20:23 00000001000000000000002F
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000030
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000031
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000032
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000033
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000034
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000035
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000036
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000037
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000038
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000039
-rw------- 1 postgres postgres 16M Feb 17 20:23 00000001000000000000003A
-rw------- 1 postgres postgres 16M Feb 17 20:23 00000001000000000000003B
-rw------- 1 postgres postgres 16M Feb 17 20:23 00000001000000000000003C
-rw------- 1 postgres postgres 16M Feb 17 20:23 00000001000000000000003D
-rw------- 1 postgres postgres 16M Feb 17 20:23 00000001000000000000003E
-rw------- 1 postgres postgres 16M Feb 17 20:23 00000001000000000000003F
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000040
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000041
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000042
-rw------- 1 postgres postgres 16M Feb 17 20:23 000000010000000000000043
-rw------- 1 postgres postgres 16M Feb 17 20:25 000000010000000000000044
[postgres@localhost ~]$ 

总结

出现的错误

上述的"备份和还原"是第2次的结果. 第1次进行"备份和还原", 在恢复table3操作时, 出现"cp: cannot stat ‘/home/postgres/archivedir/00000001000000000000005C’: No such file or directory"错误:

[postgres@localhost ~]$ pg_ctl -D db1 start
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start....2020-02-17 19:50:27.795 CST [50373] LOG:  listening on IPv6 address "::1", port 5432
2020-02-17 19:50:27.795 CST [50373] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-02-17 19:50:27.796 CST [50373] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-02-17 19:50:27.811 CST [50374] LOG:  database system was interrupted; last known up at 2020-02-17 19:07:24 CST
2020-02-17 19:50:27.832 CST [50374] LOG:  starting point-in-time recovery to 2020-02-17 19:10:52+08
2020-02-17 19:50:27.871 CST [50374] LOG:  restored log file "00000001000000000000002E" from archive
2020-02-17 19:50:27.902 CST [50374] LOG:  redo starts at 0/2E000028
2020-02-17 19:50:27.903 CST [50374] LOG:  consistent recovery state reached at 0/2E000130
2020-02-17 19:50:27.904 CST [50373] LOG:  database system is ready to accept read only connections
2020-02-17 19:50:27.940 CST [50374] LOG:  restored log file "00000001000000000000002F" from archive
 done
server started
[postgres@localhost ~]$ 2020-02-17 19:50:28.234 CST [50374] LOG:  restored log file "000000010000000000000030" from archive
2020-02-17 19:50:28.519 CST [50374] LOG:  restored log file "000000010000000000000031" from archive
2020-02-17 19:50:28.816 CST [50374] LOG:  restored log file "000000010000000000000032" from archive
2020-02-17 19:50:29.114 CST [50374] LOG:  restored log file "000000010000000000000033" from archive
2020-02-17 19:50:29.408 CST [50374] LOG:  restored log file "000000010000000000000034" from archive
2020-02-17 19:50:29.690 CST [50374] LOG:  restored log file "000000010000000000000035" from archive
2020-02-17 19:50:29.964 CST [50374] LOG:  restored log file "000000010000000000000036" from archive
2020-02-17 19:50:30.260 CST [50374] LOG:  restored log file "000000010000000000000037" from archive
2020-02-17 19:50:30.545 CST [50374] LOG:  restored log file "000000010000000000000038" from archive
2020-02-17 19:50:30.843 CST [50374] LOG:  restored log file "000000010000000000000039" from archive
[postgres@localhost ~]$ 2020-02-17 19:50:31.190 CST [50374] LOG:  restored log file "00000001000000000000003A" from archive
2020-02-17 19:50:31.467 CST [50374] LOG:  restored log file "00000001000000000000003B" from archive
2020-02-17 19:50:31.753 CST [50374] LOG:  restored log file "00000001000000000000003C" from archive
2020-02-17 19:50:32.046 CST [50374] LOG:  restored log file "00000001000000000000003D" from archive
2020-02-17 19:50:32.351 CST [50374] LOG:  restored log file "00000001000000000000003E" from archive
2020-02-17 19:50:32.666 CST [50374] LOG:  restored log file "00000001000000000000003F" from archive
2020-02-17 19:50:32.958 CST [50374] LOG:  restored log file "000000010000000000000040" from archive
2020-02-17 19:50:33.265 CST [50374] LOG:  restored log file "000000010000000000000041" from archive
2020-02-17 19:50:33.622 CST [50374] LOG:  restored log file "000000010000000000000042" from archive
2020-02-17 19:50:33.894 CST [50374] LOG:  restored log file "000000010000000000000043" from archive
2020-02-17 19:50:34.196 CST [50374] LOG:  restored log file "000000010000000000000044" from archive
2020-02-17 19:50:34.482 CST [50374] LOG:  restored log file "000000010000000000000045" from archive
2020-02-17 19:50:34.793 CST [50374] LOG:  restored log file "000000010000000000000046" from archive
2020-02-17 19:50:35.101 CST [50374] LOG:  restored log file "000000010000000000000047" from archive
2020-02-17 19:50:35.386 CST [50374] LOG:  restored log file "000000010000000000000048" from archive
2020-02-17 19:50:35.663 CST [50374] LOG:  restored log file "000000010000000000000049" from archive
2020-02-17 19:50:35.984 CST [50374] LOG:  restored log file "00000001000000000000004A" from archive
2020-02-17 19:50:36.309 CST [50374] LOG:  restored log file "00000001000000000000004B" from archive
2020-02-17 19:50:36.624 CST [50374] LOG:  restored log file "00000001000000000000004C" from archive
2020-02-17 19:50:36.913 CST [50374] LOG:  restored log file "00000001000000000000004D" from archive
2020-02-17 19:50:37.218 CST [50374] LOG:  restored log file "00000001000000000000004E" from archive
2020-02-17 19:50:37.515 CST [50374] LOG:  restored log file "00000001000000000000004F" from archive
2020-02-17 19:50:37.852 CST [50374] LOG:  restored log file "000000010000000000000050" from archive
2020-02-17 19:50:38.201 CST [50374] LOG:  restored log file "000000010000000000000051" from archive
2020-02-17 19:50:38.489 CST [50374] LOG:  restored log file "000000010000000000000052" from archive
2020-02-17 19:50:38.790 CST [50374] LOG:  restored log file "000000010000000000000053" from archive
2020-02-17 19:50:39.088 CST [50374] LOG:  restored log file "000000010000000000000054" from archive
2020-02-17 19:50:39.507 CST [50374] LOG:  restored log file "000000010000000000000055" from archive
2020-02-17 19:50:39.793 CST [50374] LOG:  restored log file "000000010000000000000056" from archive
2020-02-17 19:50:40.240 CST [50374] LOG:  restored log file "000000010000000000000057" from archive
2020-02-17 19:50:40.533 CST [50374] LOG:  restored log file "000000010000000000000058" from archive
2020-02-17 19:50:40.854 CST [50374] LOG:  restored log file "000000010000000000000059" from archive
2020-02-17 19:50:41.135 CST [50374] LOG:  restored log file "00000001000000000000005A" from archive
2020-02-17 19:50:41.210 CST [50374] LOG:  restored log file "00000001000000000000005B" from archive
cp: cannot stat ‘/home/postgres/archivedir/00000001000000000000005C’: No such file or directory
2020-02-17 19:50:41.257 CST [50374] LOG:  redo done at 0/5B000060
2020-02-17 19:50:41.257 CST [50374] LOG:  last completed transaction was at log time 2020-02-17 19:10:45.375614+08
2020-02-17 19:50:41.278 CST [50374] LOG:  restored log file "00000001000000000000005B" from archive
cp: cannot stat ‘/home/postgres/archivedir/00000002.history’: No such file or directory
2020-02-17 19:50:41.312 CST [50374] LOG:  selected new timeline ID: 2
2020-02-17 19:50:41.347 CST [50374] LOG:  archive recovery complete
cp: cannot stat ‘/home/postgres/archivedir/00000001.history’: No such file or directory
2020-02-17 19:50:42.004 CST [50373] LOG:  database system is ready to accept connections

第1次做"备份与还原"时, 创建table3后的, archivedir目录情况如下, 查看archivedir内容, 并没有"00000001000000000000005C"归档文件.

[postgres@localhost ~]$ ll -h archivedir/
total 1.1G
-rw------- 1 postgres postgres 16M Feb 17 19:06 000000010000000000000018
-rw------- 1 postgres postgres 16M Feb 17 19:06 000000010000000000000019
-rw------- 1 postgres postgres 16M Feb 17 19:06 00000001000000000000001A
-rw------- 1 postgres postgres 16M Feb 17 19:06 00000001000000000000001B
-rw------- 1 postgres postgres 16M Feb 17 19:06 00000001000000000000001C
-rw------- 1 postgres postgres 16M Feb 17 19:06 00000001000000000000001D
-rw------- 1 postgres postgres 16M Feb 17 19:06 00000001000000000000001E
-rw------- 1 postgres postgres 16M Feb 17 19:06 00000001000000000000001F
-rw------- 1 postgres postgres 16M Feb 17 19:06 000000010000000000000020
-rw------- 1 postgres postgres 16M Feb 17 19:06 000000010000000000000021
-rw------- 1 postgres postgres 16M Feb 17 19:06 000000010000000000000022
-rw------- 1 postgres postgres 16M Feb 17 19:06 000000010000000000000023
-rw------- 1 postgres postgres 16M Feb 17 19:06 000000010000000000000024
-rw------- 1 postgres postgres 16M Feb 17 19:06 000000010000000000000025
-rw------- 1 postgres postgres 16M Feb 17 19:06 000000010000000000000026
-rw------- 1 postgres postgres 16M Feb 17 19:06 000000010000000000000027
-rw------- 1 postgres postgres 16M Feb 17 19:06 000000010000000000000028
-rw------- 1 postgres postgres 16M Feb 17 19:06 000000010000000000000029
-rw------- 1 postgres postgres 16M Feb 17 19:06 00000001000000000000002A
-rw------- 1 postgres postgres 16M Feb 17 19:06 00000001000000000000002B
-rw------- 1 postgres postgres 16M Feb 17 19:06 00000001000000000000002C
-rw------- 1 postgres postgres 16M Feb 17 19:07 00000001000000000000002D
-rw------- 1 postgres postgres 16M Feb 17 19:08 00000001000000000000002E
-rw------- 1 postgres postgres 291 Feb 17 19:08 00000001000000000000002E.00000028.backup
-rw------- 1 postgres postgres 16M Feb 17 19:10 00000001000000000000002F
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000030
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000031
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000032
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000033
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000034
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000035
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000036
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000037
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000038
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000039
-rw------- 1 postgres postgres 16M Feb 17 19:10 00000001000000000000003A
-rw------- 1 postgres postgres 16M Feb 17 19:10 00000001000000000000003B
-rw------- 1 postgres postgres 16M Feb 17 19:10 00000001000000000000003C
-rw------- 1 postgres postgres 16M Feb 17 19:10 00000001000000000000003D
-rw------- 1 postgres postgres 16M Feb 17 19:10 00000001000000000000003E
-rw------- 1 postgres postgres 16M Feb 17 19:10 00000001000000000000003F
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000040
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000041
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000042
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000043
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000044
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000045
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000046
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000047
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000048
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000049
-rw------- 1 postgres postgres 16M Feb 17 19:10 00000001000000000000004A
-rw------- 1 postgres postgres 16M Feb 17 19:10 00000001000000000000004B
-rw------- 1 postgres postgres 16M Feb 17 19:10 00000001000000000000004C
-rw------- 1 postgres postgres 16M Feb 17 19:10 00000001000000000000004D
-rw------- 1 postgres postgres 16M Feb 17 19:10 00000001000000000000004E
-rw------- 1 postgres postgres 16M Feb 17 19:10 00000001000000000000004F
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000050
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000051
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000052
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000053
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000054
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000055
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000056
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000057
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000058
-rw------- 1 postgres postgres 16M Feb 17 19:10 000000010000000000000059
-rw------- 1 postgres postgres 16M Feb 17 19:11 00000001000000000000005A
-rw------- 1 postgres postgres 16M Feb 17 19:12 00000001000000000000005B
-rw------- 1 postgres postgres  44 Feb 17 19:50 00000002.history
[postgres@localhost ~]$

但table3恢复后的查询结果和第2次均一样: 1361628

[postgres@localhost ~]$ psql
psql (10.10)
Type "help" for help.

postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# \d
           List of relations
 Schema |   Name    | Type  |  Owner   
--------+-----------+-------+----------
 public | testpitr1 | table | postgres
 public | testpitr2 | table | postgres
 public | testpitr3 | table | postgres
(3 rows)

test=# select count(*) from testpitr3;
  count  
---------
 1361628
(1 row)

test=# 

错误可能原因分析

具体原因未知, 唯一找到的差异如下, 且记录下来:

第1次做时, postgres.conf 中 archive_command的赋值如下:

archive_command = ' test ! -f /home/postgres/archivedir/%f && cp %p /home/postgres/archivedir/%f' 

第2次做时, postgres.conf 中 archive_command的赋值如下:

archive_command = 'test ! -f /home/postgres/archivedir/%f && cp %p /home/postgres/archivedir/%f'

看出来了没, test 前面多了一个空格, 不知道是不是此问题引起的.

另外, 两次备份与还原后的table3的查询结果均正确, 说明第1次尽管报错了, 但有可能成功恢复了.

参考

PostgreSQL 9.5 基于日志的备份与还原

你可能感兴趣的:(postgresql)