diff --git a/Dockerfile b/Dockerfile index 181b4a4..25a8a6c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM alpine:3.7@sha256:8c03bb07a531c53ad7d0f6e7041b64d81f99c6e493cb39abba56d956b MAINTAINER Leonardo Gatica -RUN apk add --no-cache mongodb-tools py2-pip && \ +RUN apk add --no-cache mongodb-tools curl py2-pip && \ pip install pymongo awscli && \ mkdir /backup diff --git a/README.md b/README.md index ee573a1..91b08cc 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ docker run -d --name mongodump \ lgatica/mongodump-s3 ``` -### Inmediatic backup +### Immediate backup ```bash docker run -d --name mongodump \ @@ -65,9 +65,22 @@ docker run -d --name mongodump \ lgatica/mongodump-s3 ``` -## IAM Policity +### Slack Hook +```bash +docker run -d --name mongodump \ + -e "MONGO_URI=mongodb://user:pass@host:port/dbname" + -e "AWS_ACCESS_KEY_ID=your_aws_access_key" + -e "AWS_SECRET_ACCESS_KEY=your_aws_secret_access_key" + -e "AWS_DEFAULT_REGION=us-west-1" + -e "S3_BUCKET=your_aws_bucket" + -e "SLACK_URI=your_slack_uri" + lgatica/mongodump-s3 +``` + -You need to add a user with the following policies. Be sure to change `your_bucket` by the correct. +## IAM Policy + +You need to add a user with the following policies. Be sure to change `your_bucket` by the correct name. ```xml { @@ -98,11 +111,19 @@ You need to add a user with the following policies. Be sure to change `your_buck } ``` -## Extra environmnet +## Extra environment - `S3_PATH` - Default value is `mongodb`. Example `s3://your_bucket/mongodb` - `MONGO_COMPLETE` - Default not set. If set doing backup full mongodb - `MAX_BACKUPS` - Default not set. If set doing it keeps the last n backups in /backup +- `BACKUP_NAME` - Default is `$(date -u +%Y-%m-%d_%H-%M-%S)_UTC.gz`. If set this is the name of the backup file. Useful when using s3 versioning. (Remember to place .gz extension on your filename) +- `EXTRA_OPTIONS` - Default not set. +- `SLACK_URI` - Default not set. Sends a curl notification to the Slack Incoming Webhook. + +## Troubleshoot + +1. If you get SASL Authentication failure, add `--authenticationDatabase=admin` to EXTRA_OPTIONS. +2. If you get "Failed: error writing data for collection ... Unrecognized field 'snapshot'", add `--forceTableScan` to EXTRA_OPTIONS. ## License diff --git a/backup.sh b/backup.sh index 5ddc97b..588a01a 100755 --- a/backup.sh +++ b/backup.sh @@ -1,24 +1,56 @@ #!/usr/bin/env sh OPTIONS=`python /usr/local/bin/mongouri` -BACKUP_NAME="$(date -u +%Y-%m-%d_%H-%M-%S)_UTC.gz" +OPTIONS="$OPTIONS $EXTRA_OPTIONS" +DEFAULT_BACKUP_NAME="$(date -u +%Y-%m-%d_%H-%M-%S)_UTC.gz" +BACKUP_NAME=${BACKUP_NAME:-$DEFAULT_BACKUP_NAME} +LOCAL_BACKUP_ROOT_FOLDER="/backup" +LOCAL_DUMP_LOCATION="$LOCAL_BACKUP_ROOT_FOLDER/dump" + +notify() { + if [ "${SLACK_URI}" ]; then + message="$BACKUP_NAME has been backed up at s3://${S3_BUCKET}/${S3_PATH}/${BACKUP_NAME}" + if [ "${1}" != "0" ]; then + message="Unable to backup $BACKUP_NAME at s3://${S3_BUCKET}/${S3_PATH}/${BACKUP_NAME}. See Logs." + fi + curl -X POST --data-urlencode "payload={\"text\": \"$message\"}" $SLACK_URI + fi +} # Run backup -mongodump ${OPTIONS} -o /backup/dump +mongodump ${OPTIONS} -o "${LOCAL_DUMP_LOCATION}" +status=$? +if [ "${status}" -eq "1" ]; then + echo "ERROR: Mongodump failed." + notify 1 + exit 1 +fi + # Compress backup -cd /backup/ && tar -cvzf "${BACKUP_NAME}" dump +tar -cvzf "${LOCAL_BACKUP_ROOT_FOLDER}/${BACKUP_NAME}" "${LOCAL_DUMP_LOCATION}" + # Upload backup -aws s3 cp "/backup/${BACKUP_NAME}" "s3://${S3_BUCKET}/${S3_PATH}/${BACKUP_NAME}" +aws s3 cp "${LOCAL_BACKUP_ROOT_FOLDER}/${BACKUP_NAME}" "s3://${S3_BUCKET}/${S3_PATH}/${BACKUP_NAME}" +status=$? +echo $status +if [ "${status}" != "0" ]; then + echo "ERROR: AWS Upload failed." + notify 1 + exit 1 +fi + +notify 0 + # Delete temp files -rm -rf /backup/dump +rm -rf "${LOCAL_DUMP_LOCATION}" # Delete backup files if [ -n "${MAX_BACKUPS}" ]; then - while [ $(ls /backup -w 1 | wc -l) -gt ${MAX_BACKUPS} ]; + while [ $(ls ${LOCAL_BACKUP_ROOT_FOLDER} -w 1 | wc -l) -gt ${MAX_BACKUPS} ]; do BACKUP_TO_BE_DELETED=$(ls /backup -w 1 | sort | head -n 1) - rm -rf /backup/${BACKUP_TO_BE_DELETED} + rm -rf ${LOCAL_BACKUP_ROOT_FOLDER}/${BACKUP_TO_BE_DELETED} done else - rm -rf /backup/* -fi + rm -rf ${LOCAL_BACKUP_ROOT_FOLDER}/* +fi \ No newline at end of file