# PGsql通过归档恢复到指定位置实验

> 作者/来源: UCloud 运营管理员
> 发布时间: 2023-01-11T05:19:00.000Z
> 分类: 其他
> 原文链接: http://117.50.162.249:3000/yun/articles/1427

---

# PGsql通过归档恢复到指定位置实验

> 来源: https://www.ucloud.cn/yun/129500.html
> 作者: IT那活儿
> 发布日期: 发布于2023-01-11 13:19

PGsql通过归档恢复到指定位置实验

**点击上方“IT那活儿”公众号，关注后了解更多内容，不管IT什么活儿，干就完了！！！**

1

**当前环境：**

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129500/images/129500_000.png)

2

**归档配置**

因为本次实验需要通过归档来恢复数据，所以需要开启pgsql的归档模式。重启生效。

--修改postgresql.conf参数文件：

```
vim postgresql.confarchive_mode = onarchive_command = test ! -f /pgdata/archive/%f && cp %p /pgdata/archive/%f
```

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129500/images/129500_001.png)

3

**前期准备**

**1）测试数据准备**

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129500/images/129500_002.png)

**2）备份数据**

---执行备份：

```
pg_basebackup -D /pgdata/pg_backup -Ft -Pv -Upostgres -h 192.168.168.123 -p 1921
```

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129500/images/129500_003.png)

**3）备份后继续创建测试数据**

```
test=# create table test3 (id int);CREATE TABLEtest=# insert into test3 values(123);INSERT 0 1test=# insert into test3 values(125);
```

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129500/images/129500_004.png)

**4）切换归档日志**

```
postgres=# select pg_switch_wal();pg_switch_wal---------------0/9012CB0(1 row)
```

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129500/images/129500_005.png)

4

**故障模拟**

**1）模拟误操作删除database**

```
test=# c postgresYou are now connected to database "postgres" as user "postgres".postgres=# drop database test;DROP DATABASE
```

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129500/images/129500_006.png)

**2）关闭pgsql**

--关闭：

```
pg_ctl stop
```

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129500/images/129500_007.png)

**3）删除数据**

--删除：

```
rm -rf /pgdata/12/data/*‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
```

5

**恢复到drop前的状态**

**1）恢复数据**

--恢复数据文件：

```
tar xf base.tar -C /pgdata/12/data/
```

--恢复归档：

```
tar xf pg_wal.tar -C /pgdata/archive/
```

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129500/images/129500_008.png)

**注：**本次实验虽然删除了数据文件，但归档仍保留；所以在这一步中，恢复归档作用不大，因为后续需要用的归档是完全备份后生成的归档，而不是备份文件中的归档文件。

**2）查看归档**

--查看最近的归档文件中内容：

```
pg_waldump 000000040000000000000009
```

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129500/images/129500_009.png)

**3）选择xid**

通过查看最近的归档可以看到，tx为499的记录中有drop操作记录。而当前需要恢复到drop操作之前的位置。

所以选择tx为498的位置。

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129500/images/129500_010.png)

**4）修改postgresql.auto.conf文件**

--添加恢复参照：

```
restore_command = cp /pgdata/archive/%f %precovery_target_xid=498
```

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129500/images/129500_011.png)

注：这里的参数设置中需要注释recovery\_target = immediate；否则使用后会将误删除操作一并恢复。

**5）恢复**

---进入恢复模式：

```
touch /pgdata/12/data/recovery.signalpg_ctl start
```

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129500/images/129500_012.png)

**6）验证drop的database是否恢复**

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129500/images/129500_013.png)

---关闭恢复模式：

```
select pg_wal_replay_resume();
```

至此，pgsql通过归档恢复到指定位置完成。

END

## 本文作者：胡晓明

## 本文来源：IT那活儿（上海新炬王翦团队）

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129500/images/129500_014.png)

​