PG中文社区 /

如何打造 PostgreSQL 闪回环境?

原作者:乐途  创作时间:2019-01-16 02:20:58+08
redraiment 发布于2019-01-16 02:20:58           评论: 3   浏览: 8293   顶: 779  踩: 828 

作者介绍

Lottu(乐途),目前就职于深圳宜搜科技有限公司,担任数据库DBA,主要承PostgreSQL、Oracle数据库维护工作以及数据库去O工作。

引言

在 PostgreSQL 数据库;目前是不支持类似 Oracle 闪回特性;我们知道 Oracle 闪回特性在数据恢复是非常简单给力。增加数据库操作人员(开发,DBA)的容错率。Oracle 闪回特性使用场景:

  • flashback database:数据库闪回;多用于数据库恢复;数据库,用户,表空间误删。
  • flashback table:表闪回;用于数据表恢复;数据表误删。
  • flashback query:闪回查询;应用于修复误操作数据。

对于 PostgreSQL 闪回;德哥有写过一系列的文章PostgreSQL flashback(闪回)功能实现与介绍;如今我取巧实现 PostgreSQL 闪回特性。原理参考我之前的文章PostgreSQL恢复误删数据。

  • 支持在线数据库恢复(不推荐),多用于数据表恢复,修复误操作数据。
  • 支持多次闪回操作;在 flashback query 中恢复的时间点不是很理想;可以重新设定时间点再闪回操作一次。

一. flashback 所需命令

实现 flashback 就是一个有效的数据库备份/恢复。

  • 备份操作命令有 scp/cp, pg_basebackup, pg_rman。我选 pg_basebackup。因为pg_basebackup 支持异构备份。意味着可以在同一台服务器在 cp 一个 PostgreSQL 实例。即可以在一台服务器上实现闪回。
  • 恢复操作有 copy 或者 pg_dump 命令。

二. flashback 所需条件

实现 flashback 可以是在线服务器上操作,也可以是在另一台服务器上操作。换句话说:可以是一台服务器或者是两台服务器来实现。在一台服务器上需要更改数据库端口。由于本环境有清理目录操作;避免在在线服务器上误删目录。所以建议用两条服务器来实现。

三. flashback 搭建步骤

本文档采用两台服务器来实现。两台服务器时间校准,免密码登录。

  • 正式数据库环境:192.168.1.201
  • 闪回数据库环境:192.168.1.202

在闪回数据库192.168.1.202上操作:

3.1 创建闪回用户和配置环境变量

这里用postgres用户作为闪回用户。并加载正式环境192.168.1.201的postgres用户的环境变量;以及配置免密码登录。

#创建用户
useradd postgres
passwd postgres

# 配置免密码登录
N/A

# 配置环境变量
N/A

# 创建闪回目录
mkdir -p /data/flash_back
chown -R postgres:postgres /data/flash_back

3.2 安装pg_flashback脚本

下载地址:

[postgres@Postgres202 ~]$  tar -zxvf pg_flashback.tar.gz 
[postgres@Postgres202 ~]$  cd pg_flashback/
[postgres@Postgres202 pg_flashback]$ ./install.sh 192.168.1.201

执行 “./install.sh” 后面的参数 “192.168.1.201”是目标数据库IP地址。 注意:生成recovery.conf;在/home/postgres/recovery.conf;样板如下:

restore_command = 'scp postgres@192.168.1.201:/data/arch/%f %p'
recovery_target_time = '2019-01-11 06:58:34'

3.3 初始化pg_flashback脚本

安装完成之后在bin目录下;会生成下列文件

[postgres@Postgres202 pg_flashback]$ cd 
[postgres@Postgres202 ~]$ cd bin
[postgres@Postgres202 bin]$ ll
total 20
-rwxrwxr-x. 1 postgres postgres 203 Jan 11 05:00 backup.sh
-rwxrwxr-x. 1 postgres postgres 109 Jan 11 05:00 cleandir.sh
-rwxrwxr-x. 1 postgres postgres 101 Jan 11 05:00 init.sh
-rwxrwxr-x. 1 postgres postgres 924 Jan 11 05:00 pg_flashback.sh
-rwxrwxr-x. 1 postgres postgres 213 Jan 11 05:00 rebackup.sh

初始化操作

[postgres@Postgres202 bin]$ export PATH=$PATH:$HOME/bin
[postgres@Postgres202 bin]$ init.sh
341688/341688 kB (100%), 4/4 tablespaces
NOTICE:  pg_stop_backup complete, all required WAL segments have been archived

加入定时任务 crontab 中 例如:周末凌晨过5分执行初始化一次;即最早能闪回到本周凌晨5分时刻

5 0 * * 0 init.sh

其中pg_flashback.sh 脚本用于数据库误操作实现闪回到某个时间点。支持多次闪回。执行闪回 之前;需要配置 recovery.conf 中的 “recovery_target_time” 时间点。即恢复到哪个时间点。

第一次执行,执行pg_flashback.sh 1;往后执行闪回,通称再次执行,执行命令pg_flashback.sh 2

四. flashback 测试验证

4.1 验证目的

  • 验证是否可以闪回/恢复
  • 验证是否可以多次操作闪回
  • 验证在闪回的过程中;正式环境是否正常对外服务

4.2 实验操作

4.2.1 模拟事故

正式数据库

-- 创建表t_lottu 并插入两条记录
lottu=# create table t_lottu(id int,col varchar(20));
CREATE TABLE
lottu=# insert into t_lottu values(1,'ABC');
INSERT 0 1
lottu=# insert into t_lottu values(2,'DEF');
INSERT 0 1
lottu=# select * from t_lottu;
 id | col 
----+-----
  1 | ABC
  2 | DEF
(2 rows)

--获取时间点;这个时间点“2019-01-11 06:57:13”

lottu=# select now();
              now              
-------------------------------
 2019-01-11 06:57:13.342117+08
(1 row)

--删除id=1的记录

lottu=# delete from t_lottu where id = 1;
DELETE 1
lottu=# insert into t_lottu values(3,'ABC');
INSERT 0 1
lottu=# select * from t_lottu;
 id | col 
----+-----
  2 | DEF
  3 | ABC
(2 rows)

lottu=# select now();
             now              
------------------------------
 2019-01-11 06:58:34.49212+08
(1 row)

-- 误删除表t_lottu
lottu=# drop table t_lottu;
DROP TABLE

由于表t_lottu误删了;需要恢复到时间点“2019-01-11 06:58:34”。表中的记录是2,3

4.2.2 恢复操作

在闪回数据库,我们先在配置文件 recovery.conf 中的参数“recovery_target_time” 设置为 “2019-01-11 06:57:13”。执行闪回pg_flashback.sh 1

[postgres@Postgres202 ~]$ ll recovery.conf 
-rw-rw-r--. 1 postgres postgres 109 Jan 11 05:00 recovery.conf
[postgres@Postgres202 ~]$ vi recovery.conf 

[postgres@Postgres202 ~]$ pg_flashback.sh 1
Info: backup backupset
Info: edit recovery.conf
Info: begin start database
server starting
Info: start database succes
LOG:  00000: redirecting log output to logging collector process
HINT:  Future log output will appear in directory "pg_log".
LOCATION:  SysLogger_Start, syslogger.c:622
 pg_xlog_replay_resume 
-----------------------

(1 row)
[postgres@Postgres202 ~]$ psql lottu lottu
psql (9.6.0)
Type "help" for help.

lottu=# select * from t_lottu;
 id | col 
----+-----
  1 | ABC
  2 | DEF

闪回数据库成功启动;登录查看表lottu情况:表数据记录是1,2 这不是我所需闪回的目标!在配置文件 recovery.conf 中的参数“recoverytargettime” 设置为 “2019-01-11 06:58:34”。再执行闪回pg_flashback.sh 2

[postgres@Postgres202 ~]$ pg_stop
waiting for server to shut down....... done
server stopped
[postgres@Postgres202 ~]$ vi recovery.conf 

[postgres@Postgres202 ~]$ pg_flashback.sh 2
Info: edit recovery.conf
Info: begin start database
server starting
Info: start database succes
LOG:  00000: redirecting log output to logging collector process
HINT:  Future log output will appear in directory "pg_log".
LOCATION:  SysLogger_Start, syslogger.c:622
 pg_xlog_replay_resume 
-----------------------

(1 row)

[postgres@Postgres202 ~]$ psql lottu lottu
psql (9.6.0)
Type "help" for help.

lottu=# select * from t_lottu;
 id | col 
----+-----
  2 | DEF
  3 | ABC
(2 rows)

数据成功找回;在使用pg_dump进行数据导出;再到正式数据库执行即可。

4.3 实验结论

  • 可以闪回
  • 可以多次操作
  • 不影响正式环境

五. 其他实现方式参考

  • PostgreSQL flashback(闪回) 功能实现与介绍

六. 不足

不推荐使用这种方式恢复数据库。多用于ddl/dml误操作恢复。

七. 代码下载

pg_flashback安装包下载地址链接:https://pan.baidu.com/s/1-dm58lMno5X_P7krCQneXg 提取码:ofrj

CENTER_PostgreSQL_Community


评论:3   浏览: 8293                   顶: 779  踩: 828 

请在登录后发表评论,否则无法保存。

1# __ xcvxcvsdf 回答于 2024-11-13 14:04:14+08
https://zulin.tiancebbs.cn/sh/1007.html https://aihuishou.tiancebbs.cn/sh/3000.html https://www.tiancebbs.cn/ershoufang/472958.html https://suixi.tiancebbs.cn/qths/457222.html https://taicang.tiancebbs.cn/hjzl/462553.html https://www.tiancebbs.cn/ershoufang/468355.html https://aihuishou.tiancebbs.cn/sh/3513.html https://www.tiancebbs.cn/ershoufang/467353.html https://www.tiancebbs.cn/ershoufang/472855.html https://zulin.tiancebbs.cn/sh/3672.html https://hpqsh.tiancebbs.cn/qihuotouzi/55013.html https://aihuishou.tiancebbs.cn/sh/257.html https://zulin.tiancebbs.cn/sh/1013.html https://su.tiancebbs.cn/hjzl/464939.html https://aihuishou.tiancebbs.cn/sh/2280.html https://zulin.tiancebbs.cn/sh/2678.html https://bc.tiancebbs.cn/qths/465802.html

2# __ xcvxcvsdf 回答于 2024-10-25 10:22:08+08
http://yuanbang.tjtcbmw.cn/baicheng/ https://fanhegang.tiancebbs.cn/ http://wogao.ahtcbmw.cn/benxi/ http://yz.cqtcxxw.cn/qinghai/ http://taiying.njtcbmw.cn/dgzp/ https://longgang.tiancebbs.cn/ http://jingren.hftcbmw.cn/zaozhuang/ https://pgqchengxi.tiancebbs.cn/ http://gx.lztcxxw.cn/ahsz/ http://shimai.zjtcbmw.cn/zhangzhou/ http://shengshun.njtcbmw.cn/lvliang/ http://ly.shtcxxw.cn/yu-lin/ https://yangganzhen.tiancebbs.cn/ http://jinqiang.ahtcbmw.cn/nanchang/ https://cwqqita.tiancebbs.cn/ http://fuyang.tjtcbmw.cn/twyl/ http://xinguang.sctcbmw.cn/yichun/

3# __ xiaowu 回答于 2024-04-23 14:45:53+08
拉风的游戏名字:https://www.nanss.com/mingcheng/4992.html 温柔到爆的神仙文案:https://www.nanss.com/wenan/4518.html 村干部管理办法:https://www.nanss.com/gongzuo/4754.html 发宝宝照片配个性说说:https://www.nanss.com/wenan/4465.html 还有多少个十年:https://www.nanss.com/wenan/4547.html 神奇的世界:https://www.nanss.com/xuexi/4668.html 路由器有什么用:https://www.nanss.com/wenti/4735.html 马尔代夫属于哪个国家:https://www.nanss.com/wenti/4533.html 时尚昵称:https://www.nanss.com/mingcheng/4512.html 魔兽世界公会名字:https://www.nanss.com/mingcheng/4967.html 关于保护环境的名言:https://www.nanss.com/xuexi/4886.html 五行属金的字:https://www.nanss.com/shenghuo/4660.html 科比语录:https://www.nanss.com/shenghuo/4314.html 研究性学习课题:https://www.nanss.com/xuexi/4600.html 海参的挑选怎么挑:https://www.nanss.com/wenti/4864.html 2个字的游戏名字:https://www.nanss.com/mingcheng/4287.html 绿色配什么颜色好看:https://www.nanss.com/wenti/4708.html 房屋买卖协议:https://www.nanss.com/shenghuo/4729.html 英雄联盟名字大全霸气:https://www.nanss.com/mingcheng/4427.html 干净短句暖心8字:https://www.nanss.com/yulu/4571.html 礼貌骂人:https://www.nanss.com/yulu/4572.html 通讯报道:https://www.nanss.com/gongzuo/4746.html 行会名称:https://www.nanss.com/mingcheng/4898.html 堵车的幽默说说:https://www.nanss.com/wenan/4625.html 七夕情话最经典语录:https://www.nanss.com/yulu/4814.html qq群名:https://www.nanss.com/mingcheng/4976.html 我的奇思妙想作文450字四年级:https://www.nanss.com/xuexi/4252.html uno是什么:https://www.nanss.com/wenti/4616.html 吸引人的抖音名字:https://www.nanss.com/mingcheng/4272.html 关于疫情的好词好句好段摘抄:https://www.nanss.com/xuexi/4469.html



发表评论:
加入我们
QQ群1:5276420
QQ群2:3336901
QQ群3:254622631
文档群:150657323
文档翻译平台:按此访问
社区邮件列表:按此订阅
扫码关注
© PostgreSQL中文社区 ... (自2010年起)