Topic and Category Export/Import

(Neil Lalonde) #1

There’s a command line tool to export a set of arbitrary topics or an entire category.

The tool can be run from the base directory of the Discourse project:

su - discourse
cd /var/www/discourse

@jamesmarkcook’s post below gives step-by-step instructions.

Export Topics

The topic exporter will export all topics, posts, and all users who participated in those topics.

Get the topic IDs. For example, let’s say you want to export these two topics:


Run this command:

bundle exec script/discourse export_topics 25 27

Output should look something like this:

Starting export of topics...

Best dance moves
Hurt myself dancing

Export saved to topic-export-2016-02-02-165613.json


To import it into another Discourse site, move the output file to the other Discourse server and run the topic importer:

bundle exec script/discourse import_topics topic-export-2016-02-02-165613.json

In a multisite environment, use the RAILS_DB env variable.

RAILS_DB=xxxmoves bundle exec script/discourse export_topics 25 27
RAILS_DB=dancerehab bundle exec script/discourse import_topics topic-export-2016-02-02-165613.json

Export Category

The category exporter will export:

  • the category
  • all its subcategories
  • its security settings
  • custom groups mentioned in the security settings
  • all topics and posts in the category and subcategories
  • all users who posted, and assigns them to the groups

With the category id, run it like this:

bundle exec script/discourse export_category 5

And import it like this:

bundle exec script/discourse import_category category-export-2016-02-02-154352.json

Today the tool is brand new, so there’s more that it can do. Please report bugs in the #bug category, and request enhancements in the #feature category.

UI: Topic and Category Export/Import
CLI command to migrate a single category
Moving topics between sites?
Category/Topic export fails with error "database "discourse_development" does not exist"
Recovering data from an old version - with a difference
Duplicate Group and Topics
Export customized text and import it into a new Discourse instance
Migrating vBulletin onto existing and live Discourse instance
How to divide my community into 2 parts
Please visit our Discourse Forum! (Directory)
Selective backups skipping settings and config
Is it possible to merge two Discourse instances into one?
Test driving admin/moderation features
Importing from phpBB3
Add backup to existing Discourse instance
Merging two Discourse forums
Migrate topics from one discourse instance to another?
Adding default sets of topics, categories and users to a new Discourse
Discourse vs Email & Mailing lists
A Puzzle to Solve
Move your Discourse Instance to a Different Server
(Neil Lalonde) split this topic #2

A post was split to a new topic: UI: Topic and Category Export/Import

(Jay Pfaffman) #3

If a category gets imported, do links get preserved if a bunch of topics refer to each other?

(Jay Pfaffman) #4

I think that you left out

sudo su -
cd /var/discourse
./launcher enter app

But running the exporter gives this:

discourse@discourse:/var/www/discourse$` bundle exec script/discourse export_category 5`
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `rescue in connect': FATAL:  database "discourse_development" does not exist (ActiveRecord::NoDatabaseError)

Do we think this should still work, @neil?

(Neil Lalonde) #5

It should work. Maybe you need to put RAILS_ENV=production in front of it if it’s your production database you’re trying to export from?

RAILS_ENV=production bundle exec script/discourse export_category 5

(Jay Pfaffman) #6

That did the trick! I knew that I needed to tell it to use the production database, but had no idea how to accomplish that. Many thanks, @neil.

export RAILS_ENV
bundle exec script/discourse export_category 5
(James Cook) #7

After following all steps outlined above I receive the following error:

 Failed to initialize site default
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize': FATAL:  Peer authentication failed for user "discourse" (PG::ConnectionBad)```

This is on a nearly fresh install from the Digital Ocean droplet image. What have I missed?
(Jeff Atwood) #8

Have we checked this recently @neil for any regressions or code change related breakages?

(Neil Lalonde) #9

I used it on Car Talk this week to move a category from a sandbox site to the production site. It worked fine for me. Sounds like something different in your install. Can you access db console?

root@gtmdiscourse:~# cd /var/discourse/
root@gtmdiscourse:/var/discourse# ./launcher enter app
root@gtmdiscourse-app:/var/www/discourse# rails db
psql (9.5.3)
Type "help" for help.

(James Cook) #10

I tried the above on two separate discourse installations. One installation was done the standard docker way, the other was created from the official Digital Ocean snapshot.

I can access db console on both of them.

(James Cook) #11

I have just realised I hadn’t quite followed all the instructions.

The problem I was experiencing was because I hadn’t changed to the discourse user. I thought I had followed all instructions but obviously my tiredness was preventing me from seeing the light. :facepalm:

Thanks guys for building a cool tool. It will really help me out. :clap:

(James Cook) #12

I just wanted to post a quick step by step for anyone else struggling with this who needs a bit more detailed guidance.

Steps to make this work:

  1. SSH to your server where you have Discourse running.

  2. Change directory to /var/discourse.

  3. Enter the docker container running Discourse: ./launcher enter app. If you have called your container something else, replace app with the name of your container.

  4. Change to the discourse user (inside the container): su - discourse.

  5. Navigate to the discourse directory inside the container: cd /var/www/discourse

  6. Run your command, for example:

     RAILS_ENV=production bundle exec script/discourse export_category 1

If you follow all of these steps to the letter then it should work for you. I was initially getting confused as to where I should be the discourse user in what directory.

Copying from container to host

By default the file you generate will be stored in the container itself. To copy from the container to the host (your server running the container) run the following:

docker cp <container-id>:<path-to-file-on-container> <path-to-copy-to-on-host>

You will need to replace everything inside of <> with real values. To obtain your container id run docker ps.

My command looked like this:

docker cp a72e14c978ae:/var/www/discourse/category-export-2016-07-25-144953.json /tmp
(Erlend Sogge Heggen) split this topic #13

4 posts were split to a new topic: “Let’s get discussion started” still shows after importing posts

(James Cook) #14

Just a heads up for anyone else using this to do a site migration, this won’t transfer media. You will need to copy it from your existing uploads folder on your server to your new uploads folder.

(James Cook) #15

I was wondering how trivial it would be to also transfer across user site stats (likes, bookmarks etc) and private messages. A few of my regulars have complained that since I did the site migration they’ve lost access to that stuff. I’m sure they built up quite a collection over the two years.

I don’t mind giving it a go myself if somebody can point me in the right direction.


(Stefano Costa) #16

Could this reply be incorporated in the main topic post since it provides crucial howto steps that are missing from the original?

(Neil Lalonde) #17

Good idea. I linked to his post from near the beginning of the first post.

(Matthew Gaudet) #18

One heads up, it’s not clear if this still works; My recent attempt to migrate media along with a category hasn’t been successful as of yet.

1 Like
(Yvonne Brill) #19

Hi James, I’m a community manager, not a developer but I’m doing some research on ways to export topics from a category and display them in an Excel spreadsheet or similar. Would your instructions allow that, or is your solution for things like migrating the information to another site/platform?

We want to use our community to gather information/ideas and then export it out to allow us to group the data into different themes and further inform some work we’re doing internally.

(Jay Pfaffman) #21

The data Explorer plugin will let you export any data in discourse to a csv.