Skip to content

Instantly share code, notes, and snippets.

@krisanalfa
Created February 20, 2014 03:45
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 krisanalfa/9106753 to your computer and use it in GitHub Desktop.
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.
#!/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