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. Sadly, 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:

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

Edit the provided config.yml with your db details:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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:[email protected]:port/db_name

Now, you can start syncing your databases:

1
2
./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