Skip to content

Instantly share code, notes, and snippets.

@wilspi
Last active January 24, 2018 09:54
Show Gist options
  • Save wilspi/dce3fcfc229ab4149fe31e01c9a14b43 to your computer and use it in GitHub Desktop.
Save wilspi/dce3fcfc229ab4149fe31e01c9a14b43 to your computer and use it in GitHub Desktop.
Dumps given collections from source mongo db to destination mongo db
# Dumps given collections from source mongo db to destination mongo db
# Source DB Creds
source_host=""
source_port=""
source_user=""
source_password=""
source_db=""
# Destination DB Creds
destination_host=""
destination_port=""
destination_user=""
destination_password=""
destination_db=""
# Collections to dump
collections=( "collection1" "collection2")
for c in ${collections[@]}
do
mongodump --host $source_host --port $source_port -u $source_user -p $source_password --authenticationDatabase admin --db $source_db --collection $c --out - | gzip > dump_$c_`date "+%Y-%m-%d"`.gz
gunzip -c dump_$c_`date "+%Y-%m-%d"`.gz > $c.bson
mongorestore --host $destination_host --port $destination_port -u $destination_user -p $destination_password --authenticationDatabase admin --db $destination_db $c.bson
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment