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
- Enable
Recovery Modein theRepairsection. This pauses the app while you import data.
- Open a Web Terminal using the Terminal button in the
Consolesection.
- Upload the dump file using the
Upload to /tmpbutton.
- 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
...
- 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({}) })'
- Import the dump using the
mongorestorecommand:
# 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/
- Click
Disable Recovery Modein theRepairsection to restart the app:
Verify
Verify the import:
- Click the
MongoDBbutton at the top of the terminal to paste the connection command. - Press Enter to access the MongoDB shell.
