Created
February 20, 2014 03:45
-
-
Save krisanalfa/9106753 to your computer and use it in GitHub Desktop.
Export your Mongo database. Each collection has it's own .json file. So, we can import them later using this script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
exportDb() { | |
echo "Exporting $1" | |
echo "db.getCollectionNames()" | | |
mongo $1 2> /dev/null | | |
grep '"' | | |
cut -d '"' -f 2 | | |
while read a; do | |
if [[ $a != "system.indexes" ]]; then | |
mongoexport --db $1 --collection $a > $a.json; | |
fi | |
done | |
} | |
importDb() { | |
ls -l | | |
awk 'NF>2' | | |
awk '{print $9}' | | |
cut -d '.' -f 1 | | |
while read b; do | |
mongoimport --db $1 --collection $b --file $b.json ; | |
done | |
} | |
usage() { | |
echo "Usage: mongooo.sh [option] [dbName] | |
Option: | |
--export, -e Export the [dbName] | |
--import, -i Import the [dbName] | |
" | |
} | |
if [[ $2 == "" ]]; then | |
usage; | |
exit 1; | |
fi | |
case $1 in | |
--export|-e) exportDb $2;; | |
--import|-i) importDb $2;; | |
*) usage && exit 1;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment