磨砺技术珠矶,践行数据之道,追求卓越价值

回到上一级页面: PostgreSQL基础知识与基本操作索引页     回到顶级页面:PostgreSQL索引页

看PostgreSQL中与PITR相关的设定:

wal_level=archive

# - Archiving -
archive_mode = on # allows archiving to be done
# (change requires restart)
archive_command = 'cp %p "/usr/local/pgsql/arch/%f"' # command to use to archive a logfile segment
#archive_timeout = 0 # force a logfile segment switch after this
# number of seconds; 0 disables

开始,在运行之前,看xlog状况:

[postgres@pg201 pgsql]$ ls -lrt ./data/pg_xlog
total
-rw------- postgres postgres Aug :
drwx------ postgres postgres Aug : archive_status
-rw------- postgres postgres Aug :
[postgres@pg201 pgsql]$ [postgres@pg201 pgsql]$ ls -lrt ./arch
total
-rw------- postgres postgres Aug :
[postgres@pg201 pgsql]$

为文件拷贝进行准备:

[postgres@pg201 pgsql]$ ./bin/psql
psql (9.1.2)
Type "help" for help. postgres=# select pg_start_backup('gao');
pg_start_backup
-----------------
0/3000020
(1 row) postgres=#

此时,xlog和archive log状况如下:

[postgres@pg201 pgsql]$
[postgres@pg201 pgsql]$ ls -lrt ./data/pg_xlog
total 49216
-rw------- 1 postgres postgres 16777216 Aug 6 14:16 000000010000000000000004
-rw------- 1 postgres postgres 16777216 Aug 6 14:17 000000010000000000000002
drwx------ 2 postgres postgres 4096 Aug 6 14:17 archive_status
-rw------- 1 postgres postgres 16777216 Aug 6 14:17 000000010000000000000003 [postgres@pg201 pgsql]$ ls -lrt ./arch
total 32808
-rw------- 1 postgres postgres 16777216 Aug 6 14:16 000000010000000000000001
-rw------- 1 postgres postgres 16777216 Aug 6 14:17 000000010000000000000002
[postgres@pg201 pgsql]$

完成文件拷贝:

[postgres@pg201 pgsql]$ tar -cvf basedata.tar ./data   

结束基础备份:

postgres=# select pg_stop_backup();
NOTICE: pg_stop_backup complete, all required WAL segments have been archived
pg_stop_backup
----------------
0/30000A0
(1 row) postgres=#

再次查看log:

[postgres@pg201 pgsql]$ ls -lrt ./data/pg_xlog
total 49220
-rw------- 1 postgres postgres 16777216 Aug 6 14:16 000000010000000000000004
-rw------- 1 postgres postgres 16777216 Aug 6 14:17 000000010000000000000002
drwx------ 2 postgres postgres 4096 Aug 6 14:20 archive_status
-rw------- 1 postgres postgres 267 Aug 6 14:20 000000010000000000000003.00000020.backup
-rw------- 1 postgres postgres 16777216 Aug 6 14:20 000000010000000000000003
[postgres@pg201 pgsql]$ [postgres@pg201 pgsql]$ ls -lrt ./arch
total 49216
-rw------- 1 postgres postgres 16777216 Aug 6 14:16 000000010000000000000001
-rw------- 1 postgres postgres 16777216 Aug 6 14:17 000000010000000000000002
-rw------- 1 postgres postgres 267 Aug 6 14:20 000000010000000000000003.00000020.backup
-rw------- 1 postgres postgres 16777216 Aug 6 14:20 000000010000000000000003
[postgres@pg201 pgsql]$

可以看到产生了一个backup文件,看内容:

[postgres@pg201 pgsql]$ cat ./data/pg_xlog/000000010000000000000003.00000020.backup
START WAL LOCATION: 0/3000020 (file 000000010000000000000003)
STOP WAL LOCATION: 0/30000A0 (file 000000010000000000000003)
CHECKPOINT LOCATION: 0/3000020
BACKUP METHOD: pg_start_backup
START TIME: 2013-08-06 14:17:53 CST
LABEL: gao
STOP TIME: 2013-08-06 14:20:53 CST
[postgres@pg201 pgsql]$

强制日志切换,模拟大量数据产生导致日志switch:

postgres=# select pg_switch_xlog();
pg_switch_xlog
----------------
0/4000000
(1 row) postgres=#

log发生了变化:

[postgres@pg201 pgsql]$ ls -lrt ./data/pg_xlog
total 49220
-rw------- 1 postgres postgres 16777216 Aug 6 14:20 000000010000000000000006
-rw------- 1 postgres postgres 267 Aug 6 14:20 000000010000000000000003.00000020.backup
-rw------- 1 postgres postgres 16777216 Aug 6 14:25 000000010000000000000004
drwx------ 2 postgres postgres 4096 Aug 6 14:27 archive_status
-rw------- 1 postgres postgres 16777216 Aug 6 14:27 000000010000000000000005
[postgres@pg201 pgsql]$
[postgres@pg201 pgsql]$ ls -lrt ./arch
total 65620
-rw------- 1 postgres postgres 16777216 Aug 6 14:16 000000010000000000000001
-rw------- 1 postgres postgres 16777216 Aug 6 14:17 000000010000000000000002
-rw------- 1 postgres postgres 267 Aug 6 14:20 000000010000000000000003.00000020.backup
-rw------- 1 postgres postgres 16777216 Aug 6 14:20 000000010000000000000003
-rw------- 1 postgres postgres 16777216 Aug 6 14:25 000000010000000000000004
[postgres@pg201 pgsql]$

再作几次log switch,最后是这样:

[postgres@pg201 pgsql]$ ls -lrt ./data/pg_xlog
total 49220
-rw------- 1 postgres postgres 16777216 Aug 6 14:20 000000010000000000000006
-rw------- 1 postgres postgres 267 Aug 6 14:20 000000010000000000000003.00000020.backup
-rw------- 1 postgres postgres 16777216 Aug 6 14:25 000000010000000000000004
drwx------ 2 postgres postgres 4096 Aug 6 14:27 archive_status
-rw------- 1 postgres postgres 16777216 Aug 6 14:27 000000010000000000000005
[postgres@pg201 pgsql]$
[postgres@pg201 pgsql]$ ls -lrt ./arch
total 65620
-rw------- 1 postgres postgres 16777216 Aug 6 14:16 000000010000000000000001
-rw------- 1 postgres postgres 16777216 Aug 6 14:17 000000010000000000000002
-rw------- 1 postgres postgres 267 Aug 6 14:20 000000010000000000000003.00000020.backup
-rw------- 1 postgres postgres 16777216 Aug 6 14:20 000000010000000000000003
-rw------- 1 postgres postgres 16777216 Aug 6 14:25 000000010000000000000004
[postgres@pg201 pgsql]$

经过一次正常关机后,日志再次变化:

[postgres@pg201 pgsql]$ ./bin/pg_ctl -D ./data stop                    

日志变为:

[postgres@pg201 pgsql]$ ls -lrt ./data/pg_xlog
total 49220
-rw------- 1 postgres postgres 267 Aug 6 14:20 000000010000000000000003.00000020.backup
-rw------- 1 postgres postgres 16777216 Aug 6 14:25 000000010000000000000007
drwx------ 2 postgres postgres 4096 Aug 6 14:39 archive_status
-rw------- 1 postgres postgres 16777216 Aug 6 14:39 000000010000000000000006
-rw------- 1 postgres postgres 16777216 Aug 6 14:39 000000010000000000000005
[postgres@pg201 pgsql]$
[postgres@pg201 pgsql]$ ls -lrt ./arch
total 82024
-rw------- 1 postgres postgres 16777216 Aug 6 14:16 000000010000000000000001
-rw------- 1 postgres postgres 16777216 Aug 6 14:17 000000010000000000000002
-rw------- 1 postgres postgres 267 Aug 6 14:20 000000010000000000000003.00000020.backup
-rw------- 1 postgres postgres 16777216 Aug 6 14:20 000000010000000000000003
-rw------- 1 postgres postgres 16777216 Aug 6 14:25 000000010000000000000004
-rw------- 1 postgres postgres 16777216 Aug 6 14:39 000000010000000000000005
[postgres@pg201 pgsql]$

重新启动PostgreSQL,添加数据,然后杀掉进程,模拟崩溃:

[postgres@pg201 pgsql]$ ./bin/psql
psql (9.1.2)
Type "help" for help. postgres=# create table test(id integer);
CREATE TABLE
postgres=# insert into test values(1);
INSERT 0 1
postgres=# \q
[postgres@pg201 pgsql]$
[root@pg201 ~]# ps -ef | grep post
root 3120 3049 0 14:08 pts/1 00:00:00 su - postgres
postgres 3121 3120 0 14:08 pts/1 00:00:00 -bash
root 3216 3191 0 14:15 pts/2 00:00:00 su - postgres
postgres 3217 3216 0 14:15 pts/2 00:00:00 -bash
root 3299 3279 0 14:17 pts/3 00:00:00 su - postgres
postgres 3300 3299 0 14:17 pts/3 00:00:00 -bash
postgres 3516 1 0 15:00 pts/2 00:00:00 /usr/local/pgsql/bin/postgres -D ./data
postgres 3518 3516 0 15:00 ? 00:00:00 postgres: writer process
postgres 3519 3516 0 15:00 ? 00:00:00 postgres: wal writer process
postgres 3520 3516 0 15:00 ? 00:00:00 postgres: autovacuum launcher process
postgres 3521 3516 0 15:00 ? 00:00:00 postgres: archiver process
postgres 3522 3516 0 15:00 ? 00:00:00 postgres: stats collector process
root 3559 3535 0 15:01 pts/4 00:00:00 grep post
[root@pg201 ~]# kill -s SIGQUIT 3516
[root@pg201 ~]#

下面开始回复动作、找出基础备份:

[postgres@pg201 pgsql]$ tar -xvf basedata.tar ./data

其pg_xlog目录下,还是旧的online redo log:

[postgres@pg201 pg_xlog]$ ls -lrt
total 49216
-rw------- 1 postgres postgres 16777216 Aug 6 14:16 000000010000000000000004
-rw------- 1 postgres postgres 16777216 Aug 6 14:17 000000010000000000000002
drwx------ 2 postgres postgres 4096 Aug 6 14:17 archive_status
-rw------- 1 postgres postgres 16777216 Aug 6 14:17 000000010000000000000003
[postgres@pg201 pg_xlog]$

把它清空后,不补足online redolog,仅仅用 archive log的效果:

在解压好的data目录下,建立 recovery.conf文件:

[postgres@pg201 data]$ cat recovery.conf
restore_command = 'cp /usr/local/pgsql/arch/%f %p'
[postgres@pg201 data]$

启动PostgreSQL:

[postgres@pg201 pgsql]$ ./bin/pg_ctl -D ./data status
pg_ctl: no server running
[postgres@pg201 pgsql]$ ./bin/pg_ctl -D ./data start
pg_ctl: another server might be running; trying to start server anyway
server starting
[postgres@pg201 pgsql]$ LOG: database system was interrupted; last known up at 2013-08-06 14:17:53 CST
LOG: creating missing WAL directory "pg_xlog/archive_status"
LOG: starting archive recovery
LOG: restored log file "000000010000000000000003" from archive
LOG: redo starts at 0/3000078
LOG: consistent recovery state reached at 0/4000000
LOG: restored log file "000000010000000000000004" from archive
LOG: restored log file "000000010000000000000005" from archive
cp: cannot stat `/usr/local/pgsql/arch/000000010000000000000006': No Such File or Directory
LOG: could not open file "pg_xlog/000000010000000000000006" (log file 0, segment 6): No Such File or Directory
LOG: redo done at 0/5000078 LOG: restored log file "000000010000000000000005" from archive cp: cannot stat `/usr/local/pgsql/arch/00000002.history': No Such File or Directory
LOG: selected new timeline ID:  cp: cannot stat `/usr/local/pgsql/arch/00000001.history':No Such File or Directory
LOG: archive recovery complete 
LOG: autovacuum launcher started
LOG: database system is ready to accept connections

因为我的创建table test 的动作虽然计入了 redo log中,但是由于突然的崩溃,没有来得及放入archive 日志中,所以,没有得到恢复:

[postgres@pg201 pgsql]$ ./bin/psql
psql (9.1.2)
Type "help" for help. postgres=# select * from test;
ERROR: relation "test" does not exist
LINE 1: select * from test;
^
postgres=#

此时,看Log,就可以清晰地看到TimeLine的痕迹了:

[postgres@pg201 pgsql]$ ls -lrt ./data/pg_xlog
total 49220
-rw------- 1 postgres postgres 56 Aug 6 15:11 00000002.history
-rw------- 1 postgres postgres 16777216 Aug 6 15:11 00000000000000000000005
-rw------- 1 postgres postgres 16777216 Aug 6 15:17 00000000000000000000006
drwx------ 2 postgres postgres 4096 Aug 6 15:17 archive_status
-rw------- 1 postgres postgres 16777216 Aug 6 15:17 00000000000000000000007
[postgres@pg201 pgsql]$
[postgres@pg201 pgsql]$ ls -lrt ./arch
total 114836
-rw------- 1 postgres postgres 16777216 Aug 6 14:16 000000010000000000000001
-rw------- 1 postgres postgres 16777216 Aug 6 14:17 000000010000000000000002
-rw------- 1 postgres postgres 267 Aug 6 14:20 000000010000000000000003.00000020.backup
-rw------- 1 postgres postgres 16777216 Aug 6 14:20 000000010000000000000003
-rw------- 1 postgres postgres 16777216 Aug 6 14:25 000000010000000000000004
-rw------- 1 postgres postgres 16777216 Aug 6 14:39 000000010000000000000005
-rw------- 1 postgres postgres 56 Aug 6 15:11 0000000.history
-rw------- 1 postgres postgres 16777216 Aug 6 15:17 00000000000000000000006
-rw------- 1 postgres postgres 16777216 Aug 6 15:17 00000000000000000000005
[postgres@pg201 pgsql]$ cat ./arch/00000002.history
1 000000010000000000000005 no recovery target specified
[postgres@pg201 pgsql]$

这次再看清空pg_xlog目录后,补足online redolog+archive log的效果:

还要把 pg_xlog/archive_status 目录清空:

[postgres@pg201 pgsql]$ cp ./data.bak/pg_xlog/*  ./data/pg_xlog/
... [postgres@pg201 pg_xlog]$ ls -lrt
total 98432
-rw------- 1 postgres postgres 16777216 Aug 6 14:16 000000010000000000000004
-rw------- 1 postgres postgres 16777216 Aug 6 14:17 000000010000000000000002
-rw------- 1 postgres postgres 16777216 Aug 6 14:17 000000010000000000000003
-rw------- 1 postgres postgres 267 Aug 6 15:41 000000010000000000000003.00000020.backup
-rw------- 1 postgres postgres 16777216 Aug 6 15:41 000000010000000000000007
-rw------- 1 postgres postgres 16777216 Aug 6 15:41 000000010000000000000006
-rw------- 1 postgres postgres 16777216 Aug 6 15:41 000000010000000000000005
drwx------ 2 postgres postgres 4096 Aug 6 15:44 archive_status
[postgres@pg201 pg_xlog]$

此时,启动 PostgreSQL:

[postgres@pg201 pgsql]$ ./bin/pg_ctl -D ./data start
pg_ctl: another server might be running; trying to start server anyway
server starting
[postgres@pg201 pgsql]$ LOG: database system was interrupted; last known up at 2013-08-06 14:17:53 CST
LOG: starting archive recovery
LOG: restored log file "000000010000000000000003" from archive
LOG: redo starts at 0/3000078
LOG: consistent recovery state reached at 0/4000000
LOG: restored log file "000000010000000000000004" from archive
LOG: restored log file "000000010000000000000005" from archive
cp: cannot stat `/usr/local/pgsql/arch/000000010000000000000006': No such file or directory
LOG: record with zero length at 0/6011770
LOG: redo done at 0/6011730
LOG: last completed transaction was at log time 2013-08-06 15:01:17.757945+08
LOG: restored log file "00000002.history" from archive
cp: cannot stat `/usr/local/pgsql/arch/00000003.history': No such file or directory
LOG: selected new timeline ID: 3
cp: cannot stat `/usr/local/pgsql/arch/00000001.history': No such file or directory
LOG: archive recovery complete
LOG: autovacuum launcher started
LOG: database system is ready to accept connections

看数据:

[postgres@pg201 pgsql]$ ./bin/psql
psql (9.1.2)
Type "help" for help. postgres=# select * from test;
id
----
1
(1 row) postgres=#

回到上一级页面: PostgreSQL基础知识与基本操作索引页     回到顶级页面:PostgreSQL索引页

磨砺技术珠矶,践行数据之道,追求卓越价值

PostgreSQL PITR实验的更多相关文章

  1. PostgreSQL基础知识与基本操作索引页

    磨砺技术珠矶,践行数据之道,追求卓越价值 返回顶级页:PostgreSQL索引页 luckyjackgao@gmail.com 本页记录所有本人所写的PostgreSQL的基础知识和基本操作相关文摘和 ...

  2. PostgreSQL的PITR中,对 unfilled wal log 如何处理为好

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL基础知识与基本操作索引页     回到顶级页面:PostgreSQL索引页 通过实验,可以发现,PostgreSQL中使 ...

  3. PostgreSQL LIKE 查询效率提升实验<转>

    一.未做索引的查询效率 作为对比,先对未索引的查询做测试 EXPLAIN ANALYZE select * from gallery_map where author = '曹志耘'; QUERY P ...

  4. PostgreSQL数据库表名的大小写实验

    磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面:PostgreSQL基础知识与基本操作索引页    回到顶级页面:PostgreSQL索引页[作者 高健@博客园  luckyjackgao@g ...

  5. postgresql基于备份点PITR恢复

    实验目的: 01.基于备份点直接恢复数据库 02.基于备份点后续增量wal日志恢复到特定的时间点 实验环境: centos7 postgresql9.5 01.安装postgresql9.5 post ...

  6. PostgreSQL的 PITR实战---运用 recovery_target_time

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL基础知识与基本操作索引页     回到顶级页面:PostgreSQL索引页 看了很多的例子,没有发现具体讲 recove ...

  7. PostgreSQL的xlog实验一

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL基础知识与基本操作索引页     回到顶级页面:PostgreSQL索引页 一,从initdb开始,initdb执行结束 ...

  8. PostgreSQL设置事务隔离级别实验

    apple=# begin; BEGIN apple=# set transaction ISOLATION LEVEL read committed ; SET apple=# select * f ...

  9. 对PostgreSQL数据库的hstore类型建立GisT索引的实验

    磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面:PostgreSQL基础知识与基本操作索引页    回到顶级页面:PostgreSQL索引页[作者 高健@博客园  luckyjackgao@g ...

随机推荐

  1. java笔记--关于多线程如何查看JVM中运行的线程

    查看JVM中的线程 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3890280.html "谢谢-- ThreadGrou ...

  2. ASP.NET MVC实现通用设置

    网站中的设置实现方式有好几种,其中有将设置类序列化然后保存到文件中(例如使用XML序列化然后以XML形式保存在文件中),或者将设置信息保存到数据库中. 保存到数据库中的方式就是将设置的项作为key,设 ...

  3. spring配置datasource

    1.使用org.springframework.jdbc.datasource.DriverManagerDataSource  说明:DriverManagerDataSource建立连接是只要有连 ...

  4. UNIX日期与时间

    日期和时间 UINX系统内部有一个变量记录自开机以来经过的时间.从用户的角度,UNIX时间函数分为3类: 度量进程已使用CPU时间的函数: 给出绝对时间或日历时间的函数: 设置闹钟.定时器以及睡眠的函 ...

  5. virtual memory exhausted: Cannot allocate memory 解决方法

    在阿里云买了个云服务器,内存1G.编译kudu时出现下面的错误: virtual memory exhausted: Cannot allocate memory 问题原因:由于物理内存本身很小,且阿 ...

  6. python3: 字符串和文本

    1. 分割字符串-使用多个界定符[re.split()] >>> line = 'asdf fjdk; afed, fjek,asdf, foo' >>> impo ...

  7. 对于开发WEB方面项目需要的工具和技术了解

    1.IDE:Webstorm,JavaScript 开发工具. 2.版本管理系统:Git,独一无二. 3.单元测试:jsamine,前后端共用.Jasmine是我们梦寐以求的Javascript测试框 ...

  8. P3565 [POI2014]HOT-Hotels

    题目描述 There are nn towns in Byteotia, connected with only n-1n−1 roads. Each road directly links two ...

  9. mongodb的学习-3-在Mac上的安装配置

    1.使用homebrew安装: brew install mongodb 查看安装好的版本: mongo --version MongoDB shell version v3.6.4 git vers ...

  10. pm2踩过的坑

    pm2实现一键部署,能将github上的代码拉到服务器,但是死活就是起不了服务. pm2部署命令: pm2 deploy ecosystem.json production setup pm2 dep ...