Last Updated: February 25, 2016
·
1.092K
· wendel

Moving your Heroku app with MongoLabs to another region

So, you made all these awesome apps and deployed them to Heroku. In all of Herokus awesomeness and ease of use, you forgot to specify the Heroku region your app should live in. Or, your app is old and was made before Heroku opened their European region. The latency diff between regions can be quite alot, so you'd want to move your app into the region closest to your users. This post explains the process of moving your app to Europe (or any other region).

Heroku actually doesn't support moving apps - the trick is to create a fork of the app, a duplicate if you will, and then specify the correct region upon creation. The command is as follows:

heroku fork -a sourceapp targetapp --region eu

This is the easy part. Heroku has now created a duplicate app and set it up in a nice, new apartment in the specified region. The problem is that all your MongoLabs data was left in their cardboard boxes in the old flat. The solution is to use the mongodb tools mongodump and mongorestore to first dump the contents of the DB to disk as .json and .bson-files and then restore them in the new database.

First off we need to attach the MongoLab addon to your new app:

heroku addons:add mongolab

Then we dump the contents of the database associated with your old app to drive using the following command:

mongodump -h <mongoDB-address>:<port> -d <app-name> -u <user> -p <password> -o <output directory>

This should look something like this:

mongodump -h ds057528.mongolab.com:57528 -d heroku_app20500089 -u myuser -p mypassword -o mongolab-backup

The final step of the process includes loading up the new MongoLab instance associated with your newly created app with this data:

mongorestore -h <mongoDB-address>:<port> -d <app-name> -u <user> -p <password> <input db directory>

This may look something like this:

mongorestore -h ds057528.mongolab.com:57528 -d heroku_app20500089 -u myuser -p mypassword mongolab-backup

Voila!

Enjoy your new app.