作者:颜博 青云科技数据库研发工程师
目前从事 PostgreSQL 产品开发工作,热衷于 PostgreSQL 数据库的学习和钻研
在数据库系统中,数据是所有的根底,数据的平安更是重中之重。但可能因为各种起因导致数据失落或损坏,因而数据的备份和复原便显得尤为重要。
PostgreSQL 是一个弱小的开源对象关系数据库系统,通过 30 多年的踊跃开发,在可靠性、性能稳健性和性能方面博得了良好的名誉。Point-In-Time Recovery(简称 PITR) 是 PostgreSQL 的基于工夫点的数据恢复技术,在开启 WAL 日志归档及根底备份后,能够即时复原用户误操作失落的数据,为您的数据库加上一重"保险"。
本文将演示 PostgreSQL 如何配置 PITR 数据备份,以及当数据被误删除后如何进行数据即时复原。
|环境筹备
筹备数据库
筹备 PostgreSQL 11 版数据库环境。
# echo "$PGHOME" 查看PGHOME=/usr/lib/postgresql/11# echo "$PGDATA" 查看,PostgreSQL 的数据目录PGDATA=/data/pgsql/main# pg 命令行执行 show config_file; 命令查看,PostgreSQL 的配置文件地位PGCONFIG_FILE=/etc/postgresql/11/main/postgresql.conf# PostgreSQL 的启动日志地位,当启动失败时,能够通过日志疾速查看报错内容PG_LOG=/data/pglog/start.log
|PITR 数据备份
当用户误删除数据之后,能够通过 PITR 复原指定的工夫戳的数据。上面介绍开启备份及复原数据的具体步骤。
开启 WAL 日志归档
通过批改配置文件,开启 WAL 日志归档性能。
# 创立保留wal日志归档的目录,并批改目录拥有者$ mkdir -p /data/wallog_back$ chown -R postgres:postgres /data/wallog_back# 批改 PostgreSQL 数据库配置文件 postgresql.conf$ vim /etc/postgresql/11/main/postgresql.conf# 批改如下配置项:wal_level = archivearchive_mode = on archive_command = 'test ! -f /data/wallog_back/%f && cp %p /data/wallog_back/%f' # 配置归档命令,拷贝 wal 文件到指定目录,%p 为理论的 wal 文件目录,%f 为 wal 文件名称# 保留后重启数据库root:~# su postgres# stop postgresql serverpostgres:~$ /usr/lib/postgresql/11/bin/pg_ctl stop# start postgresql serverpostgres:~$ /usr/lib/postgresql/11/bin/pg_ctl -D /data/pgsql/main/ start -o '-c config_file=/etc/postgresql/11/main/postgresql.conf' -l /data/pglog/start.log
胜利保留配置批改,即关上了 WAL 日志归档性能。
筹备数据
# 创立测试表CREATE TABLE test_table( ID INT NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL);# 插入测试数据。通过以下命令,疾速插入 200001 条测试数据insert into test_table(id, name, age) values(generate_series(0,200000), substr(md5(random()::text), 0, 25), generate_series(0,200000));
执行根底备份
- PostgreSQL 查看以后工夫,指定备份时的 lable。
postgres=# select now(); now ------------------------------- 2021-11-04 14:50:42.482253+08(1 row)
- 开始备份
# PostgreSQL 命令行执行 postgres=# select pg_start_backup('backup_2021-11-04 14:50:42'); pg_start_backup ----------------- 0/9000060(1 row)# 切换到 linux 命令行执行cd /data$ sudo tar -cvzf pgsql.tar pgsqlpostgres=# select pg_stop_backup();NOTICE: pg_stop_backup complete, all required WAL segments have been archived pg_stop_backup ---------------- 0/9000168(1 row)
- 执行一次 WAL 切换,使得 WAL 日志归档
postgres=# select pg_switch_wal(); pg_switch_wal --------------- 0/A000078(1 row)
- 查看归档状态,如果 archived_count 为 0 代表异样,须要查看配置文件是否批改胜利或尝试重启 PostgreSQL 服务
postgres=# select * from pg_stat_archiver;
留神:
- 备份实现后,
/data/wallog_back/
目录下应该有相应的归档文件。
select * from pg_stat_archiver;
命令执行实现后,如果archived_count
为 0,代表归档设置开启失败,请从新查看配置文件或重启服务。
|PITR 数据恢复
模仿误删数据
# 删除 test_table 表中 id 大于 10000 的数据,模仿误删除数据的操作postgres=# delete from test_table where id>10000;DELETE 190000# 查问出以后工夫,依据此工夫确定大略的复原工夫点postgres=# select now(); now ------------------------------- 2021-11-04 14:56:17.452967+08(1 row)
即时复原数据
# 进行数据库运行$ /usr/lib/postgresql/11/bin/pg_ctl stop# 重命名 pgsql 目录cd /datamv pgsql pgsql_back# 复原备份包到 data 目录sudo tar -zxvf pgsql.tar# 配置 data 目录下的recovery.done文件,如果没有则创立。restore_command='cp /data/wallog_back/%f %p' # wal归档的目录recovery_target_time='2021-11-04 14:56:17.452967' # 复原到此时刻,依据之前的 select now()判断复原时刻。recovery_target_timeline='latest'
重新启动数据库
$ /usr/lib/postgresql/11/bin/pg_ctl -D /data/pgsql/main/ start -o '-c config_file=/etc/postgresql/11/main/postgresql.conf' -l /data/pglog/start.log
|数据恢复验证
postgres=# select * from test_table;postgres=# select count(*) from test_table; count -------- 200001(1 row)
如图所示,数据恢复到了 200001 条。
|总结
PITR 在配置肯定条件后,能够进行数据的复原,在误删除数据后,能够帮忙咱们找回数据。
|参考
- https://www.postgresql.org/
- https://blog.csdn.net/arcticJian/article/details/102746287
- https://my.oschina.net/Kenyon/blog/58112