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.
TEAM_QUERY='{"team_id":"'"$TEAM_ID"'"}'
ID=`mongo $DB --quiet --eval "db.teams.findOne($TEAM_QUERY)._id.valueOf()"`
Export team data.
mongoexport --db $DB -c teams --out $OUT/teams.dump --query=$TEAM_QUERY
Construct a query string with the MongoDB Object ID, an $oid
.
ID_QUERY='{"team_id":{"$oid":"'"$ID"'"}}'
Export all related collections.
for coll in challenges matches seasons users
do
mongoexport --db $DB -c $coll --out $OUT/$coll.dump --query=$ID_QUERY
done
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.
dokku mongo:expose app
This will randomly assign ports. You can run dokku mongo:info app
to see the exposed ports.
# dokku mongo:info app
=====> Container Information
...
Exposed ports: 27017->13450 27018->25362 27019->18545 28017->29405
Dsn: mongodb://username:password@dokku-mongo-app:27017/app
Status: running
Version: mongo:3.2.1
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.
Restore Data
mongoimport --host=... --port=13450 --username=username --password=password --db app -c teams teams.dump
Unexpose MongoDB
dokku mongo:unexpose app