Skip to content

Instantly share code, notes, and snippets.

@janbaer
Last active August 19, 2022 15:00
Show Gist options
  • Save janbaer/e99e0f55f6652a0d0f178d2d5b94a4a4 to your computer and use it in GitHub Desktop.
Save janbaer/e99e0f55f6652a0d0f178d2d5b94a4a4 to your computer and use it in GitHub Desktop.

Revisions

  1. janbaer renamed this gist Aug 19, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. janbaer revised this gist Apr 16, 2021. 1 changed file with 10 additions and 5 deletions.
    15 changes: 10 additions & 5 deletions mongo-snasphot.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,10 @@
    #!/bin/bash

    if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root"
    exit 1
    fi

    if [ -z "${MONGO_PASSWORD}" ]; then
    echo "You need to set then environment variable MONGO_PASSWORD"
    exit 1
    @@ -12,8 +17,8 @@ if [ -z "${COMMAND}" ]; then
    exit 1
    fi

    MONGO_VOLUME="/dev/host-vg/mongodb"
    MONGO_SNAPSHOT_VOLUME="/dev/host-vg/mongodb_snapshot"
    MONGO_VOLUME="/dev/host-vg/mongo-data"
    MONGO_SNAPSHOT_VOLUME="/dev/host-vg/mongo-snapshot"
    MONGO_USER="administrator"
    MONGO_CMD="/usr/bin/mongo --username $MONGO_USER --password $MONGO_PASSWORD --authenticationDatabase admin --quiet"

    @@ -36,7 +41,7 @@ function create_snapshot() {
    echo "Database is locked for creating snapshot."
    fi

    lvcreate -L 10GB --snapshot -n mongodb_snapshot ${MONGO_VOLUME}
    lvcreate -L 10GB --snapshot -n mongo-snapshot ${MONGO_VOLUME}
    if [ $? -ne 0 ]; then
    print_error "Creation of snapshot failed"
    else
    @@ -46,9 +51,9 @@ function create_snapshot() {
    echo -en " Unlocking MongoDB. "
    $MONGO_CMD -eval "d=db.fsyncUnlock(); printjson(d);" >/dev/null 2>&1

    mkdir -p /mongo_snapshot
    mkdir -p /mongo-snapshot

    echo "You can mount the snapshot with mount -t xfs -o nouuid ${MONGO_SNAPSHOT_VOLUME} /mongo_snapshot"
    echo "You can mount the snapshot with mount -t xfs -o nouuid ${MONGO_SNAPSHOT_VOLUME} /mongo-snapshot"
    lvdisplay ${MONGO_SNAPSHOT_VOLUME}
    }

  3. janbaer renamed this gist Apr 12, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. janbaer created this gist Apr 12, 2021.
    79 changes: 79 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    #!/bin/bash

    if [ -z "${MONGO_PASSWORD}" ]; then
    echo "You need to set then environment variable MONGO_PASSWORD"
    exit 1
    fi

    COMMAND=${1}

    if [ -z "${COMMAND}" ]; then
    echo "Please pass one of the following commands: create, restore, remove."
    exit 1
    fi

    MONGO_VOLUME="/dev/host-vg/mongodb"
    MONGO_SNAPSHOT_VOLUME="/dev/host-vg/mongodb_snapshot"
    MONGO_USER="administrator"
    MONGO_CMD="/usr/bin/mongo --username $MONGO_USER --password $MONGO_PASSWORD --authenticationDatabase admin --quiet"

    function print_error() {
    RED="\033[0;31m"
    NC="\033[0m"
    message=$1
    echo -e "${RED}${message}${NC}"
    }

    function create_snapshot() {
    echo -n "Locking database before creating snapshot. You're using MongoDB "
    $MONGO_CMD --eval "db.version();"

    $MONGO_CMD -eval "d=db.fsyncLock(); printjson(d.info);" | sed s/\"//g >/dev/null 2>&1
    if [ $? -ne 0 ]; then
    print_error " Error: Could not lock DB. Aborting."
    exit 1
    else
    echo "Database is locked for creating snapshot."
    fi

    lvcreate -L 10GB --snapshot -n mongodb_snapshot ${MONGO_VOLUME}
    if [ $? -ne 0 ]; then
    print_error "Creation of snapshot failed"
    else
    echo "Snapshot created successfully"
    fi

    echo -en " Unlocking MongoDB. "
    $MONGO_CMD -eval "d=db.fsyncUnlock(); printjson(d);" >/dev/null 2>&1

    mkdir -p /mongo_snapshot

    echo "You can mount the snapshot with mount -t xfs -o nouuid ${MONGO_SNAPSHOT_VOLUME} /mongo_snapshot"
    lvdisplay ${MONGO_SNAPSHOT_VOLUME}
    }

    function remove_snapshot() {
    echo "Removing snapshot ${MONGO_SNAPSHOT_VOLUME}..."
    lvremove ${MONGO_SNAPSHOT_VOLUME}
    }

    function restore_snapshot() {
    echo -n "Do you really want to restore the snapshot (y/n)? "
    read answer
    if [ "${answer}" == "y" ]; then
    echo "Unmounting mongodb volume and restoring snapshot..."
    unmount ${MONGO_VOLUME}
    lvconvert --merge ${MONGO_SNAPSHOT_VOLUME}
    fi
    }

    case "${COMMAND}" in
    "create" )
    create_snapshot;;
    "restore" )
    restore_snapshot;;
    "remove" )
    remove_snapshot;;
    *)
    print_error "The command ${COMMAND} is not supported..." ;;
    esac