diff --git a/Dockerfile b/Dockerfile index 6276d72..a9e75f1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM mongo:2.6 +FROM mongo MAINTAINER Ilya Stepanov RUN apt-get update && \ @@ -11,6 +11,10 @@ RUN chmod +x /backup.sh ADD start.sh /start.sh RUN chmod +x /start.sh +RUN touch /var/log/cron.log + +ADD crontab /crontab + VOLUME /backup ENTRYPOINT ["/start.sh"] diff --git a/README.md b/README.md index 3062212..4240ee0 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,24 @@ docker-mongodump ================ -Docker image with mongodump running as a cron task +This is a fork of https://github.com/istepanov/docker-mongodump -- Docker image with mongodump running as a cron task. I added scripts to automatize installation for a backup docker container. -### Usage +### How To Use +First you have to build docker image from the repository: +```docker build -t istepanov/mongodump .``` +After that you can install backup job for your mongodb docker container as follows (basically provide container name as an argument to install-backup.sh script): +``` +$ docker ps +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +ea3a814c3720 campus2020portal_web:latest "node app.js" 6 days ago Up 5 days 0.0.0.0:8080->8080/tcp campus2020portal_web_1 +39cae58c3dc8 mongo:latest "/entrypoint.sh mong 4 weeks ago Up 5 days 27017/tcp campus2020portal_mongo_1 +$ ./install-backup.sh campus2020portal_mongo_1 +``` +This will run a docker container with cron job installed inside, which will backup mongodb once a day at 1.00 A.M. and save the output to /var/backups/mongodb as a .tar.gz archive. When you run docker via remote access (i.e. access remote docker host), the data will be saved on remote machine! -Attach a target mongo container to this container and mount a volume to container's `/data` folder. Backups will appear in this volume. Optionally set up cron job schedule (default is `0 1 * * *` - runs every day at 1:00 am). +For configuration options, please, take a look inside the scripts. Crontab schedule is available in crontab file and `0 1 * * *` by default. - docker run -d \ - -v /path/to/target/folder:/backup # where to put backups - -e 'CRON_SCHEDULE=0 1 * * *' # cron job schedule - --link my-mongo-container:mongo # linked container with running mongo - istepanov/mongodump - -To run backup once without cron job, add `no-cron` parameter: - - docker run --rm \ - -v /path/to/target/folder:/backup # where to put backups - --link my-mongo-container:mongo # linked container with running mongo - istepanov/mongodump no-cron +To run backup once you can use the following script: +``` +$ ./run-backup.sh campus2020portal_mongo_1 +``` diff --git a/crontab b/crontab new file mode 100644 index 0000000..382e2a1 --- /dev/null +++ b/crontab @@ -0,0 +1,2 @@ +0 1 * * * root /backup.sh >> /var/log/cron.log 2>&1 +# An empty line is required at the end of this file for a valid cron file. diff --git a/install-backup.sh b/install-backup.sh new file mode 100755 index 0000000..682e57e --- /dev/null +++ b/install-backup.sh @@ -0,0 +1,6 @@ +#!/bin/bash +BACKUP_DIR=/var/backups/mongodb +CONTAINER_NAME=$1 +DOCKER_NAME="istepanov/mongodump" + +docker run -d --name mongodump_$CONTAINER_NAME -v $BACKUP_DIR:/backup --link $CONTAINER_NAME:mongo $DOCKER_NAME diff --git a/run-backup.sh b/run-backup.sh new file mode 100755 index 0000000..d3c5624 --- /dev/null +++ b/run-backup.sh @@ -0,0 +1,7 @@ +#!/bin/bash +DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +BACKUP_DIR=/var/backups/mongodb +CONTAINER_NAME=$1 +DOCKER_NAME="istepanov/mongodump" + +docker run --rm -v $BACKUP_DIR:/backup --link $CONTAINER_NAME:mongo $DOCKER_NAME no-cron diff --git a/start.sh b/start.sh index 2123eb7..28bf24e 100644 --- a/start.sh +++ b/start.sh @@ -9,6 +9,6 @@ CRON_SCHEDULE=${CRON_SCHEDULE:-0 1 * * *} if [[ "$1" == 'no-cron' ]]; then exec /backup.sh else - echo "$CRON_SCHEDULE /backup.sh" | crontab - + cp /crontab /etc/cron.d/mongobackup-cron exec cron -f fi