首页 技术 正文
技术 2022年11月12日
0 收藏 927 点赞 3,432 浏览 2891 个字

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

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

看了很多的例子,没有发现具体讲 recovery_target_time的例子,于是自己作一个吧

在开始之前,先把postgresql.conf的配置设置好:

wal_level = archive             # minimal, archive, or hot_standby
# (change requires restart) # - 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 = # force a logfile segment switch after this
# number of seconds; disables

然后启动PostgreSQL:

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

在开始备份前,先作一条数据:此时,时间大约是13:40之前:

postgres=# create table test(id integer);
CREATE TABLE
postgres=# insert into test values(100);
INSERT 0 1

然后开始进行基础备份:

postgres=# select pg_start_backup('gao');
pg_start_backup
-----------------
0/2000020
(1 row)

tar命令打包:

tar -cvf ./base.tar ./data

结束基础备份:

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

此时大约是13:40左右,再插入一条数据:

postgres=# insert into test values(200);
INSERT 0 1

等待,到13:50左右,再插入一条数据:

postgres=# insert into test values(300);
INSERT 0 1

为了方便操作,进行一次强制的日志切换:

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

现在,模拟崩溃,强制杀掉进程:

[root@pg201 ~]# kill -s SIGQUIT Postmaster进程号                        

然后,原有data目录改名保存,开始恢复过程:

[postgres@pg201 pgsql]$ mv ./data ./data.bak
[postgres@pg201 pgsql]$ tar -xvf base.tar ./data                    

把崩溃时后的online wal log 也准备好:

[postgres@pg201 pgsql]$ rm -rf ./data/pg_xlog
[postgres@pg201 pgsql]$ cp -r ./data.bak/pg_xlog/ ./data
[postgres@pg201 pgsql]$ cd ./data/pg_xlog
[postgres@pg201 pg_xlog]$ [postgres@pg201 archive_status]$ pwd
/usr/local/pgsql/data/pg_xlog/archive_status
[postgres@pg201 archive_status]$ rm -f *
[postgres@pg201 archive_status]$

作一个 recovery.conf文件,指明要恢复到13:45,也就是200数据已经出现,300数据尚未插入的时候

[postgres@pg201 data]$ pwd
/usr/local/pgsql/data [postgres@pg201 data]$ cat recovery.conf
restore_command = 'cp /usr/local/pgsql/arch/%f %p' recovery_target_time = '2013-08-07 13:45:00+08' [postgres@pg201 data]$

再次启动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 -- :: CST
LOG: starting point-in-time recovery to -- ::+
LOG: restored log file "" from archive
LOG: redo starts at /
LOG: consistent recovery state reached at /
LOG: restored log file "" from archive
LOG: recovery stopping before commit of transaction , time -- ::05.407695+
LOG: redo done at /
LOG: last completed transaction was at log time -- ::44.338862+
cp: cannot stat `/usr/local/pgsql/arch/.history': No such file or directory
LOG: selected new timeline ID:
cp: cannot stat `/usr/local/pgsql/arch/.history': No such file or directory
LOG: archive recovery complete
LOG: autovacuum launcher started
LOG: database system is ready to accept connections

验证:

[postgres@pg201 ~]$ cd /usr/local/pgsql
[postgres@pg201 pgsql]$ ./bin/psql
psql (9.1.2)
Type "help" for help.postgres=# select * from test;
id
-----
100
200
(2 rows)postgres=# \q

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

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,121
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,593
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,438
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,209
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,845
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,930