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 | ssh -i PATH_TO_KEYPAIR.pem |
1 | var text = "Hello World"; |
1 | var text = "Hello World"; |