postgres实现任意时间点恢复(使用pg_basebackup)

配置postgresql.conf文件

#日志级别
wal_level = replica
#归档开关
archive_mode = on
#归档命令
archive_command = 'cp %p /usr/local/postgres/archive/%f' 

第一次执行这个配置,需要重启数据库;

查看归档目录

此后数据库的操作都会记录wal日志文件到/usr/local/postgres/archive目录下
在这里插入图片描述
如果归档目录中没有内容,需要执行强制日志归档命令

select pg_switch_wal();

使用pg_basebackup进行基础备份

[bxy@localhost postgres]$ ./bin/pg_basebackup -h 127.0.0.1 -Ft -Pv -Xf -D backup/

备份完成后,对应备份目录会出现基础备份 是一个压缩包
在这里插入图片描述

准备试验数据

在2023-02-01 16:40:00 创建表dog 并随意插入几条数据
等待适当时间,在16:43:00创建表cat 并随意插入几条数据
恢复时间点 2023-02-01 16:42:00 (创建两张表之间的时间点就行)
如果恢复后数据库中存在dog表,不存在cat表 则试验成功。
在这里插入图片描述

模拟数据库故障

模拟数据库故障,停掉数据库

使用备份替换data目录

在这里插入图片描述

修改postgresql.conf

restore_command = 'cp /usr/local/postgres/archive/%f %p'
recovery_target_time = '2023-02-02 06:55:00'

添加空文件 提醒postgresql要做recovery

touch data/recovery.signal

重启数据库

[bxy@localhost postgres]$ ./bin/pg_ctl -D /usr/local/postgres/data/ start
waiting for server to start....2023-02-02 07:09:59.237 CST [2897] LOG:  starting PostgreSQL 13.8 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit
2023-02-02 07:09:59.238 CST [2897] LOG:  listening on IPv6 address "::1", port 5432
2023-02-02 07:09:59.238 CST [2897] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2023-02-02 07:09:59.241 CST [2897] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2023-02-02 07:09:59.250 CST [2898] LOG:  database system was interrupted; last known up at 2023-02-02 06:50:57 CST
cp: 无法获取"/usr/local/postgres/archive/00000003.history" 的文件状态(stat): 没有那个文件或目录
2023-02-02 07:09:59.277 CST [2898] LOG:  starting point-in-time recovery to 2023-02-02 06:55:00+08
2023-02-02 07:09:59.282 CST [2898] LOG:  restored log file "00000002.history" from archive
2023-02-02 07:09:59.311 CST [2898] LOG:  restored log file "000000020000000000000006" from archive
2023-02-02 07:09:59.349 CST [2898] LOG:  redo starts at 0/6000028
2023-02-02 07:09:59.350 CST [2898] LOG:  consistent recovery state reached at 0/6000100
2023-02-02 07:09:59.350 CST [2897] LOG:  database system is ready to accept read only connections
2023-02-02 07:09:59.379 CST [2898] LOG:  restored log file "000000020000000000000007" from archive
 done
server started
[bxy@localhost postgres]$ 2023-02-02 07:09:59.413 CST [2898] LOG:  recovery stopping before commit of transaction 491, time 2023-02-02 07:00:38.056702+08
2023-02-02 07:09:59.413 CST [2898] LOG:  pausing at the end of recovery
2023-02-02 07:09:59.413 CST [2898] HINT:  Execute pg_wal_replay_resume() to promote.

[bxy@localhost postgres]$ 
[bxy@localhost postgres]$ ./bin/psql -d postgres
psql (13.8)
Type "help" for help.

postgres=# 
postgres=# \d
       List of relations
 Schema | Name | Type  | Owner 
--------+------+-------+-------
 public | dog  | table | bxy
(1 row)

postgres=# select pg_wal_replay_resume();
 pg_wal_replay_resume 
----------------------
 
(1 row)

postgres=# 2023-02-02 07:12:05.587 CST [2898] LOG:  redo done at 0/700F3B0
cp: 无法获取"/usr/local/postgres/archive/00000003.history" 的文件状态(stat): 没有那个文件或目录
2023-02-02 07:12:05.594 CST [2898] LOG:  selected new timeline ID: 3
2023-02-02 07:12:05.652 CST [2898] LOG:  archive recovery complete
2023-02-02 07:12:05.660 CST [2898] LOG:  restored log file "00000002.history" from archive
2023-02-02 07:12:05.678 CST [2897] LOG:  database system is ready to accept connections

postgres=# 

恢复后的数据库为只读模式 需要执行

select pg_wal_replay_resume()

查看当前时间线的命令

[bxy@localhost postgres]$ ./bin/pg_controldata /usr/local/postgres/data/ |grep TimeLineID
Latest checkpoint's TimeLineID:       3
Latest checkpoint's PrevTimeLineID:   2
PostgreSQL中,`pg_basebackup` 是一个用于创建数据库集群基础备份的工具。通常情况下,`pg_basebackup` 需要连接到PostgreSQL数据库服务器,并且需要提供正确的用户名和密码。为了避免每次备份时都输入密码,可以通过以下几种方式实现免密码备份: ### 1. 使用 `.pgpass` 文件 `.pgpass` 文件可以存储数据库连接的用户名和密码,从而避免在每次连接时输入密码。步骤如下: 1. 创建一个 `.pgpass` 文件,通常位于用户主目录下: ``` touch ~/.pgpass ``` 2. 编辑 `.pgpass` 文件,添加以下内容: ``` hostname:port:database:username:password ``` 例如: ``` localhost:5432:postgres:myuser:mypassword ``` 3. 设置文件权限: ``` chmod 600 ~/.pgpass ``` ### 2. 使用 SSH 密钥认证 如果通过 SSH 进行备份,可以使用 SSH 密钥认证来避免输入密码: 1. 生成 SSH 密钥对(如果还没有): ``` ssh-keygen -t rsa ``` 2. 将公钥复制到远程服务器: ``` ssh-copy-id user@remote_host ``` 3. 使用 `pg_basebackup` 时,通过 SSH 进行连接: ``` pg_basebackup -D /path/to/backup -U username -P -v -w ``` ### 3. 使用 `pg_hba.conf` 配置免密码认证 在 PostgreSQL 服务器的 `pg_hba.conf` 文件中,可以配置免密码认证。例如,使用 `trust` 方式: 1. 编辑 `pg_hba.conf` 文件: ``` host all all 0.0.0.0/0 trust ``` 2. 重启 PostgreSQL 服务以使配置生效: ``` sudo systemctl restart postgresql ``` ### 4. 使用环境变量 可以通过设置环境变量 `PGPASSWORD` 来指定密码: ``` export PGPASSWORD=mypassword pg_basebackup -h remote_host -D /path/to/backup -U username -P -v ``` 通过以上方法,可以实现 `pg_basebackup` 备份时不用输入密码,从而提高备份的自动化和效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值