Skip to content

Instantly share code, notes, and snippets.

@SammyEnigma
Forked from stanislavb/mongoconsole.sh
Created November 25, 2020 09:39
Show Gist options
  • Save SammyEnigma/0b8c5a13163ced886a1f6cc1300d096d to your computer and use it in GitHub Desktop.
Save SammyEnigma/0b8c5a13163ced886a1f6cc1300d096d to your computer and use it in GitHub Desktop.

Revisions

  1. @stanislavb stanislavb revised this gist Apr 14, 2016. 3 changed files with 5 additions and 5 deletions.
    6 changes: 3 additions & 3 deletions mongoconsole.sh
    Original file line number Diff line number Diff line change
    @@ -30,8 +30,8 @@ fi
    # -t open TTY
    # --rm remove container after use
    # --name name of our temporary container
    # --link map server container to hostname "mongo" without client container
    # --link map server container to hostname within client container
    # mongo use official mongo image
    # command line to run:
    # mongo --host mongo
    docker run -it --rm --name mongoconsole --link ${CONTAINERNAME}:mongo mongo mongo --host mongo
    # mongo --host <hostname>
    docker run -it --rm --name mongoconsole --link ${CONTAINERNAME}:${CONTAINERNAME} mongo mongo --host ${CONTAINERNAME}
    2 changes: 1 addition & 1 deletion mongodump.sh
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ if [ -z "${DBNAME}" -o -z "${CONTAINERNAME}" ]; then
    exit 1
    fi

    docker run -it --name mongodump --link ${CONTAINERNAME}:mongo mongo mongodump --db ${DBNAME} --host mongo
    docker run -it --name mongodump --link ${CONTAINERNAME}:${CONTAINERNAME} mongo mongodump --db ${DBNAME} --host ${CONTAINERNAME}
    docker cp mongodump:dump ./mongodump-$(date --iso-8601)
    if [ $? -ne 0 ]; then
    echo "MongoDB did not succeed taking a backup. Did you specify the correct database name?"
    2 changes: 1 addition & 1 deletion mongorestore.sh
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ echo "Attempting to restore mongodb dump at ${DUMPDIR} into container ${CONTAINE
    read -r -p "Is this what you want? [y/N] " response
    case $response in
    [yY][eE][sS]|[yY])
    docker run -it --rm --name mongorestore -v ${DUMPDIR}:/var/dump --link ${CONTAINERNAME}:mongo mongo mongorestore --host mongo /var/dump
    docker run -it --rm --name mongorestore -v ${DUMPDIR}:/var/dump --link ${CONTAINERNAME}:${CONTAINERNAME} mongo mongorestore --host ${CONTAINERNAME} /var/dump
    ;;
    *)
    echo "Nevermind then"
  2. @stanislavb stanislavb revised this gist Apr 14, 2016. 1 changed file with 37 additions and 0 deletions.
    37 changes: 37 additions & 0 deletions mongoconsole.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/bin/bash

    usage() {
    echo "Usage $0 -c mongo_docker_container_name"
    }

    while [[ $# > 1 ]]
    do
    key="$1"

    case $key in
    -c|--container)
    CONTAINERNAME="$2"
    shift # past argument
    ;;
    *)
    usage
    exit 1
    ;;
    esac
    shift # past argument or value
    done

    if [ -z "${CONTAINERNAME}" ]; then
    usage
    exit 1
    fi

    # -i interactive
    # -t open TTY
    # --rm remove container after use
    # --name name of our temporary container
    # --link map server container to hostname "mongo" without client container
    # mongo use official mongo image
    # command line to run:
    # mongo --host mongo
    docker run -it --rm --name mongoconsole --link ${CONTAINERNAME}:mongo mongo mongo --host mongo
  3. @stanislavb stanislavb revised this gist Apr 14, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions mongodump.sh
    Original file line number Diff line number Diff line change
    @@ -32,4 +32,8 @@ fi

    docker run -it --name mongodump --link ${CONTAINERNAME}:mongo mongo mongodump --db ${DBNAME} --host mongo
    docker cp mongodump:dump ./mongodump-$(date --iso-8601)
    if [ $? -ne 0 ]; then
    echo "MongoDB did not succeed taking a backup. Did you specify the correct database name?"
    fi
    # Clean up container
    docker rm mongodump
  4. @stanislavb stanislavb revised this gist Apr 14, 2016. 1 changed file with 35 additions and 0 deletions.
    35 changes: 35 additions & 0 deletions mongodump.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!/bin/bash

    usage() {
    echo "Usage $0 -c mongo_docker_container_name -db db_name"
    }

    while [[ $# > 1 ]]
    do
    key="$1"

    case $key in
    -db|--database)
    DBNAME="$2"
    shift # past argument
    ;;
    -c|--container)
    CONTAINERNAME="$2"
    shift # past argument
    ;;
    *)
    usage
    exit 1
    ;;
    esac
    shift # past argument or value
    done

    if [ -z "${DBNAME}" -o -z "${CONTAINERNAME}" ]; then
    usage
    exit 1
    fi

    docker run -it --name mongodump --link ${CONTAINERNAME}:mongo mongo mongodump --db ${DBNAME} --host mongo
    docker cp mongodump:dump ./mongodump-$(date --iso-8601)
    docker rm mongodump
  5. @stanislavb stanislavb revised this gist Sep 14, 2015. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions mongorestore.sh
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,7 @@
    # Use > 1 to consume two arguments per pass in the loop (e.g. each
    # argument has a corresponding value to go with it).
    # Use > 0 to consume one or more arguments per pass in the loop (e.g.
    # some arguments don't have a corresponding value to go with it such
    # as in the --default example).
    # note: if this is set to > 0 the /etc/hosts part is not recognized ( may be a bug )
    # some arguments don't have a corresponding value to go with it)

    usage() {
    echo "Usage $0 -d /mongodump/dir -c mongo_docker_container_name"
  6. @stanislavb stanislavb created this gist Sep 14, 2015.
    48 changes: 48 additions & 0 deletions mongorestore.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #!/bin/bash
    # Use > 1 to consume two arguments per pass in the loop (e.g. each
    # argument has a corresponding value to go with it).
    # Use > 0 to consume one or more arguments per pass in the loop (e.g.
    # some arguments don't have a corresponding value to go with it such
    # as in the --default example).
    # note: if this is set to > 0 the /etc/hosts part is not recognized ( may be a bug )

    usage() {
    echo "Usage $0 -d /mongodump/dir -c mongo_docker_container_name"
    }

    while [[ $# > 1 ]]
    do
    key="$1"

    case $key in
    -d|--dump)
    DUMPDIR="$2"
    shift # past argument
    ;;
    -c|--container)
    CONTAINERNAME="$2"
    shift # past argument
    ;;
    *)
    usage
    exit 1
    ;;
    esac
    shift # past argument or value
    done

    if [ -z "${DUMPDIR}" -o -z "${CONTAINERNAME}" ]; then
    usage
    exit 1
    fi

    echo "Attempting to restore mongodb dump at ${DUMPDIR} into container ${CONTAINERNAME}"
    read -r -p "Is this what you want? [y/N] " response
    case $response in
    [yY][eE][sS]|[yY])
    docker run -it --rm --name mongorestore -v ${DUMPDIR}:/var/dump --link ${CONTAINERNAME}:mongo mongo mongorestore --host mongo /var/dump
    ;;
    *)
    echo "Nevermind then"
    ;;
    esac