mongodbherokumongohq

How to download production data from MongoHQ in Heroku to local machine?


I have a web application with production data running on a Cidar stack in Heroku. The production data is stored in a MongoDB and I use MongoHQ to manage it in production. I'd like to download the production data to my local machine so I can run my web app locally with production data for debugging purposes. I'm still relatively new to MongoDB, so after many attempts, I've had no success.

Is there a way to download MongoDB data (collections) from Heroku to a local MongoDB I have on my local machine?


Solution

  • I figured it out, thanks to this blog post: http://stevespiga.rel.li/mongodump-mongorestore.html

    Here a summary of the solution:

    1. I ran the command: heroku config | grep "MONGOHQ". This gave me output of the form:

    MONGOHQ_URL:mongodb://heroku:veryLongPasswordString@somewhere.mongohq.com:88888/app123456.

    1. This can be interpreted as:

    MONGOHQ_URL:mongodb://username:password@host:port/path

    1. Then I dumped the production db to a local directory by running:

    mongodump --db <path> --host <host> --port <port> --username <username> --password <password> --out <folder for dump>.

    1. Example:

    mongodump --db app123456 --host somewhere.mongohq.com --port 88888 --username heroku --password veryLongPasswordString --out ./testDump.

    1. The next step is to take the dumped data and restore it to your local database:

    mongorestore ./testdump.

    Note, this assumes that you do NOT have a local db of the same name as the dumped db before you restore. If need be you can rename the db by following the steps outlined in this stackoverflow post.

    I hope this helps!