Skip to main content

Import MongoDB

Overview

Export a MongoDB database from your current setup and import it into a Cloudron app.

Export

Create a dump of your existing MongoDB database:

$ mongodump -h host:port -u mongouser -p mongopassword --db databasename --out /tmp
$ tar -C /tmp -zcvf /tmp/mongodump.tar.gz databasename

If the MongoDB server is on Cloudron, you can export it using this command line on the Web Terminal:

# mongodump -u "${CLOUDRON_MONGODB_USERNAME}" -p "${CLOUDRON_MONGODB_PASSWORD}" -h ${CLOUDRON_MONGODB_HOST}:${CLOUDRON_MONGODB_PORT} --db ${CLOUDRON_MONGODB_DATABASE} --out /tmp

# tar -C /tmp -zcvf /tmp/mongodump.tar.gz ${CLOUDRON_MONGODB_DATABASE}

Import

  1. Enable Recovery Mode in the Repair section. This pauses the app while you import data.
  1. Open a Web Terminal using the Terminal button in the Console section.
  1. Upload the dump file using the Upload to /tmp button.
  1. Extract the dump:
root@e6bb147e-9f9c-4d17-a9af-65d9fbb0dd72:/tmp# tar -zxvf mongodump.tar.gz
e6bb147e-9f9c-4d17-a9af-65d9fbb0dd72/
e6bb147e-9f9c-4d17-a9af-65d9fbb0dd72/rocketchat_custom_sounds.metadata.json
e6bb147e-9f9c-4d17-a9af-65d9fbb0dd72/users.metadata.json
e6bb147e-9f9c-4d17-a9af-65d9fbb0dd72/rocketchat_custom_sounds.bson
...
  1. Clear the existing database:
# mongosh -u "${CLOUDRON_MONGODB_USERNAME}" -p "${CLOUDRON_MONGODB_PASSWORD}" ${CLOUDRON_MONGODB_HOST}:${CLOUDRON_MONGODB_PORT}/${CLOUDRON_MONGODB_DATABASE} --eval 'db.getCollectionNames().forEach(function (col) { db.getCollection(col).deleteMany({}) })'
  1. Import the dump using the mongorestore command:
# mongorestore -u "${CLOUDRON_MONGODB_USERNAME}" -p "${CLOUDRON_MONGODB_PASSWORD}" -h ${CLOUDRON_MONGODB_HOST}:${CLOUDRON_MONGODB_PORT} --db ${CLOUDRON_MONGODB_DATABASE} --dir=/tmp/e6bb147e-9f9c-4d17-a9af-65d9fbb0dd72/
  1. Click Disable Recovery Mode in the Repair section to restart the app:

Verify

Verify the import:

  1. Click the MongoDB button at the top of the terminal to paste the connection command.
  2. Press Enter to access the MongoDB shell.