Skip to content

Instantly share code, notes, and snippets.

@hoangdh
Last active November 26, 2024 03:44
Show Gist options
  • Save hoangdh/2fd1860c17b1dd75cbcdd2edc5961d4d to your computer and use it in GitHub Desktop.
Save hoangdh/2fd1860c17b1dd75cbcdd2edc5961d4d to your computer and use it in GitHub Desktop.
Migrate data MongoDB to another cluster without dumping to local disk

Migrate data MongoDB to another 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 Nov 26, 2024

Migrate each collection per DB to new replica set

Note: Fill your information

#!/bin/bash

DB_NAME="datamining_report"
CLUSTER_SRC="mongodb://admin:[email protected]:27017,10.10.1.21:27017,10.10.1.22:27017/${DB_NAME}?readPreference=secondary"

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

mongosh --quiet "${CLUSTER_SRC}" --authenticationDatabase=admin --eval "db.getCollectionNames().join('\n')" | while read col; 
do 
  echo "Clone collection ${col} on ${DB_NAME}"
  mongodump --uri "${CLUSTER_SRC}" --authenticationDatabase=admin --db=${DB_NAME} --collection=${col}  --archive | \
  mongorestore --uri "${CLUSTER_DEST}" --authenticationDatabase=admin --archive --nsFrom="${DB_NAME}.*" --nsTo="${DB_NAME}.*";
done

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