Skip to content

Instantly share code, notes, and snippets.

@jcppman
Created July 28, 2014 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcppman/bca37e081664c7b51c3d to your computer and use it in GitHub Desktop.
Save jcppman/bca37e081664c7b51c3d to your computer and use it in GitHub Desktop.
copyMongo
#!/usr/local/bin/fish
function tips
set_color red
echo $argv[1]
set_color normal
end
set SOURCE $argv[1]
set TARGET $argv[2]
set DBNAME $argv[3]
# Drop target Db
tips "Drop Database $TARGET/$DBNAME"
set RESULT (mongo $TARGET/$DBNAME --eval "db.dropDatabase()")
# Get all collection
set COLLECTIONS (mongo $SOURCE/$DBNAME --quiet --eval "print(db.getCollectionNames().join('\n'))")
# Export & Import
for COLLECTION in $COLLECTIONS
tips "Copy Mongo Collection '$COLLECTION' from $SOURCE to $TARGET"
mongoexport -h $SOURCE -d $DBNAME -c $COLLECTION | mongoimport -h $TARGET -d $DBNAME -c $COLLECTION
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment