Jun 04, 2020

Sync MongoDB data from production to dev machine via SSH

When you need to replicate a production MongoDB database locally to debug an issue, you can do it in one piped command over SSH — no intermediate file needed.

Context

  • db.copyDatabase() and db.cloneDatabase() were removed in MongoDB 4.0+
  • The mongo-sync module doesn’t work well with MongoDB >4.0
  • The approach below works with any version by piping mongodump output directly over SSH

The command

1
2
3
ssh -i PATH_TO_KEYPAIR.pem USERNAME@IP_ADDRESS \
'mongodump > /dev/null && tar -zc dump && rm -rf dump' \
| tar -zx && mongorestore dump && rm -rf dump

What this does, step by step:

  1. SSH into your EC2 instance using your key pair
  2. Run mongodump on the server to export the database
  3. Compress the dump with tar -zc and stream it back through the pipe
  4. tar -zx decompresses it locally
  5. mongorestore dump imports it into your local MongoDB
  6. Clean up the local dump folder

Reference