Sign inSign up

alphaott/mongodb-backup-s3

By alphaott

•Updated over 6 years ago

Image
0

6.6K

alphaott/mongodb-backup-s3 repository overview

⁠mongodb-backup-s3

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

⁠Forked from deenoize/mongodb-backup-s3.git⁠

Added support for AWS S3 v4 authorization mechanism for those who are experiencing error:

A client error (InvalidRequest) occurred when calling the PutObject operation: The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256.

⁠Usage:

docker run -d \
  --env AWS_ACCESS_KEY_ID=awsaccesskeyid \
  --env AWS_SECRET_ACCESS_KEY=awssecretaccesskey \
  --env S3_BUCKET=mybucketname
  --env MONGODB_HOST=mongodb.host \
  --env MONGODB_PORT=27017 \
  --env MONGODB_USER=admin \
  --env MONGODB_PASS=password \
  --env MONGODB_DB=test
  alphaott/mongodb-backup-s3

If you link deenoize/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 S3_BUCKET=mybucketname \
  --env S3_KEY=/a/sub/folder/path \
  --env INIT_BACKUP=true \
  --link my_mongo_db:mongodb \
  alphaott/mongodb-backup-s3

If your bucket in not standard region and you get A client error (PermanentRedirect) occurred when calling the PutObject operation: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint use AWS_DEFAULT_REGION env var like this:

docker run -d \
  --env AWS_ACCESS_KEY_ID=myaccesskeyid \
  --env AWS_SECRET_ACCESS_KEY=mysecretaccesskey \
  --env AWS_DEFAULT_REGION=us-east-1 \
  --env S3_BUCKET=mybucketname \
  --env S3_KEY=/a/sub/folder/path \
  --env INIT_BACKUP=true \
  --link my_mongo_db:mongodb \
  alphaott/mongodb-backup-s3

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

For automated backups

mongodbbackup:
  image: 'alphaott/mongodb-backup-s3:latest'
  links:
    - mongodb
  environment:
    - AWS_ACCESS_KEY_ID=myaccesskeyid
    - AWS_SECRET_ACCESS_KEY=mysecretaccesskey
    - S3_BUCKET=my-s3-bucket
    - S3_KEY=/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: 'alphaott/mongodb-backup-s3:latest'
  links:
    - mongodb
  environment:
    - AWS_ACCESS_KEY_ID=myaccesskeyid
    - AWS_SECRET_ACCESS_KEY=mysecretaccesskey
    - S3_BUCKET=my-s3-bucket
    - S3_KEY=/prod/db
  command: restore

⁠Commands

cron - run cron task. This is default command.

backup - run backup task

restore - run restore task

list - run list-backup task

⁠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)

AWS_DEFAULT_REGION - your s3 bucket' region (eg us-east-2 for Ohio). Optional. Add if you get an error A client error (PermanentRedirect)

S3_BUCKET - your s3 bucket

S3_KEY - path to put backups, starting from slash, without trailing slash (eg /myapp/db_backups). defaults to root of bucket.

S3_EXTRA_OPTS - any extra options to pass to aws s3 command

BACKUP_FILE_DATE_FORMAT: - backup filename date part format, using date command syntax. default: %Y-%m-%dT%H-%M-%S

BACKUP_FILE_PREFIX: - backup file name prefix. default: ${MONGODB_DB:-backup}

BACKUP_FILE_EXT: - backup file extension. default: .dump.gz

BACKUP_FILE_NAME: - backup file name. default: .dump.gz

MONGODB_HOST - the host/ip of your mongodb database. default: mongo

MONGODB_PORT - the port number of your mongodb database. default: 27017

MONGODB_USER - the username of your mongodb database.

MONGODB_PASS - the password of your mongodb database

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

TEMP_DIR - temporary directory for backups and logs. default: /tmp

EXTRA_OPTS - any extra options to pass to mongodump command

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

TZ - timezone. default: UTC

CRON_TZ - cron timezone. default: UTC

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

⁠Restore from a backup

To see the list of backups, you can run:

docker exec mongodb-backup-s3 list-backups

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

docker exec mongodb-backup-s3 restore 2017-04-06T15-58-12

To restore latest just:

docker exec mongodb-backup-s3 restore

⁠Acknowledgements

Tag summary

Content type

Image

Digest

Size

196.6 MB

Last updated

over 6 years ago

docker pull alphaott/mongodb-backup-s3