--//当前时间线为5
--//插入一些数据
[postgres@postgres pg_wal]$ p
psql (11.1)
Type "help" for help.
postgres=# select * from tbl;
id | ival | description | created_time
----+------+-------------+-------------------------------
1 | 1 | | 2020-04-02 11:02:33.039506-04
34 | 2 | | 2020-04-02 13:52:24.368258-04
(2 rows)
postgres=# insert into tbl(ival) values (3);
INSERT 0 1
postgres=# select * from tbl;
id | ival | description | created_time
----+------+-------------+-------------------------------
1 | 1 | | 2020-04-02 11:02:33.039506-04
34 | 2 | | 2020-04-02 13:52:24.368258-04
67 | 3 | | 2020-04-02 14:32:05.711384-04
(3 rows)
postgres=#
postgres=# select pg_switch_wal();
pg_switch_wal
---------------
0/B000328
(1 row)
postgres=# select pg_switch_wal();
pg_switch_wal
---------------
0/C000078
(1 row)
[postgres@postgres pg_wal]$ ls -ltr
total 65552
-rw------- 1 postgres postgres 50 Apr 2 14:30 00000004.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:30 000000030000000000000009
-rw------- 1 postgres postgres 16777216 Apr 2 14:30 00000004000000000000000A
-rw------- 1 postgres postgres 50 Apr 2 14:30 RECOVERYHISTORY
-rw------- 1 postgres postgres 92 Apr 2 14:30 00000005.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:32 00000005000000000000000B
-rw------- 1 postgres postgres 16777216 Apr 2 14:32 00000005000000000000000C
drwx------ 2 postgres postgres 4096 Apr 2 14:32 archive_status
[postgres@postgres pg_wal]$
--//做一个基础备份
pg_basebackup -Ft -Pv -Xf -z -Z5 -h127.0.0.1 -p 5442 -D /home/postgres/11/backups/new
[postgres@postgres pg_wal]$ pg_basebackup -Ft -Pv -Xf -z -Z5 -h127.0.0.1 -p 5442 -D /home/postgres/11/backups/new
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/E000028 on timeline 5
41728/41728 kB (100%), 2/2 tablespaces
pg_basebackup: write-ahead log end point: 0/E0000F8
pg_basebackup: base backup completed
[postgres@postgres pg_wal]$
[postgres@postgres pg_wal]$ ls -ltr
total 114708
-rw------- 1 postgres postgres 50 Apr 2 14:30 00000004.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:30 000000030000000000000009
-rw------- 1 postgres postgres 16777216 Apr 2 14:30 00000004000000000000000A
-rw------- 1 postgres postgres 50 Apr 2 14:30 RECOVERYHISTORY
-rw------- 1 postgres postgres 92 Apr 2 14:30 00000005.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:32 00000005000000000000000B
-rw------- 1 postgres postgres 16777216 Apr 2 14:32 00000005000000000000000C
-rw------- 1 postgres postgres 16777216 Apr 2 14:34 00000005000000000000000D
-rw------- 1 postgres postgres 16777216 Apr 2 14:34 00000005000000000000000E
-rw------- 1 postgres postgres 337 Apr 2 14:34 00000005000000000000000E.00000028.backup
drwx------ 2 postgres postgres 4096 Apr 2 14:34 archive_status
-rw------- 1 postgres postgres 16777216 Apr 2 14:34 00000005000000000000000F
[postgres@postgres pg_wal]$
--//删除数据库用基于时间线5的备份来恢复
[postgres@postgres pg_wal]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[postgres@postgres pg_wal]$ rm -rf /home/postgres/pg11/
[postgres@postgres pg_wal]$ mkdir -p /home/postgres/pg11/
[postgres@postgres pg_wal]$ chmod 0700 /home/postgres/pg11/
[postgres@postgres pg_wal]$
[postgres@postgres new]$ ls -ltr /home/postgres/11/backups/new
total 2948
-rw------- 1 postgres postgres 433 Apr 2 14:34 16387.tar.gz
-rw------- 1 postgres postgres 3014491 Apr 2 14:34 base.tar.gz
[postgres@postgres new]$
--//恢复
tar -xvf /home/postgres/11/backups/new/base.tar.gz -C /home/postgres/pg11/
[postgres@postgres new]$ tar -xvf /home/postgres/11/backups/new/base.tar.gz -C /home/postgres/pg11/
backup_label
tablespace_map
current_logfiles
pg_multixact/
pg_multixact/offsets/
pg_multixact/offsets/0000
pg_multixact/members/
.
.
.
--//启动数据库
cp /home/postgres/recovery.done /home/postgres/pg11/recovery.conf
[postgres@postgres new]$ pg_ctl start
waiting for server to start....2020-04-02 14:37:28.485 EDT [41164] LOG: listening on IPv4 address "0.0.0.0", port 5442
2020-04-02 14:37:28.485 EDT [41164] LOG: listening on IPv6 address "::", port 5442
2020-04-02 14:37:28.488 EDT [41164] LOG: listening on Unix socket "/tmp/.s.PGSQL.5442"
2020-04-02 14:37:28.499 EDT [41164] LOG: redirecting log output to logging collector process
2020-04-02 14:37:28.499 EDT [41164] HINT: Future log output will appear in directory "on".
done
server started
[postgres@postgres new]$
2020-04-02 14:37:28.539 EDT,,,41166,,5e8630e8.a0ce,2,,2020-04-02 14:37:28 EDT,,0,LOG,00000,"starting archive recovery",,,,,,,,,""
2020-04-02 14:37:28.542 EDT,,,41166,,5e8630e8.a0ce,3,,2020-04-02 14:37:28 EDT,,0,LOG,00000,"restored log file ""00000005.history"" from archive",,,,,,,,,""
2020-04-02 14:37:28.585 EDT,,,41166,,5e8630e8.a0ce,4,,2020-04-02 14:37:28 EDT,,0,LOG,00000,"restored log file ""00000005000000000000000E"" from archive",,,,,,,,,""
2020-04-02 14:37:28.616 EDT,,,41166,,5e8630e8.a0ce,5,,2020-04-02 14:37:28 EDT,1/0,0,LOG,00000,"redo starts at 0/E000028",,,,,,,,,""
2020-04-02 14:37:28.617 EDT,,,41166,,5e8630e8.a0ce,6,,2020-04-02 14:37:28 EDT,1/0,0,LOG,00000,"consistent recovery state reached at 0/E0000F8",,,,,,,,,""
2020-04-02 14:37:28.617 EDT,,,41164,,5e8630e8.a0cc,2,,2020-04-02 14:37:28 EDT,,0,LOG,00000,"database system is ready to accept read only connections",,,,,,,,,""
2020-04-02 14:37:28.633 EDT,,,41166,,5e8630e8.a0ce,7,,2020-04-02 14:37:28 EDT,1/0,0,LOG,00000,"restored log file ""00000005000000000000000F"" from archive",,,,,,,,,""
2020-04-02 14:37:28.661 EDT,,,41166,,5e8630e8.a0ce,8,,2020-04-02 14:37:28 EDT,1/0,0,LOG,00000,"invalid record length at 0/10000098: wanted 24, got 0",,,,,,,,,""
2020-04-02 14:37:28.661 EDT,,,41166,,5e8630e8.a0ce,9,,2020-04-02 14:37:28 EDT,1/0,0,LOG,00000,"redo done at 0/10000028",,,,,,,,,""
2020-04-02 14:37:28.666 EDT,,,41166,,5e8630e8.a0ce,10,,2020-04-02 14:37:28 EDT,1/0,0,LOG,00000,"selected new timeline ID: 6",,,,,,,,,""
2020-04-02 14:37:28.702 EDT,,,41166,,5e8630e8.a0ce,11,,2020-04-02 14:37:28 EDT,1/0,0,LOG,00000,"archive recovery complete",,,,,,,,,""
2020-04-02 14:37:28.705 EDT,,,41166,,5e8630e8.a0ce,12,,2020-04-02 14:37:28 EDT,1/0,0,LOG,00000,"restored log file ""00000005.history"" from archive",,,,,,,,,""
2020-04-02 14:37:28.813 EDT,,,41164,,5e8630e8.a0cc,3,,2020-04-02 14:37:28 EDT,,0,LOG,00000,"database system is ready to accept connections",,,,,,,,,""
--//产生新的时间线pg_wal目录
[postgres@postgres pg_wal]$ ls -ltr
total 147480
-rw------- 1 postgres postgres 50 Apr 2 14:30 00000004.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:30 000000030000000000000009
-rw------- 1 postgres postgres 16777216 Apr 2 14:30 00000004000000000000000A
-rw------- 1 postgres postgres 16777216 Apr 2 14:32 00000005000000000000000B
-rw------- 1 postgres postgres 16777216 Apr 2 14:32 00000005000000000000000C
-rw------- 1 postgres postgres 16777216 Apr 2 14:34 00000005000000000000000D
-rw------- 1 postgres postgres 337 Apr 2 14:34 00000005000000000000000E.00000028.backup
-rw------- 1 postgres postgres 16777216 Apr 2 14:35 000000050000000000000010.partial
-rw------- 1 postgres postgres 92 Apr 2 14:37 00000005.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:37 00000005000000000000000E
-rw------- 1 postgres postgres 16777216 Apr 2 14:37 00000005000000000000000F
-rw------- 1 postgres postgres 92 Apr 2 14:37 RECOVERYHISTORY
-rw------- 1 postgres postgres 135 Apr 2 14:37 00000006.history
drwx------ 2 postgres postgres 4096 Apr 2 14:37 archive_status
-rw------- 1 postgres postgres 16777216 Apr 2 14:37 000000060000000000000010
[postgres@postgres pg_wal]$
--//归档
[postgres@postgres pg_wal]$ ls -ltr
total 147480
-rw------- 1 postgres postgres 50 Apr 2 14:30 00000004.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:30 000000030000000000000009
-rw------- 1 postgres postgres 16777216 Apr 2 14:30 00000004000000000000000A
-rw------- 1 postgres postgres 16777216 Apr 2 14:32 00000005000000000000000B
-rw------- 1 postgres postgres 16777216 Apr 2 14:32 00000005000000000000000C
-rw------- 1 postgres postgres 16777216 Apr 2 14:34 00000005000000000000000D
-rw------- 1 postgres postgres 337 Apr 2 14:34 00000005000000000000000E.00000028.backup
-rw------- 1 postgres postgres 16777216 Apr 2 14:35 000000050000000000000010.partial
-rw------- 1 postgres postgres 92 Apr 2 14:37 00000005.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:37 00000005000000000000000E
-rw------- 1 postgres postgres 16777216 Apr 2 14:37 00000005000000000000000F
-rw------- 1 postgres postgres 92 Apr 2 14:37 RECOVERYHISTORY
-rw------- 1 postgres postgres 135 Apr 2 14:37 00000006.history
drwx------ 2 postgres postgres 4096 Apr 2 14:37 archive_status
-rw------- 1 postgres postgres 16777216 Apr 2 14:37 000000060000000000000010
[postgres@postgres pg_wal]$
--//在新的时间线插入一行数据
[postgres@postgres on]$ p
psql (11.1)
Type "help" for help.
postgres=# select * from tbl;
id | ival | description | created_time
----+------+-------------+-------------------------------
1 | 1 | | 2020-04-02 11:02:33.039506-04
34 | 2 | | 2020-04-02 13:52:24.368258-04
67 | 3 | | 2020-04-02 14:32:05.711384-04
(3 rows)
postgres=# insert into tbl(ival) values (4);
INSERT 0 1
postgres=# select * from tbl;
id | ival | description | created_time
----+------+-------------+-------------------------------
1 | 1 | | 2020-04-02 11:02:33.039506-04
34 | 2 | | 2020-04-02 13:52:24.368258-04
67 | 3 | | 2020-04-02 14:32:05.711384-04
68 | 4 | | 2020-04-02 14:40:01.597981-04
(4 rows)
postgres=#
postgres=# select pg_switch_wal();
pg_switch_wal
---------------
0/100003D0
(1 row)
postgres=# select pg_switch_wal();
pg_switch_wal
---------------
0/11000078
(1 row)
postgres=#
--//查看pg_wal
[postgres@postgres pg_wal]$ ls -ltr
total 180248
-rw------- 1 postgres postgres 50 Apr 2 14:30 00000004.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:30 000000030000000000000009
-rw------- 1 postgres postgres 16777216 Apr 2 14:30 00000004000000000000000A
-rw------- 1 postgres postgres 16777216 Apr 2 14:32 00000005000000000000000B
-rw------- 1 postgres postgres 16777216 Apr 2 14:32 00000005000000000000000C
-rw------- 1 postgres postgres 16777216 Apr 2 14:34 00000005000000000000000D
-rw------- 1 postgres postgres 337 Apr 2 14:34 00000005000000000000000E.00000028.backup
-rw------- 1 postgres postgres 16777216 Apr 2 14:35 000000050000000000000010.partial
-rw------- 1 postgres postgres 92 Apr 2 14:37 00000005.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:37 00000005000000000000000E
-rw------- 1 postgres postgres 16777216 Apr 2 14:37 00000005000000000000000F
-rw------- 1 postgres postgres 92 Apr 2 14:37 RECOVERYHISTORY
-rw------- 1 postgres postgres 135 Apr 2 14:37 00000006.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:40 000000060000000000000010
-rw------- 1 postgres postgres 16777216 Apr 2 14:40 000000060000000000000011
drwx------ 2 postgres postgres 4096 Apr 2 14:40 archive_status
-rw------- 1 postgres postgres 16777216 Apr 2 14:40 000000060000000000000012
[postgres@postgres pg_wal]$
--//基于时间线6再做一个基础备份
mkdir -p /home/postgres/11/backups/new6
pg_basebackup -Ft -Pv -Xf -z -Z5 -h127.0.0.1 -p 5442 -D /home/postgres/11/backups/new6
[postgres@postgres ~]$ pg_basebackup -Ft -Pv -Xf -z -Z5 -h127.0.0.1 -p 5442 -D /home/postgres/11/backups/new6
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/13000028 on timeline 6
41734/41734 kB (100%), 2/2 tablespaces
pg_basebackup: write-ahead log end point: 0/130000F8
pg_basebackup: base backup completed
[postgres@postgres ~]$
--//查看pg_wals
[postgres@postgres pg_wal]$ ls -ltr
total 213016
-rw------- 1 postgres postgres 50 Apr 2 14:30 00000004.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:30 000000030000000000000009
-rw------- 1 postgres postgres 16777216 Apr 2 14:30 00000004000000000000000A
-rw------- 1 postgres postgres 16777216 Apr 2 14:32 00000005000000000000000B
-rw------- 1 postgres postgres 16777216 Apr 2 14:32 00000005000000000000000C
-rw------- 1 postgres postgres 16777216 Apr 2 14:34 00000005000000000000000D
-rw------- 1 postgres postgres 16777216 Apr 2 14:35 000000050000000000000010.partial
-rw------- 1 postgres postgres 92 Apr 2 14:37 00000005.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:37 00000005000000000000000E
-rw------- 1 postgres postgres 16777216 Apr 2 14:37 00000005000000000000000F
-rw------- 1 postgres postgres 92 Apr 2 14:37 RECOVERYHISTORY
-rw------- 1 postgres postgres 135 Apr 2 14:37 00000006.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:40 000000060000000000000010
-rw------- 1 postgres postgres 16777216 Apr 2 14:40 000000060000000000000011
-rw------- 1 postgres postgres 16777216 Apr 2 14:41 000000060000000000000012
-rw------- 1 postgres postgres 16777216 Apr 2 14:41 000000060000000000000013
-rw------- 1 postgres postgres 340 Apr 2 14:41 000000060000000000000013.00000028.backup
drwx------ 2 postgres postgres 4096 Apr 2 14:41 archive_status
-rw------- 1 postgres postgres 16777216 Apr 2 14:41 000000060000000000000014
--//查看归档
[postgres@postgres archivedir]$ ls -ltr
total 245784
-rw------- 1 postgres postgres 16777216 Apr 2 13:51 000000030000000000000008
-rw------- 1 postgres postgres 16777216 Apr 2 13:51 000000030000000000000009
-rw------- 1 postgres postgres 337 Apr 2 13:51 000000030000000000000009.00000028.backup
-rw------- 1 postgres postgres 16777216 Apr 2 13:53 00000003000000000000000A
-rw------- 1 postgres postgres 16777216 Apr 2 13:53 00000003000000000000000B
-rw------- 1 postgres postgres 50 Apr 2 13:56 00000004.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:30 00000004000000000000000A
-rw------- 1 postgres postgres 92 Apr 2 14:30 00000005.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:32 00000005000000000000000B
-rw------- 1 postgres postgres 16777216 Apr 2 14:32 00000005000000000000000C
-rw------- 1 postgres postgres 16777216 Apr 2 14:34 00000005000000000000000D
-rw------- 1 postgres postgres 16777216 Apr 2 14:34 00000005000000000000000E
-rw------- 1 postgres postgres 337 Apr 2 14:34 00000005000000000000000E.00000028.backup
-rw------- 1 postgres postgres 16777216 Apr 2 14:35 00000005000000000000000F
-rw------- 1 postgres postgres 16777216 Apr 2 14:37 000000050000000000000010.partial
-rw------- 1 postgres postgres 135 Apr 2 14:37 00000006.history
-rw------- 1 postgres postgres 16777216 Apr 2 14:40 000000060000000000000010
-rw------- 1 postgres postgres 16777216 Apr 2 14:40 000000060000000000000011
-rw------- 1 postgres postgres 16777216 Apr 2 14:41 000000060000000000000012
-rw------- 1 postgres postgres 16777216 Apr 2 14:41 000000060000000000000013
-rw------- 1 postgres postgres 340 Apr 2 14:41 000000060000000000000013.00000028.backup
--//删除数据库
[postgres@postgres archivedir]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[postgres@postgres archivedir]$
[postgres@postgres archivedir]$ rm -rf /home/postgres/pg11/
[postgres@postgres archivedir]$ mkdir -p /home/postgres/pg11/
[postgres@postgres archivedir]$ chmod 0700 /home/postgres/pg11/
--//用时间线5的备份来做恢复
[postgres@postgres archivedir]$ ls -ltr /home/postgres/11/backups/new
[postgres@postgres archivedir]$ tar -xvf /home/postgres/11/backups/new/base.tar.gz -C /home/postgres/pg11/
backup_label
tablespace_map
current_logfiles
pg_multixact/
pg_multixact/offsets/
pg_multixact/offsets/0000
pg_multixact/members/
pg_multixact/members/000
.
.
.
--//启动数据库
[postgres@postgres archivedir]$ cp /home/postgres/recovery.done /home/postgres/pg11/recovery.conf
[postgres@postgres archivedir]$ grep -v '^#' /home/postgres/pg11/recovery.conf
restore_command = 'cp /home/postgres/archivedir/%f %p'
recovery_target_timeline = 'latest' --//恢复到最近的
[postgres@postgres archivedir]$
[postgres@postgres archivedir]$ pg_ctl start
waiting for server to start....2020-04-02 14:47:27.961 EDT [42699] LOG: listening on IPv4 address "0.0.0.0", port 5442
2020-04-02 14:47:27.961 EDT [42699] LOG: listening on IPv6 address "::", port 5442
2020-04-02 14:47:27.964 EDT [42699] LOG: listening on Unix socket "/tmp/.s.PGSQL.5442"
2020-04-02 14:47:27.977 EDT [42699] LOG: redirecting log output to logging collector process
2020-04-02 14:47:27.977 EDT [42699] HINT: Future log output will appear in directory "on".
done
server started
--//数据全部恢复(包括时间线5和时间线6的)
[postgres@postgres archivedir]$
postgres=# select * from tbl;
id | ival | description | created_time
----+------+-------------+-------------------------------
1 | 1 | | 2020-04-02 11:02:33.039506-04
34 | 2 | | 2020-04-02 13:52:24.368258-04
67 | 3 | | 2020-04-02 14:32:05.711384-04
68 | 4 | | 2020-04-02 14:40:01.597981-04
(4 rows)
postgres=#
2020-04-02 14:47:28.040 EDT,,,42701,,5e86333f.a6cd,5,,2020-04-02 14:47:27 EDT,,0,LOG,00000,"restored log file ""00000005000000000000000E"" from archive",,,,,,,,,""
--//先恢复时间线5的归档
2020-04-02 14:47:28.071 EDT,,,42701,,5e86333f.a6cd,6,,2020-04-02 14:47:27 EDT,,0,LOG,00000,"restored log file ""00000005.history"" from archive",,,,,,,,,""
2020-04-02 14:47:28.074 EDT,,,42701,,5e86333f.a6cd,7,,2020-04-02 14:47:27 EDT,1/0,0,LOG,00000,"redo starts at 0/E000028",,,,,,,,,""
2020-04-02 14:47:28.075 EDT,,,42701,,5e86333f.a6cd,8,,2020-04-02 14:47:27 EDT,1/0,0,LOG,00000,"consistent recovery state reached at 0/E0000F8",,,,,,,,,""
2020-04-02 14:47:28.075 EDT,,,42699,,5e86333f.a6cb,2,,2020-04-02 14:47:27 EDT,,0,LOG,00000,"database system is ready to accept read only connections",,,,,,,,,""
2020-04-02 14:47:28.096 EDT,,,42701,,5e86333f.a6cd,9,,2020-04-02 14:47:27 EDT,1/0,0,LOG,00000,"restored log file ""00000005000000000000000F"" from archive",,,,,,,,,""
--//恢复时间线5之后的归档
2020-04-02 14:47:28.142 EDT,,,42701,,5e86333f.a6cd,10,,2020-04-02 14:47:27 EDT,1/0,0,LOG,00000,"restored log file ""000000060000000000000010"" from archive",,,,,,,,,""
2020-04-02 14:47:28.340 EDT,,,42701,,5e86333f.a6cd,11,,2020-04-02 14:47:27 EDT,1/0,0,LOG,00000,"restored log file ""000000060000000000000011"" from archive",,,,,,,,,""
2020-04-02 14:47:28.405 EDT,,,42701,,5e86333f.a6cd,12,,2020-04-02 14:47:27 EDT,1/0,0,LOG,00000,"restored log file ""000000060000000000000012"" from archive",,,,,,,,,""
2020-04-02 14:47:28.450 EDT,,,42701,,5e86333f.a6cd,13,,2020-04-02 14:47:27 EDT,1/0,0,LOG,00000,"restored log file ""000000060000000000000013"" from archive",,,,,,,,,""
2020-04-02 14:47:28.490 EDT,,,42701,,5e86333f.a6cd,14,,2020-04-02 14:47:27 EDT,1/0,0,LOG,00000,"restored log file ""000000060000000000000014"" from archive",,,,,,,,,""
--//开始应用时间线6的归档
2020-04-02 14:47:28.517 EDT,,,42701,,5e86333f.a6cd,15,,2020-04-02 14:47:27 EDT,1/0,0,LOG,00000,"redo done at 0/14000060",,,,,,,,,""
2020-04-02 14:47:28.517 EDT,,,42701,,5e86333f.a6cd,16,,2020-04-02 14:47:27 EDT,1/0,0,LOG,00000,"last completed transaction was at log time 2020-04-02 14:40:01.598839-04",,,,,,,,,""
2020-04-02 14:47:28.536 EDT,,,42701,,5e86333f.a6cd,17,,2020-04-02 14:47:27 EDT,1/0,0,LOG,00000,"restored log file ""000000060000000000000014"" from archive",,,,,,,,,""
2020-04-02 14:47:28.565 EDT,,,42701,,5e86333f.a6cd,18,,2020-04-02 14:47:27 EDT,1/0,0,LOG,00000,"selected new timeline ID: 7",,,,,,,,,""
2020-04-02 14:47:28.597 EDT,,,42701,,5e86333f.a6cd,19,,2020-04-02 14:47:27 EDT,1/0,0,LOG,00000,"archive recovery complete",,,,,,,,,""
2020-04-02 14:47:28.599 EDT,,,42701,,5e86333f.a6cd,20,,2020-04-02 14:47:27 EDT,1/0,0,LOG,00000,"restored log file ""00000006.history"" from archive",,,,,,,,,""
2020-04-02 14:47:28.704 EDT,,,42699,,5e86333f.a6cb,3,,2020-04-02 14:47:27 EDT,,0,LOG,00000,"database system is ready to accept connections",,,,,,,,,""
"postgresql-2020-04-02_144727.csv" 23L, 3646C
情况2
--//恢复到指定时间线5
--//理论上tbl只会有3行记录,第四行记录发生在时间线6
--//停数据库
[postgres@postgres on]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[postgres@postgres on]$
--//删除数据
[postgres@postgres on]$ rm -rf /home/postgres/pg11/
[postgres@postgres on]$ cd
[postgres@postgres ~]$ mkdir -p /home/postgres/pg11/
[postgres@postgres ~]$ chmod 0700 /home/postgres/pg11/
[postgres@postgres ~]$
--//使用时间线5的基础备份恢复
[postgres@postgres ~]$ ls -ltr /home/postgres/11/backups/new
total 2948
-rw------- 1 postgres postgres 433 Apr 2 14:34 16387.tar.gz
-rw------- 1 postgres postgres 3014491 Apr 2 14:34 base.tar.gz
[postgres@postgres ~]$ tar -xvf /home/postgres/11/backups/new/base.tar.gz -C /home/postgres/pg11/
backup_label
tablespace_map
current_logfiles
pg_multixact/
.
.
.
--//copy recovery.conf文件
[postgres@postgres ~]$ cp /home/postgres/recovery.done /home/postgres/pg11/recovery.conf
[postgres@postgres ~]$ grep -v '^#' /home/postgres/pg11/recovery.conf
restore_command = 'cp /home/postgres/archivedir/%f %p'
recovery_target_timeline = '5'
[postgres@postgres ~]$
--//启动数据库
[postgres@postgres ~]$ pg_ctl start
waiting for server to start....2020-04-02 15:00:15.563 EDT [44602] LOG: listening on IPv4 address "0.0.0.0", port 5442
2020-04-02 15:00:15.563 EDT [44602] LOG: listening on IPv6 address "::", port 5442
2020-04-02 15:00:15.567 EDT [44602] LOG: listening on Unix socket "/tmp/.s.PGSQL.5442"
2020-04-02 15:00:15.581 EDT [44602] LOG: redirecting log output to logging collector process
2020-04-02 15:00:15.581 EDT [44602] HINT: Future log output will appear in directory "on".
done
server started
[postgres@postgres ~]$
--//查看日志
2020-04-02 15:00:15.619 EDT,,,44604,,5e86363f.ae3c,3,,2020-04-02 15:00:15 EDT,,0,LOG,00000,"starting archive recovery",,,,,,,,,""
2020-04-02 15:00:15.621 EDT,,,44604,,5e86363f.ae3c,4,,2020-04-02 15:00:15 EDT,,0,LOG,00000,"restored log file ""00000005.history"" from archive",,,,,,,,,""
2020-04-02 15:00:15.655 EDT,,,44604,,5e86363f.ae3c,5,,2020-04-02 15:00:15 EDT,,0,LOG,00000,"restored log file ""00000005000000000000000E"" from archive",,,,,,,,,""
2020-04-02 15:00:15.689 EDT,,,44604,,5e86363f.ae3c,6,,2020-04-02 15:00:15 EDT,1/0,0,LOG,00000,"redo starts at 0/E000028",,,,,,,,,""
2020-04-02 15:00:15.690 EDT,,,44604,,5e86363f.ae3c,7,,2020-04-02 15:00:15 EDT,1/0,0,LOG,00000,"consistent recovery state reached at 0/E0000F8",,,,,,,,,""
2020-04-02 15:00:15.690 EDT,,,44602,,5e86363f.ae3a,2,,2020-04-02 15:00:15 EDT,,0,LOG,00000,"database system is ready to accept read only connections",,,,,,,,,""
2020-04-02 15:00:15.711 EDT,,,44604,,5e86363f.ae3c,8,,2020-04-02 15:00:15 EDT,1/0,0,LOG,00000,"restored log file ""00000005000000000000000F"" from archive",,,,,,,,,""
2020-04-02 15:00:15.740 EDT,,,44604,,5e86363f.ae3c,9,,2020-04-02 15:00:15 EDT,1/0,0,LOG,00000,"redo done at 0/F000060",,,,,,,,,""
2020-04-02 15:00:15.763 EDT,,,44604,,5e86363f.ae3c,10,,2020-04-02 15:00:15 EDT,1/0,0,LOG,00000,"restored log file ""00000005000000000000000F"" from archive",,,,,,,,,""
2020-04-02 15:00:15.800 EDT,,,44604,,5e86363f.ae3c,11,,2020-04-02 15:00:15 EDT,1/0,0,LOG,00000,"restored log file ""00000006.history"" from archive",,,,,,,,,""
2020-04-02 15:00:15.802 EDT,,,44604,,5e86363f.ae3c,12,,2020-04-02 15:00:15 EDT,1/0,0,LOG,00000,"restored log file ""00000007.history"" from archive",,,,,,,,,""
2020-04-02 15:00:15.803 EDT,,,44604,,5e86363f.ae3c,13,,2020-04-02 15:00:15 EDT,1/0,0,LOG,00000,"selected new timeline ID: 8",,,,,,,,,""
2020-04-02 15:00:15.836 EDT,,,44604,,5e86363f.ae3c,14,,2020-04-02 15:00:15 EDT,1/0,0,LOG,00000,"archive recovery complete",,,,,,,,,""
2020-04-02 15:00:15.838 EDT,,,44604,,5e86363f.ae3c,15,,2020-04-02 15:00:15 EDT,1/0,0,LOG,00000,"restored log file ""00000005.history"" from archive",,,,,,,,,""
2020-04-02 15:00:15.944 EDT,,,44602,,5e86363f.ae3a,3,,2020-04-02 15:00:15 EDT,,0,LOG,00000,"database system is ready to accept connections",,,,,,,,,""
--//查看pg_wal
[postgres@postgres pg_wal]$ ls -ltr
total 49172
-rw------- 1 postgres postgres 50 Apr 2 14:30 00000004.history
-rw------- 1 postgres postgres 92 Apr 2 15:00 00000005.history
-rw------- 1 postgres postgres 16777216 Apr 2 15:00 00000005000000000000000E
-rw------- 1 postgres postgres 16777216 Apr 2 15:00 00000005000000000000000F
-rw------- 1 postgres postgres 92 Apr 2 15:00 RECOVERYHISTORY
-rw------- 1 postgres postgres 135 Apr 2 15:00 00000008.history
drwx------ 2 postgres postgres 4096 Apr 2 15:00 archive_status
-rw------- 1 postgres postgres 16777216 Apr 2 15:00 000000080000000000000010
[postgres@postgres pg_wal]$
--//查询tbl表 时间线6的数据没有包括
[postgres@postgres pg_wal]$ p
psql (11.1)
Type "help" for help.
postgres=# select * from tbl;
id | ival | description | created_time
----+------+-------------+-------------------------------
1 | 1 | | 2020-04-02 11:02:33.039506-04
34 | 2 | | 2020-04-02 13:52:24.368258-04
67 | 3 | | 2020-04-02 14:32:05.711384-04
(3 rows)
postgres=#
情况3
--//使用基于时间线6的基础备份恢复到时间线5 会不会报错??
--//理论上tbl有4行记录
--//停数据库
[postgres@postgres ~]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[postgres@postgres ~]$
--//删除数据
[postgres@postgres on]$ rm -rf /home/postgres/pg11/
[postgres@postgres on]$ cd
[postgres@postgres ~]$ mkdir -p /home/postgres/pg11/
[postgres@postgres ~]$ chmod 0700 /home/postgres/pg11/
[postgres@postgres ~]$
--//使用时间线6的基础备份恢复
[postgres@postgres ~]$ ls -ltr /home/postgres/11/backups/new6
total 2952
-rw------- 1 postgres postgres 433 Apr 2 14:41 16387.tar.gz
-rw------- 1 postgres postgres 3014883 Apr 2 14:41 base.tar.gz
[postgres@postgres ~]$
[postgres@postgres ~]$ tar -xvf /home/postgres/11/backups/new6/base.tar.gz -C /home/postgres/pg11/
[postgres@postgres ~]$ tar -xvf /home/postgres/11/backups/new6/base.tar.gz -C /home/postgres/pg11/
backup_label
tablespace_map
current_logfiles
pg_multixact/
pg_multixact/offsets/
pg_multixact/offsets/0000
pg_multixact/members/
pg_multixact/members/0000
pg_ident.conf
.
.
.
--//copy recovery.conf文件 恢复到时间线5
[postgres@postgres ~]$ cp /home/postgres/recovery.done /home/postgres/pg11/recovery.conf
[postgres@postgres ~]$ grep -v '^#' /home/postgres/pg11/recovery.conf
restore_command = 'cp /home/postgres/archivedir/%f %p'
recovery_target_timeline = '5'
[postgres@postgres ~]$
--//启动数据库(启动失败)
[postgres@postgres pg_wal]$ pg_ctl start
waiting for server to start....2020-04-02 15:05:57.556 EDT [45465] LOG: listening on IPv4 address "0.0.0.0", port 5442
2020-04-02 15:05:57.556 EDT [45465] LOG: listening on IPv6 address "::", port 5442
2020-04-02 15:05:57.559 EDT [45465] LOG: listening on Unix socket "/tmp/.s.PGSQL.5442"
2020-04-02 15:05:57.571 EDT [45465] LOG: redirecting log output to logging collector process
2020-04-02 15:05:57.571 EDT [45465] HINT: Future log output will appear in directory "on".
stopped waiting
pg_ctl: could not start server
Examine the log output.
[postgres@postgres pg_wal]$
[postgres@postgres pg_wal]$
--//详细日志
2020-04-02 15:05:57.571 EDT,,,45465,,5e863795.b199,1,,2020-04-02 15:05:57 EDT,,0,LOG,00000,"ending log output to stderr",,"Future log output will go to log destination ""csvlog"".",,,,,,,""
2020-04-02 15:05:57.573 EDT,,,45467,,5e863795.b19b,1,,2020-04-02 15:05:57 EDT,,0,LOG,00000,"database system was interrupted; last known up at 2020-04-02 14:41:51 EDT",,,,,,,,,""
2020-04-02 15:05:57.609 EDT,,,45467,,5e863795.b19b,2,,2020-04-02 15:05:57 EDT,,0,LOG,00000,"restored log file ""00000005.history"" from archive",,,,,,,,,""
2020-04-02 15:05:57.609 EDT,,,45467,,5e863795.b19b,3,,2020-04-02 15:05:57 EDT,,0,LOG,00000,"starting archive recovery",,,,,,,,,""
2020-04-02 15:05:57.612 EDT,,,45467,,5e863795.b19b,4,,2020-04-02 15:05:57 EDT,,0,LOG,00000,"restored log file ""00000005.history"" from archive",,,,,,,,,""
2020-04-02 15:05:57.621 EDT,,,45467,,5e863795.b19b,5,,2020-04-02 15:05:57 EDT,,0,LOG,00000,"invalid checkpoint record",,,,,,,,,""
2020-04-02 15:05:57.621 EDT,,,45467,,5e863795.b19b,6,,2020-04-02 15:05:57 EDT,,0,FATAL,XX000,"could not locate required checkpoint record",,"If you are not restoring from a backup, try removing the file ""/home/postgres/pg11/backup_label"".",,,,,,,""
2020-04-02 15:05:57.622 EDT,,,45465,,5e863795.b199,2,,2020-04-02 15:05:57 EDT,,0,LOG,00000,"startup process (PID 45467) exited with exit code 1",,,,,,,,,""
2020-04-02 15:05:57.622 EDT,,,45465,,5e863795.b199,3,,2020-04-02 15:05:57 EDT,,0,LOG,00000,"aborting startup due to startup process failure",,,,,,,,,""
2020-04-02 15:05:57.624 EDT,,,45465,,5e863795.b199,4,,2020-04-02 15:05:57 EDT,,0,LOG,00000,"database system is shut down",,,,,,,,,""
--//修改时间线为6
[postgres@postgres ~]$ grep -v '^#' /home/postgres/pg11/recovery.conf
restore_command = 'cp /home/postgres/archivedir/%f %p'
recovery_target_timeline = '6'
[postgres@postgres ~]$
--//再次启动数据库
[postgres@postgres pg_wal]$ pg_ctl start
waiting for server to start....2020-04-02 15:08:01.052 EDT [45763] LOG: listening on IPv4 address "0.0.0.0", port 5442
2020-04-02 15:08:01.052 EDT [45763] LOG: listening on IPv6 address "::", port 5442
2020-04-02 15:08:01.054 EDT [45763] LOG: listening on Unix socket "/tmp/.s.PGSQL.5442"
2020-04-02 15:08:01.067 EDT [45763] LOG: redirecting log output to logging collector process
2020-04-02 15:08:01.067 EDT [45763] HINT: Future log output will appear in directory "on".
done
server started
[postgres@postgres pg_wal]$
--//详细日志
2020-04-02 15:08:01.108 EDT,,,45765,,5e863811.b2c5,3,,2020-04-02 15:08:01 EDT,,0,LOG,00000,"starting archive recovery",,,,,,,,,""
2020-04-02 15:08:01.111 EDT,,,45765,,5e863811.b2c5,4,,2020-04-02 15:08:01 EDT,,0,LOG,00000,"restored log file ""00000006.history"" from archive",,,,,,,,,""
2020-04-02 15:08:01.141 EDT,,,45765,,5e863811.b2c5,5,,2020-04-02 15:08:01 EDT,,0,LOG,00000,"restored log file ""000000060000000000000013"" from archive",,,,,,,,,""
2020-04-02 15:08:01.174 EDT,,,45765,,5e863811.b2c5,6,,2020-04-02 15:08:01 EDT,1/0,0,LOG,00000,"redo starts at 0/13000028",,,,,,,,,""
2020-04-02 15:08:01.175 EDT,,,45765,,5e863811.b2c5,7,,2020-04-02 15:08:01 EDT,1/0,0,LOG,00000,"consistent recovery state reached at 0/130000F8",,,,,,,,,""
2020-04-02 15:08:01.175 EDT,,,45763,,5e863811.b2c3,2,,2020-04-02 15:08:01 EDT,,0,LOG,00000,"database system is ready to accept read only connections",,,,,,,,,""
2020-04-02 15:08:01.198 EDT,,,45765,,5e863811.b2c5,8,,2020-04-02 15:08:01 EDT,1/0,0,LOG,00000,"restored log file ""000000060000000000000014"" from archive",,,,,,,,,""
2020-04-02 15:08:01.224 EDT,,,45765,,5e863811.b2c5,9,,2020-04-02 15:08:01 EDT,1/0,0,LOG,00000,"redo done at 0/14000060",,,,,,,,,""
2020-04-02 15:08:01.241 EDT,,,45765,,5e863811.b2c5,10,,2020-04-02 15:08:01 EDT,1/0,0,LOG,00000,"restored log file ""000000060000000000000014"" from archive",,,,,,,,,""
2020-04-02 15:08:01.277 EDT,,,45765,,5e863811.b2c5,11,,2020-04-02 15:08:01 EDT,1/0,0,LOG,00000,"restored log file ""00000007.history"" from archive",,,,,,,,,""
2020-04-02 15:08:01.280 EDT,,,45765,,5e863811.b2c5,12,,2020-04-02 15:08:01 EDT,1/0,0,LOG,00000,"restored log file ""00000008.history"" from archive",,,,,,,,,""
2020-04-02 15:08:01.281 EDT,,,45765,,5e863811.b2c5,13,,2020-04-02 15:08:01 EDT,1/0,0,LOG,00000,"selected new timeline ID: 9",,,,,,,,,""
2020-04-02 15:08:01.316 EDT,,,45765,,5e863811.b2c5,14,,2020-04-02 15:08:01 EDT,1/0,0,LOG,00000,"archive recovery complete",,,,,,,,,""
2020-04-02 15:08:01.318 EDT,,,45765,,5e863811.b2c5,15,,2020-04-02 15:08:01 EDT,1/0,0,LOG,00000,"restored log file ""00000006.history"" from archive",,,,,,,,,""
2020-04-02 15:08:01.423 EDT,,,45763,,5e863811.b2c3,3,,2020-04-02 15:08:01 EDT,,0,LOG,00000,"database system is ready to accept connections",,,,,,,,,""
--//检查tbl数据
postgres=# select * from tbl;
id | ival | description | created_time
----+------+-------------+-------------------------------
1 | 1 | | 2020-04-02 11:02:33.039506-04
34 | 2 | | 2020-04-02 13:52:24.368258-04
67 | 3 | | 2020-04-02 14:32:05.711384-04
68 | 4 | | 2020-04-02 14:40:01.597981-04
(4 rows)
postgres=#