# 谨慎行事：测试时的数据库备份与恢复流程

**URL:** https://meta.discourse.org/t/being-cautious-database-backup-restore-process-when-testing-things/135716
**Category:** Support
**Tags:** unsupported-install
**Created:** [2019年十二月12日 04:06 UTC](https://meta.discourse.org/t/being-cautious-database-backup-restore-process-when-testing-things/135716 "2019-12-12T04:06:57Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![mreach](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mreach](https://meta.discourse.org/u/mreach)
#### Post date: [2019年十二月12日 04:06 UTC](https://meta.discourse.org/t/being-cautious-database-backup-restore-process-when-testing-things/135716/1 "2019-12-12T04:06:57Z")

</div>

我正准备测试一个导入大量内容的流程，但想确保万一出现问题，能够正确回滚。

我运行的是外部 PostgreSQL 实例，因此我认为其实不需要备份 Docker 中的 Discourse Rails 实例，只需通过以下步骤备份数据库即可：

备份：

1. `./launcher stop app`
2. `pg_dump -U username -p 12345 -W -F p databasename | xz > ~/backup/database/$(date +"%d-%m-%Y_%H.%M.%S").databasename.pgsql.xz`
3. `./launcher start app`
4. `### 导入内容 ###`

如果情况不妙，恢复：

1. `./launcher stop app`
2. `xzcat ~/backup/database/DATE.databasename.pgsql.xz | psql -U username -p 12345 -W databasename`
3. `./launcher start app`

这样系统应该就能恢复正常并可用了……对吗？

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2019年十二月12日 04:10 UTC](https://meta.discourse.org/t/being-cautious-database-backup-restore-process-when-testing-things/135716/2 "2019-12-12T04:10:43Z")

</div>

很可能存在一个内置的、易于恢复的方案，它经过测试、获得支持，且只需点击几下或执行一条命令行即可完成。

如果您的方法不起作用，那就只能自行解决了。

---

<div class="post-metadata">

### Author: ![mreach](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mreach](https://meta.discourse.org/u/mreach)
#### Post date: [2019年十二月12日 04:12 UTC](https://meta.discourse.org/t/being-cautious-database-backup-restore-process-when-testing-things/135716/3 "2019-12-12T04:12:02Z")

</div>

正如我所指出的，这是针对外部 PostgreSQL 数据库的，因为我并未使用 Docker 容器化的版本。遗憾的是，常规的备份/恢复方法对我来说无法使用（可能是因为我在大数据库服务器上运行的 PostgreSQL 版本稍新）。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2019年十二月12日 04:28 UTC](https://meta.discourse.org/t/being-cautious-database-backup-restore-process-when-testing-things/135716/4 "2019-12-12T04:28:39Z")

</div>

哦，是的。如果你运行的是 PostgreSQL 11，那就得靠自己了。如果你在外部数据库上运行 PostgreSQL 10，应该没问题。但最好测试一下，看看是否可行。

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [2019年十二月12日 12:54 UTC](https://meta.discourse.org/t/being-cautious-database-backup-restore-process-when-testing-things/135716/5 "2019-12-12T12:54:43Z")

</div>

PG11 和 PG12 将很快在备份与恢复过程中得到支持。敬请期待。

---

<div class="post-metadata">

### Author: ![trying2survive](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@trying2survive](https://meta.discourse.org/u/trying2survive)
#### Post date: [2020年十一月17日 04:32 UTC](https://meta.discourse.org/t/being-cautious-database-backup-restore-process-when-testing-things/135716/7 "2020-11-17T04:32:58Z")

</div>

你好，现在支持它们了吗？

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2020年十一月17日 04:43 UTC](https://meta.discourse.org/t/being-cautious-database-backup-restore-process-when-testing-things/135716/8 "2020-11-17T04:43:15Z")

</div>

是的！升级是在几个月前进行的。

---

<div class="post-metadata">

### Author: ![trying2survive](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@trying2survive](https://meta.discourse.org/u/trying2survive)
#### Post date: [2020年十一月17日 14:50 UTC](https://meta.discourse.org/t/being-cautious-database-backup-restore-process-when-testing-things/135716/9 "2020-11-17T14:50:06Z")

</div>

谢谢！ clarification 一下，将备份恢复到 Discourse 站点（包含内容）是否会完全删除外部 PostgreSQL 数据库实例（RDS）中的所有数据？如果恢复失败或连接中断，RDS 中的数据是否会损坏？

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2020年十一月17日 15:27 UTC](https://meta.discourse.org/t/being-cautious-database-backup-restore-process-when-testing-things/135716/10 "2020-11-17T15:27:37Z")

</div>

它将删除 Discourse 数据库中的所有数据，但不会影响其他数据。如果恢复失败，现有数据将保持不变。
