如何打造 PostgreSQL 闪回环境? 原作者:乐途 创作时间:2019-01-16 02:20:58+08 |
redraiment 发布于2019-01-16 02:20:58
![]() ![]() ![]() ![]() ![]() |
作者介绍
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脚本
下载地址:
- 链接:https://pan.baidu.com/s/1-dm58lMno5X_P7krCQneXg
- 提取码:ofrj
[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
请在登录后发表评论,否则无法保存。