从命令行恢复备份

:bookmark: 本指南介绍了如何在不使用 Discourse Web UI 的情况下,通过命令行恢复 Discourse 备份。
:person_raising_hand: 所需用户级别:管理员
:wrench: 需要控制台访问权限

以下是在不启动 Discourse Web UI 的情况下,通过命令行恢复 Discourse 备份的方法。这在您迁移服务器时非常有用。

先决条件

在开始之前,请确保完成以下步骤:

  1. 从源 Discourse 实例下载最新的备份文件。
  2. 通过运行 ./discourse-setup 或复制您现有的 app.yml 来引导目标 Discourse 实例。
  3. 确保目标 Discourse 实例是最新版本。如有必要,请更新它。

传输备份

  1. SSH 进入目标服务器,或在目标服务器上创建备份文件夹:

mkdir -p /var/discourse/shared/standalone/backups/default

  1. 将您的备份文件上传到目标服务器。

scp /path/to/backup/backup.tar.gz root@192.168.1.1:/var/discourse/shared/standalone/backups/default

请务必替换您正在使用的路径、文件名和服务器名称——但您确实希望备份文件最终位于:

/var/discourse/shared/standalone/backups/default

:mega: 您也可以从 Google Drive、Dropbox、OneDrive 等流行的网络存储站点上传和下载您的 Discourse 备份文件——您需要根据您首选的网络存储提供商查找特定的命令行说明。

:warning: 请勿更改备份的文件名! Discourse 将备份文件名视为元数据,因此如果您更改文件名,恢复将无法正常工作。请保留原始文件名。

/path/to/backup/discourse-xyz.tar.gz 替换为您备份文件的本地路径,将 \u003cserver_ip_address\u003e 替换为目标服务器的 IP 地址。

:bulb: 如果使用 Nginx 作为反向代理(https://meta.discourse.org/t/running-other-websites-on-the-same-machine-as-discourse/17247),请确保容器可以读取所有备份路径,并且 Nginx 可以读取 .sock 文件。

恢复备份

  1. 访问您的目标服务器并导航到 Discourse 文件夹:
cd /var/discourse
  1. 进入 Discourse Docker 应用容器:
./launcher enter app
  1. 启用恢复功能:
discourse enable_restore
  1. 恢复备份文件:
discourse restore sitename-2019-02-03-042252-v20190130013015.tar.gz

:bulb: 提示: 如果您在运行 discourse restore 时不提供文件名,它将列出所有可用的备份文件。

:warning: 如果您站点的 backup_location 设置配置为使用 S3,但您手动将备份文件上传到本地文件系统,则需要指定 --location local

discourse restore --location local sitename-2019-02-03-042252-v20190130013015.tar.gz

类似地,使用 --location s3 可以直接从 S3 备份恢复,而无需先下载它。

  1. 退出 Discourse Docker 应用容器:
exit

重新构建

恢复备份后,您可以选择重新构建目标实例以确保所有设置和配置都已正确应用。

:mega: 现在是更新 /var/discourse/containers/app.yml 以配置完整 HTTPS、附加插件或 CDN 配置的好时机。比较两个实例的 app.yml 配置以确保一致!

cd /var/discourse
./launcher rebuild app

启用电子邮件

恢复备份后,将禁用非员工用户的外发邮件。您不希望您的测试服务器、新服务器或因其他原因恢复了备份的服务器开始向您的用户发送电子邮件!将 disable_emails 站点设置更改为“no”以重新启用电子邮件。

:tada: 就这样。您的 Discourse 服务器已成功恢复。

78 个赞
Move your Discourse Instance to a Different Server
Any other way to take backup and restore?
How easy is it to move to another server?
HELP! My Discourse just deleted everything?
Restore backup is broken
How to migrate Discourse from one server to another with the same DNS name
Best Practices for Backups
Problem upgrading Discourse
Upgrading v2.2.0.beta4 forum with unknown local changes
Set up file and image uploads to S3
Quick question about site backups
My install broke after updating, how can I fix it?
Is there any way to restore your site from backup in the terminal?
Migrating Discourse from one DigitalOcean droplet to another without downtime
Restore backup right away after installing Discourse
Unable to migrate to S3, therefore unable to restore from backup
Restore Failure - S3 (compatible) backup
"EXCEPTION: psql failed: DETAIL: Key (post_id)=(36946) is duplicated."
Migrate from another forum to Discourse
My install is 16,359 commits behind! Advice?
Trying to recover an installation
Migration failed: relation "user_required_fields_versions" already exists
How can I manually verify via the CLI and bypass the Congratulations, you installed Discourse! screen?
Configuring automatic backups
Migrating to a new server that has a new DB and new S3 buckets for backup and uploads
Intended path to migrate S3 to local
Problem when updating Discourse Forum
Failed to restore from the backup
Steps involved to downgrade from 2GB to 1GB on DO?
Forum offline: Restore is not working through web
Forum offline: Restore is not working through web
Testing Restore - not working
Entire site is a blank page after upgrade
"discourse: command not found" when trying to restore a backup from the command line
Stuck with 500 error after weird bugs and a rebuild
Plesk server migration
"Key is stored in legacy trusted.gpg keyring" warning
"Key is stored in legacy trusted.gpg keyring" warning
Discourse broken after moving servers
Stuck and lost updating forum, problems with PG migration
How to manually migrate s3 files to local?
Index_users_on_username_lower error during database restore: import failed
How can I get the current version information from my backup?
Backup Prod -> Snap -> Build Test -> Change Address
How to properly package discourse as an image
2FA with OTP broken after restoring from Backup
I'm trying to migrate an old discourse by creating a new discourse, but I'm having trouble
Recover from filesystem backup: can't rebuild nor start
Error: Can't notify admin while restoring backup during a migration to a fresh install
MKJ's Opinionated Discourse Deployment Configuration
Migrate from AWS to Digital Ocean with 2 containers, spaces and 2 CDNs
Cannot restore database: sql key is duplicated
Finding UI generated backup and restoring site
Backup discourse from the command line
Migration to a Self-Hosted solution from Kubernetes
How to download the backup file without SMTP function?
Redis Problems? (Forum broken after upgrade)
Can't upload backup
Can't upload backup
Can't upload backup
Rate limiter issues when uploading a backup file / can't disable rate limiter
Help restoring - system hung at midnight
Help restoring - system hung at midnight
Uploads missing after restore
My install is 16,359 commits behind! Advice?
Issues Rebuilding After Upgrade to Ubuntu 22.04

这些说明对我们从备份恢复很有帮助,但我们不得不修改 discourse restore 命令:

discourse restore --location local sitename-2019-02-03-042252-v20190130013015.tar.gz

(我的示例使用了上面示例中的文件名)才能让恢复找到我们放在 /var/discourse/shared/standalone/backups/default 目录中的备份(而不是存储在 s3 上的备份)。

2 个赞

恢复后是否需要重建?

另外,我恢复到了一个带有 NGINX 反向代理的新服务器,该代理随后转发到上游的 discourse。因此,我在 discourse 中禁用了 SSL,但我注意到在恢复过程中:

Remapping 'https://example.com' to 'http://example.com'

这是在重新映射所有内部链接吗?是否可以通过 discourse remap http://example.com https://example.com 来撤销此操作?

不需要。

看起来是这样。是的,你可以重新映射它们。

你应该设置 force_https 变量。

1 个赞

如果备份是 15 GB,恢复需要多少空间?

1 个赞

是数据库还是上传文件也有?

可能要多 3-5 倍。

1 个赞

它是数据库+上传 15GB,我的 60GB 硬盘上有 20GB 空间,但每次还原都失败,我需要至少 50-60GB 的空间才能还原?

1 个赞

您需要足够的空间来存放备份、上传内容和未压缩的数据库。

您可以尝试先恢复仅包含数据库的备份,然后手动使用 rsync 复制上传内容。

2 个赞

我已经验证了我的管理员帐户在自托管上,并上传了 .sql.gz 备份,而不是 .tar.gz

在 UI 中恢复没有成功,所以我从命令行执行了“恢复备份”,在 discourse restore 过程结束时显示 [FAILED],官方托管的输入文件是 .tar.gz 是否会导致进程通过?

我的官方托管是几天前建立的,我的自托管在 更改 container.yml 中的 SMTP 值 后于今天开始正常工作。

1 个赞

您需要包含您收到的错误信息。文件名中包含版本信息,因此,如果您重命名了文件,则很可能需要将其重命名回原来的名称,只更改文件名开头的单词。

1 个赞

关于 discourse restore 是否有文档?因为似乎有 --location local 选项,我猜想也会有一个用于 S3 的选项?

我希望从存放在 S3 的备份中还原,避免事先手动下载。

编辑:算了,我刚刚发现 discourse restore --location s3 $filename 似乎可以正常工作。

2 个赞

因为我在app.yml中启用了S3,所以仅仅使用纯粹的discourse restore <filename>就能正常工作。

我认为如果你搜索backup restore s3,就能找到正确的帖子。

2 个赞

您好,我已完成此过程,使用了 s3(-cli) 作为我的 scp,并将发件邮件提供商从 MailGun 更改为 Brevo。

恢复后,我找不到移除横幅的方法?

移除横幅最简单的方法是在站点设置中启用电子邮件

3 个赞

对了。你也可以用 CSS 隐藏它!:joy:

1 个赞

是的。我有点犹豫推荐这个,因为我不得不考虑 Air theme hides "outgoing email disabled" warning

1 个赞

每次恢复备份时,非员工的电子邮件都会被自动禁用。 我猜想这是在恢复到测试服务器后,为了防止服务器向用户发送通知(这些用户本不该知道该服务器的存在),代码中添加的。

我已相应更新了 OP:

2 个赞

操作指南中的注意事项:

如果您定期为测试目的恢复备份,并且您的测试服务器配置正确,可以通过脚本启用电子邮件,以发送电子邮件到调试电子邮件服务(例如 https://github.com/maildev/maildev):

docker exec -it app /bin/bash --login \
-c "rails runner 'SiteSetting.disable_emails=\"no\";'"

在这种情况下,您可能想要在您的 app.yml 中将 DISCOURSE_DISABLE_EMAILS 设置为 no。(同时也将 DISCOURSE_ENABLE_RESTORE 设置为以简化还原)

1 个赞