Backup discourse from the command line

:bookmark: This guide covers how to back up and restore your Discourse site using command line tools, specifically designed for self-hosted environments.

:person_raising_hand: Required user level: Administrator

:information_source: You can use the below placeholders to automatically fill out the command line prompts in this guide:

Backing up

To create a backup of your Discourse site, follow these steps:

  1. Enter the container:
./launcher enter app
  1. Run the backup:
discourse backup
exit

This will print out the final filename of your backup. Download the backup and container configuration (if needed) to your local machine:

scp =USER=@=HOST=:/var/discourse/shared/standalone/backups/default/=FILENAME= .
scp =USER=@=HOST=:/var/discourse/containers/app.yml .

Restoring

To restore a Discourse backup, follow these steps:

  1. Install a Discourse site on the server you are restoring to.

If you’re testing this procedure multiple times, you can wipe the data on your test server.

Wipe Discourse data (optional)
./launcher stop app
./launcher destroy app
rm -r /var/discourse/shared/standalone/
mkdir -p /var/discourse/shared/standalone/backups/default/

  1. Upload the backup file:
scp =FILENAME= =USER=@=HOST=:/var/discourse/shared/standalone/backups/default/
  1. Enter the container and start the restore:
./launcher enter app
discourse enable_restore
discourse restore =FILENAME=

If you happen to forget what the filename was, run the “discourse restore” command by itself to get a list of available backup files.

:information_source: For a two-container setup, use ./launcher enter web_only and replace standalone with web-only in the path to backups.

For additional information about restoring Discourse backups, see Restore a backup from the command line.

Last edited by @SaraDev 2024-08-23T20:50:17Z

Check documentPerform check on document:
40개의 좋아요

where should I run this command? on my cloud server terminal or my local machine?
although either give me permission denied error

On your local machine.

Are you using an SSH key to connect? The error is related.

The command should then be:

scp -i /path_to_your_key/your_key.pem root@147.43.15.199:/var/discourse/shared/standalone/backups/default/discourse-2024-12-25-033740-v20241211222608.tar.gz .

I have a scenario where I want to migrate the Discourse installation to a whole new server.
My plan was:

  1. create a new blank Discourse installation
  2. backup the old Discourse, scp the backup to the new server and restore
  3. Copy over all the S3 (images, etc.) to the new S3 on a different account

The restore process goes well until it starts looking for S3 and then fail for no access. I think it is trying to access the same S3 bucket name on the old Discourse, but that one doesn’t exist on the new one and I can’t create it.

Is there a recommended solution?

I thought I could try to change the S3 bucket name on the old Discourse (before performing backup) and then when I restore, it would expect to have the new S3 bucket. But I’m afraid of what will happen to the old Discourse if I just switch it to a non-exisiting bucket.

There’s a hidden setting that will download the s3 files and our them in the backup.

You need to configure s3 in the yml file as in Configure an S3 compatible object storage provider for uploads

Then when you restore the backup it’ll push the uploads to the new bucket.

1개의 좋아요

Did you ever figure this one out?

You need to configure s3 in env variables as described in Configure an S3 compatible object storage provider for uploads

This is especially true if the s3 settings are different on the new server. Putting the s3 values in the database can cause lots of problems.

But there is a switch for the restore command that makes it pause between the various parts. It can help.

Can I double check that the file you end up with here is the same as you get when backing up from the forum website?

I always rebuild at the command line but first back up using the website. This time I forgot to back up! I think using the command line for both might help me :slight_smile: Wish me luck.

The backup script stated:

Finished!
[SUCCESS]
Backup done.
Output file is in: /var/www/discourse/public/backups/default/FILENAME.tar.gz

To begin with I tried to download that, which contains the wrong path. I found the file elsewhere, and only then noticed that the commands on this topic are correct…

안녕하세요! 업로드 없이 백업하려면 다음 명령어를 사용하세요:

discourse backup --sql-only

1개의 좋아요

좋은 지적입니다. 이 내용(그리고 몇 가지 다른 정보)을 원 게시글에 추가해야 합니다.

2개의 좋아요

안녕하세요 @supermathie! 만나서 반갑습니다. 혹시 저 기억하시나요? 얼마 전에 면접을 봤었거든요.

그리고 discourse backup 명령을 실행한 후에 --help 파라미터가 작동하면 좋겠어요. 대신에 백업 파일 이름이 "–helpXXX"로 지정되거든요. :lolsob:

감사합니다!

일부 AI의 도움으로 discourse 명령어의 하위 명령어에 대한 포괄적인 목록을 정리했습니다.
discourse CLI(script/discourse를 통해 Thor gem으로 구현됨)는 백업 및 복구, 사이트 관리 및 모드, 데이터베이스 유틸리티, **데이터 마이그레이션(가져오기/내보내기)**으로 분류된 13개의 핵심 하위 명령어를 제공합니다.


명령어 빠른 참조

카테고리 하위 명령어 주요 기능
백업 및 복구 backup 전체 또는 데이터베이스 전용 백업 아카이브 생성.
restore .tar.gz 백업 파일을 인스턴스로 복구.
enable_restore 복구 명령 실행 권한 부여.
disable_restore 복구 수행 권한 취소.
rollback 실패한 복구 후 데이터베이스 상태를 되돌림.
사이트 모드 enable_readonly 사이트 전체에 대한 임시 읽기 전용 모드 전환.
disable_readonly 읽기 전용 모드 해제하여 정상적인 사이트 쓰기 복구.
request_refresh 모든 활성 연결된 클라이언트에 브라우저 새로고침 프롬프트 트리거.
데이터베이스 작업 remap 데이터베이스 테이블 전체에서 글로벌 텍스트 검색 및 치환 수행.
데이터 마이그레이션 export_category 단일 카테고리, 해당 토픽 및 관련 사용자 내보내기.
import_category 카테고리 아카이브를 현재 포럼으로 가져오기.
export_topics ID를 기반으로 특정 토픽 및 작성자/답변자 계정 내보내기.
import_topics 토픽 아카이브를 현재 포럼으로 가져오기.

상세 하위 명령어 문서

1. 백업 및 복구 명령어

discourse backup [FILENAME]

  • 설명: 포럼 데이터베이스와 자산의 완전한 백업을 생성합니다.

  • 플래그:

  • --sql-only: (불리언, 기본값: false) 로컬 업로드 파일은 건너뛰고 PostgreSQL 데이터베이스만 덤프합니다.

  • --s3-uploads: (불리언, 기본값: false) S3에 저장된 자산을 백업 아카이브에 포함하도록 강제합니다(--sql-only가 설정된 경우 무시됨).

  • 예시:

discourse backup
discourse backup --sql-only

discourse restore [FILENAME]

  • 설명: /var/www/discourse/public/backups/default에 위치한 로컬 .tar.gz 아카이브에서 포럼 백업을 복구합니다.
  • 선행 조건: 먼저 discourse enable_restore를 실행해야 합니다.
  • 예시:
discourse restore discourse-2026-07-22-120000-v2026xxxx.tar.gz

discourse enable_restore & discourse disable_restore

  • 설명: 콘솔/웹 기반 복구 작업을 허용하거나 제한하는 애플리케이션 플래그를 전환합니다.
  • 사용 순서:
discourse enable_restore
discourse restore <filename>
discourse disable_restore

discourse rollback

  • 설명: 실패한 복구 실행 이전 시점으로 데이터베이스 스키마와 상태를 되돌립니다.

2. 사이트 상태 및 클라이언트 제어

discourse enable_readonly & discourse disable_readonly

  • 설명: Redis의 READONLY_MODE_KEY를 설정하거나 해제합니다.
  • 상세 정보:
  • 사용자가 사이트를 읽을 수 있도록 유지하면서 데이터베이스 쓰기를 동결합니다.
  • 터미널 세션이 충돌하더라도 사이트가 영구적으로 잠금되지 않도록 활성 상태 동안 갱신되는 짧은 수명(TTL) 루프를 사용합니다.

discourse request_refresh

  • 설명: MessageBus를 통해 시스템 메시지를 전송하여 온라인 웹 세션이 웹 페이지를 다시 로드하도록 지시합니다. 자산 업데이트나 글로벌 테마 변경 후 배포 시 유용합니다.

3. 데이터베이스 재매핑

discourse remap [OPTIONS] FROM TO

  • 설명: 데이터베이스의 텍스트 컬럼에서 직접 문자열 검색 및 치환을 수행합니다. 도메인 이름, CDN 경로 또는 이미지 업로드 접두사를 업데이트할 때 중요합니다.

  • 플래그:

  • --global: (불리언) 표준 콘텐츠 테이블이 아닌 모든 테이블을 스캔하고 재매핑합니다.

  • --regex: (불리언) FROM 매개변수를 리터럴 문자열이 아닌 정규 표현식으로 평가합니다.

  • 예시:

discourse remap "old-domain.com" "new-domain.com"
discourse remap --global "/uploads/default/" "/cdn-uploads/"

4. 선택적 데이터 가져오기 및 내보내기

discourse export_category [CATEGORY_ID]

  • 설명: 모든 중첩된 토픽, 게시물 및 참여 사용자 프로필을 포함하여 단일 카테고리를 JSON 아카이브 파일로 직렬화합니다.

discourse import_category [FILE_PATH]

  • 설명: export_category로 생성된 아카이브를 읽고 카테고리 데이터를 대상 인스턴스에 병합합니다.

discourse export_topics [TOPIC_IDS...]

  • 설명: 하나 이상의 공백으로 구분된 토픽 ID를 받아 토픽, 게시물 및 관련 사용자 계정을 내보냅니다.
  • 예시:
discourse export_topics 102 105 210

discourse import_topics [FILE_PATH]

  • 설명: export_topics로 생성된 JSON 아카이브를 대상 Discourse 인스턴스로 가져옵니다.
1개의 좋아요