深入浅出之时间线(一)



先看一下官方对时间线的解释:
https://www.postgresql.org/docs/9.6/static/continuous-archiving.html
提取其中一段:



´ PostgreSQL hasa notion of timelines. Whenever an archive recovery completes, a new timelineis created to identify the series of WAL records generated after that recovery.The timeline ID number is part of WAL segment file names so a new timeline doesnot overwrite the WAL data generated by previous timelines. It is in factpossible to (事实上可能会) archive manydifferent timelines. While that might seem like a useless feature, it‘s often alifesaver. Consider the situation where you aren’t quite sure whatpoint-in-time to recover to, and so have to do several point-in-time recoveriesby trial and error (反复试验) until youfind the best place to branch off from (分离) the old history. Without timelines this process wouldsoon generate an unmanageable mess. With timelines, you can recover to anyprior (先前的) state,including states in timeline branches that you abandoned earlier.



对recovery_target_timeline (string)参数的解释:



´ Specifiesrecovering into a particular timeline. The default is to recover along the sametimeline that was current when the base backup was taken. Setting this tolatest recovers to the latest timeline found in the archive, which is useful ina standby server. Other than that you only need to set this parameter incomplex re-recovery situations, where you need to return to a state that itselfwas reached after a point-in-time recovery. See Section 24.3.5 for discussion.



一、何为时间线(timeline)?
       为了区分因数据库恢复造成的不同时间段的wal日志而产生的“时间表”。(注意,它并不是一张表)
二、时间线的作用是什么?
       为了让数据库管理员在数据库出现未知灾难时候依据基础备份和wal日志(包含已经归档的)恢复到宕机前的任意一个状态。
三、何时会产生时间线?
       每进行一次即时恢复(PITR)就会产生一次时间线,每次备库切换成主库也会产生一次时间线。
四、如何指定recovery_target_timeline (string)?
       首先应该知道这个参数的使用位置,在通过pg_basebackup进行备份的时候添加参数“-R”就可以在备中自动产生recovery.conf文件,在这里就可以指定recovery_target_timeline=''和recovery_target_time=''参数来进行精准恢复了(还有其他的精准恢复方式,以后详谈),而这里的recovery_target_timeline参数可以直接填写时间线产生的“历史文件”名字用来指定恢复到哪一条时间线上去。举例如下:

[root@localhost archived]# ll
total 835636
-rw-------. 1 highgo highgo 16777216 Nov  7 15:15 000000010000000000000003
-rw-------. 1 highgo highgo 16777216 Nov  7 15:15 000000010000000000000004
-rw-------. 1 highgo highgo 16777216 Nov  7 15:15 000000010000000000000005
-rw-------. 1 highgo highgo      287 Nov  7 15:28 000000010000000000000005.00000028.backup
-rw-------. 1 highgo highgo 16777216 Nov  7 16:19 000000010000000000000006
-rw-------. 1 highgo highgo 16777216 Nov  7 15:28 000000010000000000000007
-rw-------. 1 highgo highgo 16777216 Nov  7 15:28 000000010000000000000008
-rw-------. 1 highgo highgo 16777216 Nov  7 16:19 000000020000000000000006
-rw-------. 1 highgo highgo 16777216 Nov  7 16:19 000000020000000000000007
-rw-------. 1 highgo highgo       50 Nov  7 16:19 00000002.history
-rw-------. 1 highgo highgo 16777216 Nov  7 17:15 000000030000000000000007
-rw-------. 1 highgo highgo 16777216 Nov  7 17:19 000000030000000000000008.partial
-rw-------. 1 highgo highgo       94 Nov  7 16:58 00000003.history
-rw-------. 1 highgo highgo 16777216 Nov  7 17:30 000000040000000000000008
-rw-------. 1 highgo highgo 16777216 Nov  7 17:34 000000040000000000000009.partial
-rw-------. 1 highgo highgo      138 Nov  7 17:19 00000004.history

00000002.history、00000003.history、00000004.history即为时间线产生的“历史文件”名字。

[highgo@localhost data]$ cat recovery.conf
standby_mode = 'off'
primary_conninfo = 'user=highgo password=highgo host=192.168.48.142 port=5866 sslmode=prefer sslcompression=1'
archive_directory='/backup/archived'
recovery_target_timeline='00000002.history'
recovery_target_time='2017-11-07 16:16:54'

       开启数据库之后通过读取归档日志就会将数据库恢复到“2017-11-07 16:16:54”这个时间点。
到这里你可能会有一个小问题,我想要恢复到00000002.history之前的时间线应该如何指定呢?其实官方文档已经给出了说明了,缺省情况下即为恢复到基础备份所在的时间线。
恢复文件中不填写recovery_target_timeline参数就相当于“00000001.history”了。

五、为何有时候会恢复失败?
 1、确认基础备份的时间属于哪条时间线。
 2、确认归档或未归档wal日志是否存在。
 3、确认recovery_target_time 指定的时间是正确的。




BY 海无涯




你可能感兴趣的:(Highgo,DB,PostgreSQL)