I’ve been running a pet project using Dokku on DigitalOcean for a number of years with MongoDB, backing up data into Dropbox. See Running Slack Bots on DigitalOcean with Dokku and Backing up MongoDB Databases in Dokku Containers to Dropbox.
Today I had to selectively export data from a backup, and restore this data into a MongoDB running inside a Dokku container. This was a bit tedious.
Exporting a Subset of Data
I am dealing with slack-gamebot data, which contains a teams
collection with a row per team and related data in other collections, including users
and matches
. I wrote a bash script to export this data.
Construct a query string, eg. {"team_id":"ID"}
and fetch the _id
value for querying relationships.
Export team data.
Construct a query string with the MongoDB Object ID, an $oid
.
Export all related collections.
We now have files such as teams.dump
, users.dump
, etc.
Expose Containerized MongoDB
Expose the MongoDB in a container on the Dokku host so we can connect to it.
This will randomly assign ports. You can run dokku mongo:info app
to see the exposed ports.
Note the mapping for 27017
, mine is 13450
and the username and password.
Try connecting to the database directly with mongo
. Make sure the client version matches the version of MongoDB inside the container - I was getting confusing errors until I ran apt-get mongodb-org-shell
and apt-get mongodb-org-tools
to get a MongoDB 3.x client.