Skip to content

Instantly share code, notes, and snippets.

@folkevil
Forked from nhoening/bongo.sh
Created August 29, 2016 10:20
Show Gist options
  • Save folkevil/c4a15c42017c5855572b52f6d826514f to your computer and use it in GitHub Desktop.
Save folkevil/c4a15c42017c5855572b52f6d826514f to your computer and use it in GitHub Desktop.

Revisions

  1. @nhoening nhoening revised this gist Aug 19, 2014. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions bongo.sh
    Original file line number Diff line number Diff line change
    @@ -16,10 +16,11 @@ usage()
    -H Mongo host string (ex. localhost:27017)
    -q Mongo query for export (JSON string)
    -L Limit exported results to this amount
    -d Show (debug) error messages in console
    EOF
    }

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

    case $opt in
    @@ -128,7 +129,7 @@ else
    # Support this native way here as default. Reintroduce the --jsonArray option in that case, probably.
    done

    pushd ../tmp 2>$DEBUG
    pushd .. 2>$DEBUG
    tar -czf "$DB.tar.gz" $DB 2>$DEBUG
    popd 2>$DEBUG
    popd 2>$DEBUG
  2. @nhoening nhoening revised this gist Aug 19, 2014. 1 changed file with 40 additions and 26 deletions.
    66 changes: 40 additions & 26 deletions bongo.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    #!/bin/bash

    LOADING=false

    DEBUG=/dev/null

    usage()
    {
    cat << EOF
    @@ -29,6 +30,9 @@ while getopts "hlu:p:H:q:L:" opt; do
    l)
    LOADING=true
    ;;
    d)
    DEBUG=/dev/stderr
    ;;
    u)
    USERNAME="$OPTARG"
    ;;
    @@ -74,38 +78,48 @@ 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 "**** Query: $QUERY"
    echo "**** Limit: $LIMIT"
    echo "*****************************************************************"

    if $LOADING ; then
    echo "Loading into $CONN"
    tar -xzf $DB.tar.gz
    pushd $DB >/dev/null
    echo "*************************** Mongo Import ************************"
    echo "**** Host: $HOST"
    echo "**** Database: $DB"
    echo "**** Username: $USERNAME"
    echo "**** Password: $PASSWORD"
    echo "*****************************************************************"

    if [ ! -f $DB.tar.gz ]; then
    echo "Archive $DB.tar.gz to import from could not be found! Aborting ..."
    exit
    fi

    tar -xzf $DB.tar.gz 2>$DEBUG
    pushd $DB 2>$DEBUG

    for path in *.json; do
    collection=${path%.json}
    echo "Loading into $DB/$collection from $path"
    mongoimport --host $HOST $ARGS -d $DB -c $collection $path --jsonArray
    done

    popd >/dev/null
    rm -rf $DB
    popd 2>$DEBUG
    rm -rf $DB 2>$DEBUG
    else
    echo "*************************** Mongo Export ************************"
    echo "**** Host: $HOST"
    echo "**** Database: $DB"
    echo "**** Username: $USERNAME"
    echo "**** Password: $PASSWORD"
    echo "**** Query: $QUERY"
    echo "**** Limit: $LIMIT"
    echo "*****************************************************************"

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

    mkdir /tmp/$DB
    pushd /tmp/$DB 2>/dev/null
    mkdir -p tmp/$DB 2>$DEBUG
    pushd tmp/$DB 2>$DEBUG

    for collection in $DATABASE_COLLECTIONS; do
    echo "--host $HOST $ARGS -db $DB -c $collection -q '$QUERY' -o $collection.json >/dev/null"
    mongoexport --host $HOST $ARGS -db $DB -c $collection -q "$QUERY" -o $collection.json >/dev/null
    echo "Exporting collection $collection ..."
    mongoexport --host $HOST $ARGS -db $DB -c $collection -q "$QUERY" -o $collection.json >$DEBUG
    if [ "$LIMIT" != "" ]; then
    head -n $LIMIT $collection.json > $collection.json.tmp && mv $collection.json.tmp $collection.json
    echo "limited result set to $LIMIT records"
    @@ -114,10 +128,10 @@ else
    # Support this native way here as default. Reintroduce the --jsonArray option in that case, probably.
    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
    pushd ../tmp 2>$DEBUG
    tar -czf "$DB.tar.gz" $DB 2>$DEBUG
    popd 2>$DEBUG
    popd 2>$DEBUG
    mv tmp/$DB.tar.gz ./ 2>$DEBUG
    rm -rf tmp 2>$DEBUG
    fi
  3. @nhoening nhoening revised this gist Aug 5, 2014. 1 changed file with 18 additions and 7 deletions.
    25 changes: 18 additions & 7 deletions bongo.sh
    Original file line number Diff line number Diff line change
    @@ -14,10 +14,11 @@ usage()
    -p Mongo password
    -H Mongo host string (ex. localhost:27017)
    -q Mongo query for export (JSON string)
    -L Limit exported results to this amount
    EOF
    }

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

    case $opt in
    @@ -40,6 +41,9 @@ while getopts "hlu:p:H:q:" opt; do
    q)
    QUERY="$OPTARG"
    ;;
    L)
    LIMIT="$OPTARG"
    ;;
    \?)
    echo "Invalid option $opt"
    exit 1
    @@ -76,7 +80,8 @@ echo "**** Database: $DB"
    echo "**** Username: $USERNAME"
    echo "**** Password: $PASSWORD"
    echo "**** Loading: $LOADING"
    echo "**** Query: $QUERY"
    echo "**** Query: $QUERY"
    echo "**** Limit: $LIMIT"
    echo "*****************************************************************"

    if $LOADING ; then
    @@ -99,14 +104,20 @@ else
    pushd /tmp/$DB 2>/dev/null

    for collection in $DATABASE_COLLECTIONS; do
    echo "--host $HOST $ARGS -db $DB -c $collection --jsonArray -q '$QUERY' -o $collection.json >/dev/null"
    mongoexport --host $HOST $ARGS -db $DB -c $collection --jsonArray -q "$QUERY" -o $collection.json /dev/null
    echo "--host $HOST $ARGS -db $DB -c $collection -q '$QUERY' -o $collection.json >/dev/null"
    mongoexport --host $HOST $ARGS -db $DB -c $collection -q "$QUERY" -o $collection.json >/dev/null
    if [ "$LIMIT" != "" ]; then
    head -n $LIMIT $collection.json > $collection.json.tmp && mv $collection.json.tmp $collection.json
    echo "limited result set to $LIMIT records"
    fi
    # TODO: For 2.6, mongoexport supports a limit argument (https://jira.mongodb.org/browse/SERVER-2033).
    # Support this native way here as default. Reintroduce the --jsonArray option in that case, probably.
    done

    pushd /tmp #2>/dev/null
    tar -czf "$DB.tar.gz" $DB 2>/dev/null
    popd #2>/dev/null
    popd #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
    rm -rf /tmp/$DB 2>/dev/null
    fi
  4. @nhoening nhoening revised this gist Jul 30, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions bongo.sh
    Original file line number Diff line number Diff line change
    @@ -96,17 +96,17 @@ else
    DATABASE_COLLECTIONS=$(mongo $CONN $ARGS --quiet --eval 'db.getCollectionNames()' | sed 's/,/ /g')

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

    for collection in $DATABASE_COLLECTIONS; do
    echo "--host $HOST $ARGS -db $DB -c $collection --jsonArray -q '$QUERY' -o $collection.json >/dev/null"
    mongoexport --host $HOST $ARGS -db $DB -c $collection --jsonArray -q "$QUERY" -o $collection.json #>/dev/null
    mongoexport --host $HOST $ARGS -db $DB -c $collection --jsonArray -q "$QUERY" -o $collection.json /dev/null
    done

    pushd /tmp #2>/dev/null
    tar -czf "$DB.tar.gz" $DB #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
    mv /tmp/$DB.tar.gz ./ 2>/dev/null
    rm -rf /tmp/$DB #2>/dev/null
    fi
  5. @nhoening nhoening revised this gist Jul 30, 2014. 1 changed file with 17 additions and 9 deletions.
    26 changes: 17 additions & 9 deletions bongo.sh
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    #!/bin/bash

    LOADING=false

    usage()
    @@ -11,10 +13,11 @@ usage()
    -u Mongo username
    -p Mongo password
    -H Mongo host string (ex. localhost:27017)
    -q Mongo query for export (JSON string)
    EOF
    }

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

    case $opt in
    @@ -34,6 +37,9 @@ while getopts "hlu:p:H:" opt; do
    H)
    HOST="$OPTARG"
    ;;
    q)
    QUERY="$OPTARG"
    ;;
    \?)
    echo "Invalid option $opt"
    exit 1
    @@ -70,6 +76,7 @@ echo "**** Database: $DB"
    echo "**** Username: $USERNAME"
    echo "**** Password: $PASSWORD"
    echo "**** Loading: $LOADING"
    echo "**** Query: $QUERY"
    echo "*****************************************************************"

    if $LOADING ; then
    @@ -89,16 +96,17 @@ else
    DATABASE_COLLECTIONS=$(mongo $CONN $ARGS --quiet --eval 'db.getCollectionNames()' | sed 's/,/ /g')

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

    for collection in $DATABASE_COLLECTIONS; do
    mongoexport --host $HOST $ARGS -db $DB -c $collection --jsonArray -o $collection.json >/dev/null
    echo "--host $HOST $ARGS -db $DB -c $collection --jsonArray -q '$QUERY' -o $collection.json >/dev/null"
    mongoexport --host $HOST $ARGS -db $DB -c $collection --jsonArray -q "$QUERY" -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
    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
  6. @francoisTemasys 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"
  7. @francoisTemasys 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

  8. @francoisTemasys 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

  9. @francoisTemasys 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
  10. @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
  11. @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
  12. @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