Skip to content

Commit

Permalink
add mongodbbackup
Browse files Browse the repository at this point in the history
  • Loading branch information
klamas1 committed Aug 8, 2019
1 parent 84c80e2 commit 69dfd9b
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 0 deletions.
30 changes: 30 additions & 0 deletions compose/mongodb-backup-s3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: '3.7'

services:
mongodbbackup:
build: mongodb-backup-s3/
depends_on:
- mongodb
environment:
INIT_BACKUP: "true"
AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}
AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}
AWS_S3_ENDPOINT: ${AWS_S3_ENDPOINT}
AWS_S3_BUCKET: ${AWS_S3_BUCKET}
BACKUP_FOLDER: ${BACKUP_FOLDER}
MONGODB_HOST: "mongodb.compose"
MONGODB_PORT: 27017
restart: always
networks:
default:
aliases:
- mongodb-backup-s3.compose
- mongo
application-network:
aliases:
- mongodb-backup-s3

networks:
application-network:
external: true

10 changes: 10 additions & 0 deletions compose/mongodb-backup-s3/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM mongo

RUN apt-get update && apt-get -y install cron awscli

ENV CRON_TIME="0 3 * * *" \
TZ=US/Eastern \
CRON_TZ=US/Eastern

ADD run.sh /run.sh
CMD /run.sh
117 changes: 117 additions & 0 deletions compose/mongodb-backup-s3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# mongodb-backup-s3

This image runs mongodump to backup data using cronjob to an s3 bucket

## Usage:

```
docker run -d \
--env AWS_ACCESS_KEY_ID=awsaccesskeyid \
--env AWS_SECRET_ACCESS_KEY=awssecretaccesskey \
--env BUCKET=s3bucket
--env MONGODB_HOST=mongodb.host \
--env MONGODB_PORT=27017 \
--env MONGODB_USER=admin \
--env MONGODB_PASS=password \
halvves/mongodb-backup-s3
```

If you link `halvves/mongodb-backup-s3` to a mongodb container with an alias named mongodb, this image will try to auto load the `host`, `port`, `user`, `pass` if possible. Like this:

```
docker run -d \
--env AWS_ACCESS_KEY_ID=myaccesskeyid \
--env AWS_SECRET_ACCESS_KEY=mysecretaccesskey \
--env BUCKET=mybucketname \
--env BACKUP_FOLDER=a/sub/folder/path/ \
--env INIT_BACKUP=true \
--link my_mongo_db:mongodb \
halvves/mongodb-backup-s3
```

Add to a docker-compose.yml to enhance your robotic army:

For automated backups
```
mongodbbackup:
image: 'halvves/mongodb-backup-s3:latest'
links:
- mongodb
environment:
- AWS_ACCESS_KEY_ID=myaccesskeyid
- AWS_SECRET_ACCESS_KEY=mysecretaccesskey
- BUCKET=my-s3-bucket
- BACKUP_FOLDER=prod/db/
restart: always
```

Or use `INIT_RESTORE` with `DISABLE_CRON` for seeding/restoring/starting a db (great for a fresh instance or a dev machine)
```
mongodbbackup:
image: 'halvves/mongodb-backup-s3:latest'
links:
- mongodb
environment:
- AWS_ACCESS_KEY_ID=myaccesskeyid
- AWS_SECRET_ACCESS_KEY=mysecretaccesskey
- BUCKET=my-s3-bucket
- BACKUP_FOLDER=prod/db/
- INIT_RESTORE=true
- DISABLE_CRON=true
```

## Parameters

`AWS_ACCESS_KEY_ID` - your aws access key id (for your s3 bucket)

`AWS_SECRET_ACCESS_KEY`: - your aws secret access key (for your s3 bucket)

`BUCKET`: - your s3 bucket

`BACKUP_FOLDER`: - name of folder or path to put backups (eg `myapp/db_backups/`). defaults to root of bucket.

`MONGODB_HOST` - the host/ip of your mongodb database

`MONGODB_PORT` - the port number of your mongodb database

`MONGODB_USER` - the username of your mongodb database. If MONGODB_USER is empty while MONGODB_PASS is not, the image will use admin as the default username

`MONGODB_PASS` - the password of your mongodb database

`MONGODB_DB` - the database name to dump. If not specified, it will dump all the databases

`EXTRA_OPTS` - any extra options to pass to mongodump command

`CRON_TIME` - the interval of cron job to run mongodump. `0 3 * * *` by default, which is every day at 03:00hrs.

`TZ` - timezone. default: `US/Eastern`

`CRON_TZ` - cron timezone. default: `US/Eastern`

`INIT_BACKUP` - if set, create a backup when the container launched

`INIT_RESTORE` - if set, restore from latest when container is launched

`DISABLE_CRON` - if set, it will skip setting up automated backups. good for when you want to use this container to seed a dev environment.

## Restore from a backup

To see the list of backups, you can run:
```
docker exec mongodb-backup-s3 /listbackups.sh
```

To restore database from a certain backup, simply run (pass in just the timestamp part of the filename):

```
docker exec mongodb-backup-s3 /restore.sh 20170406T155812
```

To restore latest just:
```
docker exec mongodb-backup-s3 /restore.sh
```

## Acknowledgements

* forked from [futurist](https://github.com/futurist)'s fork of [tutumcloud/mongodb-backup](https://github.com/tutumcloud/mongodb-backup)
92 changes: 92 additions & 0 deletions compose/mongodb-backup-s3/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/bin/bash

MONGODB_HOST=${MONGODB_PORT_27017_TCP_ADDR:-${MONGODB_HOST}}
MONGODB_HOST=${MONGODB_PORT_1_27017_TCP_ADDR:-${MONGODB_HOST}}
MONGODB_PORT=${MONGODB_PORT_27017_TCP_PORT:-${MONGODB_PORT}}
MONGODB_PORT=${MONGODB_PORT_1_27017_TCP_PORT:-${MONGODB_PORT}}
MONGODB_USER=${MONGODB_USER:-${MONGODB_ENV_MONGODB_USER}}
MONGODB_PASS=${MONGODB_PASS:-${MONGODB_ENV_MONGODB_PASS}}

S3PATH="s3://$AWS_S3_BUCKET/$BACKUP_FOLDER"

[[ ( -z "${MONGODB_USER}" ) && ( -n "${MONGODB_PASS}" ) ]] && MONGODB_USER='admin'

[[ ( -n "${MONGODB_USER}" ) ]] && USER_STR=" --username ${MONGODB_USER}"
[[ ( -n "${MONGODB_PASS}" ) ]] && PASS_STR=" --password '${MONGODB_PASS}'"
[[ ( -n "${MONGODB_DB}" ) ]] && DB_STR=" --db ${MONGODB_DB}"

# Export AWS Credentials into env file for cron job
printenv | sed 's/^\([a-zA-Z0-9_]*\)=\(.*\)$/export \1="\2"/g' | grep -E "^export AWS" > /root/project_env.sh

echo "=> Creating backup script"
rm -f /backup.sh
cat <<EOF >> /backup.sh
#!/bin/bash
TIMESTAMP=\`/bin/date +"%Y%m%dT%H%M%S"\`
BACKUP_NAME=\${TIMESTAMP}.dump.gz
S3BACKUP=${S3PATH}\${BACKUP_NAME}
S3LATEST=${S3PATH}latest.dump.gz
echo "=> Backup started"
if [ -z "\${AWS_S3_ENDPOINT}" ]; then AWS_ARGS=""; else AWS_ARGS="--endpoint-url \${AWS_S3_ENDPOINT}"; fi
if mongodump --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR}${DB_STR} --archive=\${BACKUP_NAME} --gzip ${EXTRA_OPTS} && aws \${AWS_ARGS} s3 cp \${BACKUP_NAME} \${S3BACKUP} && aws \${AWS_ARGS} s3 cp \${S3BACKUP} \${S3LATEST} && rm \${BACKUP_NAME} ;then
echo " > Backup succeeded"
else
echo " > Backup failed"
fi
echo "=> Done"
EOF
chmod +x /backup.sh
echo "=> Backup script created"

echo "=> Creating restore script"
rm -f /restore.sh
cat <<EOF >> /restore.sh
#!/bin/bash
if [[( -n "\${1}" )]];then
RESTORE_ME=\${1}.dump.gz
else
RESTORE_ME=latest.dump.gz
fi
S3RESTORE=${S3PATH}\${RESTORE_ME}
echo "=> Restore database from \${RESTORE_ME}"
if aws s3 cp \${S3RESTORE} \${RESTORE_ME} && mongorestore --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR}${DB_STR} --drop ${EXTRA_OPTS} --archive=\${RESTORE_ME} --gzip && rm \${RESTORE_ME}; then
echo " Restore succeeded"
else
echo " Restore failed"
fi
echo "=> Done"
EOF
chmod +x /restore.sh
echo "=> Restore script created"

echo "=> Creating list script"
rm -f /listbackups.sh
cat <<EOF >> /listbackups.sh
#!/bin/bash
aws s3 ls ${S3PATH}
EOF
chmod +x /listbackups.sh
echo "=> List script created"

ln -s /restore.sh /usr/bin/restore
ln -s /backup.sh /usr/bin/backup
ln -s /listbackups.sh /usr/bin/listbackups

touch /mongo_backup.log

if [ -n "${INIT_BACKUP}" ]; then
echo "=> Create a backup on the startup"
/backup.sh
fi

if [ -n "${INIT_RESTORE}" ]; then
echo "=> Restore store from lastest backup on startup"
/restore.sh
fi

if [ -z "${DISABLE_CRON}" ]; then
echo "${CRON_TIME} . /root/project_env.sh; /backup.sh >> /mongo_backup.log 2>&1" > /crontab.conf
crontab /crontab.conf
echo "=> Running cron job"
cron && tail -f /mongo_backup.log
fi
2 changes: 2 additions & 0 deletions compose/mongodb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ services:
kompose.service.type: nodeport
expose:
- 27017
ports:
- "27017:27017"
volumes:
- ../volumes/mongodb:/data/db
networks:
Expand Down
7 changes: 7 additions & 0 deletions dump.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset

docker-compose exec -T mongodb bash -c 'cd /data/db/dump; rm -f directheroes.tar.gz; mongodump --out=/data/db/dump --db directheroes; tar pczf directheroes.tar.gz directheroes'

0 comments on commit 69dfd9b

Please sign in to comment.