pg_basebackup备份恢复实战

2024-05-11 22:28

本文主要是介绍pg_basebackup备份恢复实战,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先PG需要开启归档模式,wal_level至少设置为replica

1.用脚本不停往一个测试表中写数据:

create table t1(id int primary key,name varchar(10));

shell中执行:

i=1;while true; do psql -p15432 -d test -c "insert into t1 values($i,'aa');";echo $i; i=$(($i+1)); sleep 1; done

INSERT 0 1

5

当前的PGDATA如下:

PGDATA=/data/pgsql-12

备份归档路径为/data/pg_arch

2.在某一个时刻发起备份命令:

pg_basebackup -D /var/lib/pgsql/pg_backup/ -Ft -Pv -U postgres  -p15432 -R

# -D 空文件,没有该目录会自动创建

# F  格式话

# t  打包为tar包

# Pv显示备份的详细过程

# -u 用户

# -p 端口

备份完成的时候,查看那个测试表的数据大概写了20多条

3.再过了一段时间之后关闭pg

#先停止插入脚本,关闭pg的时候写了大概119条

INSERT 0 1

119

#切换日志

select pg_switch_wal();

#检查当前的lsn之后关闭PG

select pg_walfile_name(pg_current_wal_lsn());

systemctl stop postgresql-12

4.恢复备份文件和日志文件

#备份一下原来的PGDATA

cd /data

mv pgsql-12 pgsql-12.bak2

#创建新的PGDATA目录,并且设置为700权限

mkdir pgsql-12

chmod 700 pgsql-12

#恢复备份文件

cd /var/lib/pgsql/pg_backup/

tar -xvf base.tar -C /data/pgsql-12

tar -xvf pg_wal.tar -C /data/pg_arch

5.设置恢复模式

cd /data/pgsql-12

vi postgresql.auto.conf 

追加:

其中有多个恢复配置:

#立刻恢复:

restore_command = 'cp /data/pg_arch/%f %p'

recovery_target = 'immediate'

#可以按时间线恢复到最新

restore_command = 'cp /data/pg_arch/%f %p'

recovery_target_timeline = 'latest'

#按时间点恢复

restore_command = 'cp /data/pg_arch/%f %p' 

recovery_target_time = '2023-03-19 16:16:16.007657+08'

#指定lsn恢复,这里我们使用这个lsn进行恢复

restore_command = 'cp /data/pg_arch/%f %p' 

recovery_target_lsn = '0/150010E0' 

#指定回放xid  ,可以手工查询select txid_current();或者是通过pg_waldump查看归档的wal log

restore_command = 'cp /data/pg_arch/%f %p'  --在备份指定其回放时,从归档路径中寻找历史wal

recovery_target_xid = '816'  --执行要回放的事务ID节点

6.创建恢复文件,提示pg启动时候需要进行恢复

touch /data/pgsql-12/recovery.signal

7.启动数据库

pg_ctl -D /data/pgsql-12 start

这里日志里面显示了恢复wal日志信息:

2024-05-10 23:01:22.846 CST [27501] LOG:  entering standby mode

2024-05-10 23:01:22.864 CST [27501] LOG:  restored log file "000000010000000000000014" from archive

2024-05-10 23:01:22.894 CST [27501] LOG:  redo starts at 0/14000028

2024-05-10 23:01:22.896 CST [27501] LOG:  consistent recovery state reached at 0/14001668

2024-05-10 23:01:22.896 CST [27499] LOG:  database system is ready to accept read only connections

2024-05-10 23:01:22.913 CST [27501] LOG:  restored log file "000000010000000000000015" from archive

2024-05-10 23:01:22.958 CST [27501] LOG:  restored log file "000000010000000000000016" from archive

2024-05-10 23:01:22.982 CST [27501] LOG:  recovery stopping after WAL location (LSN) "0/16000028"

2024-05-10 23:01:22.982 CST [27501] LOG:  recovery has paused

2024-05-10 23:01:22.982 CST [27501] HINT:  Execute pg_wal_replay_resume() to continue.

#查询数据

test=# select count(*) from t1;

 count 

-------

   119        --数据和停止的时候保持一致

(1 row)

此时数据库为只读模式,还需要修改到读写模式

8.数据库切换为读写

select pg_wal_replay_resume();

9.删除recovery.signal文件

rm -rf /data/pgsql-12/recovery.signal

#关于恢复设置,官方文档如下:

27.2. Recovery Target Settings

By default, recovery will recover to the end of the WAL log. The following parameters can be used to specify an earlier stopping point. At most one of recovery_target, recovery_target_lsn, recovery_target_name, recovery_target_time, or recovery_target_xid can be used; if more than one of these is specified in the configuration file, the last entry will be used.

recovery_target = 'immediate'

This parameter specifies that recovery should end as soon as a consistent state is reached, i.e., as early as possible. When restoring from an online backup, this means the point where taking the backup ended.

Technically, this is a string parameter, but 'immediate' is currently the only allowed value.

recovery_target_name (string)

This parameter specifies the named restore point (created with pg_create_restore_point()) to which recovery will proceed.

recovery_target_time (timestamp)

This parameter specifies the time stamp up to which recovery will proceed. The precise stopping point is also influenced by recovery_target_inclusive.

recovery_target_xid (string)

This parameter specifies the transaction ID up to which recovery will proceed. Keep in mind that while transaction IDs are assigned sequentially at transaction start, transactions can complete in a different numeric order. The transactions that will be recovered are those that committed before (and optionally including) the specified one. The precise stopping point is also influenced by recovery_target_inclusive.

recovery_target_lsn (pg_lsn)

This parameter specifies the LSN of the write-ahead log location up to which recovery will proceed. The precise stopping point is also influenced by recovery_target_inclusive. This parameter is parsed using the system data type pg_lsn.

The following options further specify the recovery target, and affect what happens when the target is reached:

recovery_target_inclusive (boolean)

Specifies whether to stop just after the specified recovery target (true), or just before the recovery target (false). Applies when recovery_target_lsn, recovery_target_time, or recovery_target_xid is specified. This setting controls whether transactions having exactly the target WAL location (LSN), commit time, or transaction ID, respectively, will be included in the recovery. Default is true.

recovery_target_timeline (string)

Specifies recovering into a particular timeline. The default is to recover along the same timeline that was current when the base backup was taken. Setting this to latest recovers to the latest timeline found in the archive, which is useful in a standby server. Other than that you only need to set this parameter in complex re-recovery situations, where you need to return to a state that itself was reached after a point-in-time recovery. See Section 25.3.5 for discussion.

recovery_target_action (enum)

Specifies what action the server should take once the recovery target is reached. The default is pause, which means recovery will be paused. promote means the recovery process will finish and the server will start to accept connections. Finally shutdown will stop the server after reaching the recovery target.

The intended use of the pause setting is to allow queries to be executed against the database to check if this recovery target is the most desirable point for recovery. The paused state can be resumed by using pg_wal_replay_resume() (see Table 9.81), which then causes recovery to end. If this recovery target is not the desired stopping point, then shut down the server, change the recovery target settings to a later target and restart to continue recovery.

The shutdown setting is useful to have the instance ready at the exact replay point desired. The instance will still be able to replay more WAL records (and in fact will have to replay WAL records since the last checkpoint next time it is started).

Note that because recovery.conf will not be renamed when recovery_target_action is set to shutdown, any subsequent start will end with immediate shutdown unless the configuration is changed or the recovery.conf file is removed manually.

This setting has no effect if no recovery target is set. If hot_standby is not enabled, a setting of pause will act the same as shutdown.

这篇关于pg_basebackup备份恢复实战的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/980828

相关文章

从原理到实战深入理解Java 断言assert

《从原理到实战深入理解Java断言assert》本文深入解析Java断言机制,涵盖语法、工作原理、启用方式及与异常的区别,推荐用于开发阶段的条件检查与状态验证,并强调生产环境应使用参数验证工具类替代... 目录深入理解 Java 断言(assert):从原理到实战引言:为什么需要断言?一、断言基础1.1 语

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

深度解析Spring AOP @Aspect 原理、实战与最佳实践教程

《深度解析SpringAOP@Aspect原理、实战与最佳实践教程》文章系统讲解了SpringAOP核心概念、实现方式及原理,涵盖横切关注点分离、代理机制(JDK/CGLIB)、切入点类型、性能... 目录1. @ASPect 核心概念1.1 AOP 编程范式1.2 @Aspect 关键特性2. 完整代码实

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

Java Web实现类似Excel表格锁定功能实战教程

《JavaWeb实现类似Excel表格锁定功能实战教程》本文将详细介绍通过创建特定div元素并利用CSS布局和JavaScript事件监听来实现类似Excel的锁定行和列效果的方法,感兴趣的朋友跟随... 目录1. 模拟Excel表格锁定功能2. 创建3个div元素实现表格锁定2.1 div元素布局设计2.

Redis 配置文件使用建议redis.conf 从入门到实战

《Redis配置文件使用建议redis.conf从入门到实战》Redis配置方式包括配置文件、命令行参数、运行时CONFIG命令,支持动态修改参数及持久化,常用项涉及端口、绑定、内存策略等,版本8... 目录一、Redis.conf 是什么?二、命令行方式传参(适用于测试)三、运行时动态修改配置(不重启服务