Skip to content

Instantly share code, notes, and snippets.

@shanestillwell
Created May 14, 2014 17:32
Show Gist options
  • Save shanestillwell/65a2e5d5f5daf8bf7be2 to your computer and use it in GitHub Desktop.
Save shanestillwell/65a2e5d5f5daf8bf7be2 to your computer and use it in GitHub Desktop.

Revisions

  1. Shane A. Stillwell created this gist May 14, 2014.
    77 changes: 77 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    #!/bin/bash
    #
    # Argument = -u user -p password -k key -s secret -b bucket
    #
    # To Do - Add logging of output.
    # To Do - Abstract bucket region to options

    set -e

    export PATH="$PATH:/usr/local/bin"

    usage()
    {
    cat << EOF
    usage: $0 options
    This script dumps the current mongo database, tars it, then sends it to an Amazon S3 bucket.
    OPTIONS:
    -h Show this message
    -u Mongodb user
    -p Mongodb password
    -k AWS Access Key
    -s AWS Secret Key
    -r Amazon S3 region
    -b Amazon S3 bucket name
    EOF
    }

    MONGODB_USER=XXX
    MONGODB_PASSWORD=XXX
    MONGODB_ADMIN_USER=XXX
    MONGODB_ADMIN_PASSWORD=XXX
    AWS_ACCESS_KEY=XXX
    AWS_SECRET_KEY=XXX
    S3_REGION=us-east-1
    S3_BUCKET=XXX
    DIR=/tmp
    DATE=$(date -u "+%a")
    # If first day of the month, then store the month snapshot
    if [ 01 = $(date -u "+%d") ]; then
    DATE=$(date -u "+%b");
    fi
    FILE_NAME="backup-$DATE"
    ARCHIVE_NAME="$FILE_NAME.tar.gz"

    if [[ -z $MONGODB_USER ]] || [[ -z $MONGODB_PASSWORD ]] || [[ -z $AWS_ACCESS_KEY ]] || [[ -z $AWS_SECRET_KEY ]] || [[ -z $S3_REGION ]] || [[ -z $S3_BUCKET ]]
    then
    usage
    exit 1
    fi

    # Lock the database
    # Note there is a bug in mongo 2.2.0 where you must touch all the databases before you run mongodump
    mongo -username "$MONGODB_USER" -password "$MONGODB_PASSWORD" --port 10096 solidweld --eval "db.fsyncLock();"

    # Dump the database
    mongodump -username "$MONGODB_USER" -password "$MONGODB_PASSWORD" --port 10096 --db solidweld --out $DIR/backup/$FILE_NAME

    # Unlock the database
    # mongo -username "$MONGODB_ADMIN_USER" -password "$MONGODB_ADMIN_PASSWORD" --port 10096 admin --eval "printjson(db.fsyncUnlock());"
    mongo -username "$MONGODB_USER" -password "$MONGODB_PASSWORD" --port 10096 solidweld --eval "db.fsyncUnlock();"

    # Tar Gzip the file
    tar -C $DIR/backup/ -zcf $DIR/backup/$ARCHIVE_NAME $FILE_NAME/

    # Remove the backup directory
    rm -r $DIR/backup/$FILE_NAME

    cd $DIR/backup/

    echo $ARCHIVE_NAME
    # Send the file to the backup drive or S3
    /usr/bin/s3cmd put $ARCHIVE_NAME s3://solidweld/mongobackups/$ARCHIVE_NAME

    # Clear out /tmp/backup
    rm -rf $DIR/backup/*