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

相关文章

MyBatis分页查询实战案例完整流程

《MyBatis分页查询实战案例完整流程》MyBatis是一个强大的Java持久层框架,支持自定义SQL和高级映射,本案例以员工工资信息管理为例,详细讲解如何在IDEA中使用MyBatis结合Page... 目录1. MyBATis框架简介2. 分页查询原理与应用场景2.1 分页查询的基本原理2.1.1 分

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

从原理到实战解析Java Stream 的并行流性能优化

《从原理到实战解析JavaStream的并行流性能优化》本文给大家介绍JavaStream的并行流性能优化:从原理到实战的全攻略,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的... 目录一、并行流的核心原理与适用场景二、性能优化的核心策略1. 合理设置并行度:打破默认阈值2. 避免装箱

Maven中生命周期深度解析与实战指南

《Maven中生命周期深度解析与实战指南》这篇文章主要为大家详细介绍了Maven生命周期实战指南,包含核心概念、阶段详解、SpringBoot特化场景及企业级实践建议,希望对大家有一定的帮助... 目录一、Maven 生命周期哲学二、default生命周期核心阶段详解(高频使用)三、clean生命周期核心阶

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

Java 正则表达式的使用实战案例

《Java正则表达式的使用实战案例》本文详细介绍了Java正则表达式的使用方法,涵盖语法细节、核心类方法、高级特性及实战案例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、正则表达式语法详解1. 基础字符匹配2. 字符类([]定义)3. 量词(控制匹配次数)4. 边

Java Scanner类解析与实战教程

《JavaScanner类解析与实战教程》JavaScanner类(java.util包)是文本输入解析工具,支持基本类型和字符串读取,基于Readable接口与正则分隔符实现,适用于控制台、文件输... 目录一、核心设计与工作原理1.底层依赖2.解析机制A.核心逻辑基于分隔符(delimiter)和模式匹