Skip to content

Instantly share code, notes, and snippets.

@smashew

smashew/bongo.sh

Forked from jmoiron/bongo.sh
Last active Feb 16, 2018
Embed
What would you like to do?
This one works... Tested
LOADING=false
usage()
{
cat << EOF
usage: $0 [options] dbname
OPTIONS:
-h Show this help.
-l Load instead of export
-u Mongo username
-p Mongo password
-H Mongo host string (ex. localhost:27017)
EOF
}
while getopts "hlu:p:H:" opt; do
MAXOPTIND=$OPTIND
case $opt in
h)
usage
exit
;;
l)
LOADING=true
;;
u)
USERNAME="$OPTARG"
;;
p)
PASSWORD="$OPTARG"
;;
H)
HOST="$OPTARG"
;;
\?)
echo "Invalid option $opt"
exit 1
;;
esac
done
shift $(($MAXOPTIND-1))
if [ -z "$1" ]; then
echo "Usage: export-mongo [opts] <dbname>"
exit 1
fi
DB="$1"
if [ -z "$HOST" ]; then
CONN="localhost:27017/$DB"
else
CONN="$HOST/$DB"
fi
ARGS=""
if [ -n "$USERNAME" ]; then
ARGS="-u $USERNAME"
fi
if [ -n "$PASSWORD" ]; then
ARGS="$ARGS -p $PASSWORD"
fi
echo "*************************** Mongo Export ************************"
echo "**** Host: $HOST"
echo "**** Database: $DB"
echo "**** Username: $USERNAME"
echo "**** Password: $PASSWORD"
echo "**** Loading: $LOADING"
echo "*****************************************************************"
if $LOADING ; then
echo "Loading into $CONN"
tar -xzf $DB.tar.gz
pushd $DB >/dev/null
for path in *.json; do
collection=${path%.json}
echo "Loading into $DB/$collection from $path"
mongoimport $ARGS -d $DB -c $collection $path
done
popd >/dev/null
rm -rf $DB
else
DATABASE_COLLECTIONS=$(mongo $CONN $ARGS --quiet --eval 'db.getCollectionNames()' | sed 's/,/ /g')
mkdir /tmp/$DB
pushd /tmp/$DB 2>/dev/null
for collection in $DATABASE_COLLECTIONS; do
mongoexport --host $HOST -u $USERNAME -p $PASSWORD -db $DB -c $collection --jsonArray -o $collection.json >/dev/null
done
pushd /tmp 2>/dev/null
tar -czf "$DB.tar.gz" $DB 2>/dev/null
popd 2>/dev/null
popd 2>/dev/null
mv /tmp/$DB.tar.gz ./ 2>/dev/null
rm -rf /tmp/$DB 2>/dev/null
fi
@TomA-R

This comment has been minimized.

Copy link

@TomA-R TomA-R commented Oct 9, 2013

In line 94, why not use $ARGS instead of -u $USERNAME -p $PASSWORD? When connecting without username and password, it breaks..

@mze3e

This comment has been minimized.

Copy link

@mze3e mze3e commented Dec 22, 2013

Hey... sorry for being very lame. how do i use this?

@francoisTemasys

This comment has been minimized.

Copy link

@francoisTemasys francoisTemasys commented Mar 19, 2014

Better here: https://gist.github.com/francoisTemasys/9634471
Line 94:

mongoexport --host $HOST $ARGS -db $DB -c $collection --jsonArray -o $collection.json >/dev/null

Line 82:

mongoimport --host $HOST $ARGS -d $DB -c $collection $path --jsonArray
@camlafit

This comment has been minimized.

Copy link

@camlafit camlafit commented Apr 14, 2017

Use an external targ.gz. Export can be any name or path

https://gist.github.com/camlafit/4690c2fa948f2e760d566875daa39a14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.