Sheharyar Naseer

Sync MongoDB data between Development and Production databases


For a recent project of mine, I had to constantly copy my local MongoDB Database to and from my Production Heroku Database. Running mongodump and mongorestore again and again with complicated flags to support remote urls quickly became a hassle. I had previously used heroku-mongo-sync, but it is now deprecated in favor of marcofognog’s fork. Unfortunately, neither of them worked for me.

So I decided to write my own. Meet mongo-sync:

mongo-sync demo gif

It’s a simple shell script that allows you to sync your local and remote MongoDB databases, using two commands; push and pull. Start by cloning the script on your machine:

git clone https://github.com/sheharyarn/mongo-sync.git
cd mongo-sync

Edit the provided config.yml with your db details:

local:
  db: 'local_db_name'

remote:
  db: 'remote_db_name'
  host:
    url: 'some.remoteurl.com'
    port: 27017
  access:
    username: 'remote_mongo_user'
    password: 'remote_mongo_pass'

# For Heroku MongoDB URLs, here's the legend:
# mongodb://username:password@hosturl.com:port/db_name

Now, you can start syncing your databases:

./mongo-sync push       # Push DB to Remote
./mongo-sync pull       # Pull DB to Local

I’m planning to convert it into a Ruby Gem and a Heroku Plugin as well.

Github: sheharyarn / mongo-sync

Notes

  • The push and pull commands overwrite the target DB
  • If you include this script into your project, it’s a good idea to add config.yml to your .gitignore

Further Reading