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()anddb.cloneDatabase()were removed in MongoDB 4.0+- The
mongo-syncmodule doesn’t work well with MongoDB >4.0 - The approach below works with any version by piping
mongodumpoutput directly over SSH
The command
1 | ssh -i PATH_TO_KEYPAIR.pem USERNAME@IP_ADDRESS \ |
What this does, step by step:
- SSH into your EC2 instance using your key pair
- Run
mongodumpon the server to export the database - Compress the dump with
tar -zcand stream it back through the pipe tar -zxdecompresses it locallymongorestore dumpimports it into your local MongoDB- Clean up the local dump folder