🌱 planting ideas 🌱

Sync Mongo data from prod to dev machine via ssh

With MongoDB 4.0 and earlier there db.copyDatabase() and db.cloneDatabase().
Stackoverflow script available on the link below for MongoDB 4.0 and earlier:

https://stackoverflow.com/questions/16619598/sync-mongodb-via-ssh
https://sheharyar.me/blog/sync-mongodb-local-and-production-heroku/

For MongoDB >4.0 mongo-sync module doesn’t seem to work. I had a mongodb
instance on AWS EC2 and needed to sync the data to debug the issue and
replicate in dev environment. Below is a command which will sync mongodb from
aws ec2. Below command does the following:


1 . connect to aws instance using keypair pem file
2 . use mongodump to export the data from server
3 . pipe the data to local machine
4 . restore the data in local machine

1
2
3
4
5
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
1
2
var text = "Hello World";
console.log("text");
1
2
var text = "Hello World";
console.log("text");