暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

《课程笔记 | PostgreSQL深入浅出》之增量备份恢复(十三)

原创 布衣 2022-12-06
1433

介绍下PG物理备份增量恢复

增量恢复也是在生产环环境中数据恢复中使用最高频的一种恢复方式;因为增量恢复是可以恢复到数据库的任意时间点,也就是说不受限于咱们得备份时间点,只要日志保留完整,可以恢复到备份时间点之后,尽可能做到数据的少丢失,甚至是不丢失。
Egg:
1、先制造数据:

[postgres@pgclass pgdata]$ psql -h 192.168.84.9 -p 5432 用户 postgres 的口令: psql (12.12) 输入 "help" 来获取帮助信息. postgres=# create database reco; CREATE DATABASE postgres=# create table t1(id int); CREATE TABLE postgres=# insert into t1 values(1); INSERT 0 1 postgres=# insert into t1 values(2); INSERT 0 1 postgres=# insert into t1 values(3); INSERT 0 1 postgres=# insert into t1 values(4); INSERT 0 1 postgres=# insert into t1 values(5); INSERT 0 1 postgres=# commit; WARNING: there is no transaction in progress COMMIT postgres=# exit
复制

2、先备份基础数据:

[postgres@pgclass pg_basebackup]$ pg_basebackup -D /home/postgres/pg_basebackup/ -Ft -Pv -U postgres pg_basebackup: 开始基础备份,等待检查点完成 pg_basebackup: 已完成检查点 pg_basebackup: 预写日志起始于时间点: 0/1A000028, 基于时间轴2 pg_basebackup: 启动后台 WAL 接收进程 pg_basebackup: 已创建临时复制槽"pg_basebackup_1549" 41895/41895 kB (100%), 1/1 表空间 pg_basebackup: 预写日志结束点: 0/1A000100 pg_basebackup: 等待后台进程结束流操作... pg_basebackup: 同步数据到磁盘... pg_basebackup: 基础备份已完成
复制

3、备份后继续插入数据

reco=# create table t2(id int); CREATE TABLE reco=# insert into t2 values(1); INSERT 0 1 reco=# insert into t2 values(11); INSERT 0 1 reco=# insert into t2 values(113); INSERT 0 1
复制

4、误删除数据库

woo=# drop database reco; DROP DATABASE
复制

5、切一下归档

woo=# select pg_walfile_name(pg_current_wal_lsn()); pg_walfile_name -------------------------- 00000002000000000000001B (1 行记录) woo=# select pg_switch_wal(); pg_switch_wal --------------- 0/1B013998 (1 行记录) woo=# select pg_walfile_name(pg_current_wal_lsn()); pg_walfile_name -------------------------- 00000002000000000000001C (1 行记录)
复制

6、关库并删除数据

[postgres@pgclass pg_basebackup]$ pg_ctl stop -m fast
复制

等待服务器进程关闭 … 完成
服务器进程已经关闭
7、将备份数据解压到数据目录

[postgres@pgclass pgdata]$ tar -xf /home/postgres/pg_basebackup/base.tar -C $PGDATA
复制

8、编辑文件postgresql.auto.conf追加:

[postgres@pgclass pgdata]$ cat postgresql.auto.conf # Do not edit this file manually! # It will be overwritten by the ALTER SYSTEM command. archive_mode = 'on' archive_command = '/usr/bin/lz4 -q -z %p /usr/local/pgsql/pgarch/%f.lz4' restore_command = 'cp /usr/local/pgsql/pgarch/%f %p' recovery_target_time = '2022-11-15 23:15:00+08' #recovery_target = 'immediate'
复制

9、启动数据库

pg_ctl -D $PGDATA -l /tmp/logfile start pg_ctl: another server might be running; trying to start server anyway server starting
复制

10、验证数据

[postgres@pgclass ~]$ psql -h 192.168.84.9 -p 5432 -U postgres -l 用户 postgres 的口令: 数据库列表 名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限 -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres woo | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres reco | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | (5 行记录)
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论