Skip to content

Instantly share code, notes, and snippets.

@francoisTemasys
Forked from smashew/bongo.sh
Last active November 5, 2018 12:12
Show Gist options
  • Save francoisTemasys/9634471 to your computer and use it in GitHub Desktop.
Save francoisTemasys/9634471 to your computer and use it in GitHub Desktop.

Revisions

  1. francoisTemasys revised this gist Mar 31, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions bongo.sh
    Original file line number Diff line number Diff line change
    @@ -50,6 +50,7 @@ fi

    DB="$1"
    if [ -z "$HOST" ]; then
    HOST="localhost:27017"
    CONN="localhost:27017/$DB"
    else
    CONN="$HOST/$DB"
  2. francoisTemasys revised this gist Mar 31, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions bongo.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ LOADING=false
    usage()
    {
    cat << EOF
    usage: $0 <dbname> [options]
    usage: $0 [options] <DBNAME>
    OPTIONS:
    -h Show this help.
    @@ -44,7 +44,7 @@ done
    shift $(($MAXOPTIND-1))

    if [ -z "$1" ]; then
    echo "Usage: $0 <dbname> [opts]"
    echo "Usage: $0 [options] <DBNAME>"
    exit 1
    fi

  3. francoisTemasys revised this gist Mar 31, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions bongo.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ LOADING=false
    usage()
    {
    cat << EOF
    usage: $0 [options] dbname
    usage: $0 <dbname> [options]
    OPTIONS:
    -h Show this help.
    @@ -44,7 +44,7 @@ done
    shift $(($MAXOPTIND-1))

    if [ -z "$1" ]; then
    echo "Usage: export-mongo [opts] <dbname>"
    echo "Usage: $0 <dbname> [opts]"
    exit 1
    fi

  4. francoisTemasys revised this gist Mar 19, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions bongo.sh
    Original file line number Diff line number Diff line change
    @@ -79,7 +79,7 @@ if $LOADING ; then
    for path in *.json; do
    collection=${path%.json}
    echo "Loading into $DB/$collection from $path"
    mongoimport $ARGS -d $DB -c $collection $path
    mongoimport --host $HOST $ARGS -d $DB -c $collection $path --jsonArray
    done

    popd >/dev/null
    @@ -91,7 +91,7 @@ else
    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
    mongoexport --host $HOST $ARGS -db $DB -c $collection --jsonArray -o $collection.json >/dev/null
    done

    pushd /tmp 2>/dev/null
  5. @smashew smashew revised this gist Jul 31, 2013. 1 changed file with 32 additions and 27 deletions.
    59 changes: 32 additions & 27 deletions bongo.sh
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,10 @@
    #!/bin/bash

    LOADING=false

    usage()
    {
    cat << EOF
    usage: $0 [options] dbname
    OPTIONS:
    -h Show this help.
    -l Load instead of export
    @@ -15,9 +13,10 @@ usage()
    -H Mongo host string (ex. localhost:27017)
    EOF
    }

    while getopts "hlu:p:H:" opt; do
    MAXOPTIND=$OPTIND

    case $opt in
    h)
    usage
    @@ -27,13 +26,13 @@ while getopts "hlu:p:H:" opt; do
    LOADING=true
    ;;
    u)
    USERNAME="$opt"
    USERNAME="$OPTARG"
    ;;
    p)
    PASSWORD="$opt"
    PASSWORD="$OPTARG"
    ;;
    H)
    HOST="$opt"
    HOST="$OPTARG"
    ;;
    \?)
    echo "Invalid option $opt"
    @@ -43,56 +42,62 @@ while getopts "hlu:p:H:" opt; do
    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\""
    ARGS="-u $USERNAME"
    fi
    if [ -n "$PASSWORD" ]; then
    ARGS="$ARGS -p \"$PASSWORD\""
    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
    echo "Exporting from $CONN"
    COLLECTIONS=$(mongo $CONN $ARGS --quiet --eval "db.getCollectionNames()" | sed 's/,/ /g')

    DATABASE_COLLECTIONS=$(mongo $CONN $ARGS --quiet --eval 'db.getCollectionNames()' | sed 's/,/ /g')

    mkdir /tmp/$DB
    pushd /tmp/$DB 2>/dev/null

    for collection in $COLLECTIONS; do
    echo "Exporting $DB/$collection ..."
    mongoexport $ARGS -d $DB -c $collection -o $collection.json

    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
    tar -czf "$DB.tar.gz" $DB 2>/dev/null
    popd 2>/dev/null
    popd 2>/dev/null
    mv /tmp/$DB.tar.gz ./
    rm -rf /tmp/$DB
    fi
    mv /tmp/$DB.tar.gz ./ 2>/dev/null
    rm -rf /tmp/$DB 2>/dev/null
    fi
  6. @jmoiron jmoiron revised this gist Jul 21, 2012. 1 changed file with 9 additions and 6 deletions.
    15 changes: 9 additions & 6 deletions bongo.sh
    Original file line number Diff line number Diff line change
    @@ -64,7 +64,7 @@ if [ -n "$PASSWORD" ]; then
    ARGS="$ARGS -p \"$PASSWORD\""
    fi

    if [ LOADING ]; then
    if $LOADING ; then
    echo "Loading into $CONN"
    tar -xzf $DB.tar.gz
    pushd $DB >/dev/null
    @@ -81,15 +81,18 @@ else
    echo "Exporting from $CONN"
    COLLECTIONS=$(mongo $CONN $ARGS --quiet --eval "db.getCollectionNames()" | sed 's/,/ /g')

    mkdir $DB
    pushd $DB >/dev/null
    mkdir /tmp/$DB
    pushd /tmp/$DB 2>/dev/null

    for collection in $COLLECTIONS; do
    echo "Exporting $DB/$collection ..."
    mongoexport $ARGS -d $DB -c $collection -o $collection.json
    done

    popd >/dev/null
    pushd /tmp 2>/dev/null
    tar -czf "$DB.tar.gz" $DB
    rm -rf $DB
    fi
    popd 2>/dev/null
    popd 2>/dev/null
    mv /tmp/$DB.tar.gz ./
    rm -rf /tmp/$DB
    fi
  7. @jmoiron jmoiron created this gist Jun 30, 2012.
    95 changes: 95 additions & 0 deletions bongo.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    #!/bin/bash

    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="$opt"
    ;;
    p)
    PASSWORD="$opt"
    ;;
    H)
    HOST="$opt"
    ;;
    \?)
    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

    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
    echo "Exporting from $CONN"
    COLLECTIONS=$(mongo $CONN $ARGS --quiet --eval "db.getCollectionNames()" | sed 's/,/ /g')

    mkdir $DB
    pushd $DB >/dev/null

    for collection in $COLLECTIONS; do
    echo "Exporting $DB/$collection ..."
    mongoexport $ARGS -d $DB -c $collection -o $collection.json
    done

    popd >/dev/null
    tar -czf "$DB.tar.gz" $DB
    rm -rf $DB
    fi