Skip to content

Instantly share code, notes, and snippets.

@hoangdh
Last active January 10, 2026 04:30
Show Gist options
  • Select an option

  • Save hoangdh/2fd1860c17b1dd75cbcdd2edc5961d4d to your computer and use it in GitHub Desktop.

Select an option

Save hoangdh/2fd1860c17b1dd75cbcdd2edc5961d4d to your computer and use it in GitHub Desktop.
Migrate data MongoDB to other cluster without dumping to local disk

Migrate data MongoDB to a new cluster

  • Source cluster: 10.10.10.10
  • Destination cluster: 10.10.20.10
mongodump --host 10.10.10.10 --port 27017 --archive --numParallelCollections=10 | \
mongorestore --host 10.10.20.10 --port 27017 --archive --numParallelCollections=10 --drop

Note: Use --gzip to save bandwidth.

mongodump --host 10.10.10.10 --port 27017 --archive --gzip --numParallelCollections=10 | \
mongorestore --host 10.10.20.10 --port 27017 --archive --gzip --numParallelCollections=10 --drop
@hoangdh
Copy link
Author

hoangdh commented Jan 10, 2026

Migrate specify databases to other cluster.

dbs="db01 db02 db03"
CLUSTER_SRC="mongodb://admin:[email protected]:27017,10.10.10.13:27017"

# New cluster
CLUSTER_DEST="mongodb://admin:[email protected]:27017,10.20.1.14:27017"


for db in ${dbs}
do
  echo "Clone ${db}"
  mongodump --uri "${CLUSTER_SRC}" --numParallelCollections=10 --authenticationDatabase=admin --db=${db} --archive | \
  mongorestore --uri "${CLUSTER_DEST}" --numParallelCollections=10 --authenticationDatabase=admin --archive --nsFrom="${db}.*" --nsTo="${db}.*" --drop;
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment