From becd415afe494ea892b57a75b50f368e67299a9d Mon Sep 17 00:00:00 2001 From: Ivan Ermilov Date: Tue, 11 Aug 2015 12:51:14 +0200 Subject: [PATCH 1/8] Changed the mongo version to conform with my use case --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 6276d72..fd8b167 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM mongo:2.6 +FROM mongo MAINTAINER Ilya Stepanov RUN apt-get update && \ From e39ad4514aec36ea9c99857409e7042f2123fb02 Mon Sep 17 00:00:00 2001 From: Ivan Ermilov Date: Tue, 11 Aug 2015 12:53:13 +0200 Subject: [PATCH 2/8] added test script for one time backup run --- run-backup.sh | 5 +++++ 1 file changed, 5 insertions(+) create mode 100755 run-backup.sh diff --git a/run-backup.sh b/run-backup.sh new file mode 100755 index 0000000..f8947ac --- /dev/null +++ b/run-backup.sh @@ -0,0 +1,5 @@ +#!/bin/bash +DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +CONTAINER_NAME=$1 + +docker run --rm -v $DIR:/backup --link $CONTAINER_NAME:mongo mongobackup no-cron From 41d40a778c5ba0f55c1113963681deb9d31171fa Mon Sep 17 00:00:00 2001 From: Ivan Ermilov Date: Tue, 11 Aug 2015 12:59:31 +0200 Subject: [PATCH 3/8] changed the name of the docker image to the original one --- run-backup.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/run-backup.sh b/run-backup.sh index f8947ac..6731ce1 100755 --- a/run-backup.sh +++ b/run-backup.sh @@ -1,5 +1,6 @@ #!/bin/bash DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) CONTAINER_NAME=$1 +DOCKER_NAME="istepanov/mongodump" -docker run --rm -v $DIR:/backup --link $CONTAINER_NAME:mongo mongobackup no-cron +docker run --rm -v $DIR:/backup --link $CONTAINER_NAME:mongo $DOCKER_NAME no-cron From 6f5fc76297bd521314e58be3630042856aaff7ef Mon Sep 17 00:00:00 2001 From: Ivan Ermilov Date: Tue, 11 Aug 2015 13:47:57 +0200 Subject: [PATCH 4/8] move cron in the separate file --- Dockerfile | 2 ++ crontab | 2 ++ install-backup.sh | 6 ++++++ run-backup.sh | 3 ++- start.sh | 2 +- 5 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 crontab create mode 100755 install-backup.sh diff --git a/Dockerfile b/Dockerfile index fd8b167..7208f41 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,8 @@ RUN chmod +x /backup.sh ADD start.sh /start.sh RUN chmod +x /start.sh +ADD crontab /crontab + VOLUME /backup ENTRYPOINT ["/start.sh"] diff --git a/crontab b/crontab new file mode 100644 index 0000000..2f5d6b5 --- /dev/null +++ b/crontab @@ -0,0 +1,2 @@ +* * * * * root /backup.sh +# 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..b12c337 --- /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 -e 'CRON_SCHEDULE=* * * * *' --link $CONTAINER_NAME:mongo $DOCKER_NAME diff --git a/run-backup.sh b/run-backup.sh index 6731ce1..d3c5624 100755 --- a/run-backup.sh +++ b/run-backup.sh @@ -1,6 +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 $DIR:/backup --link $CONTAINER_NAME:mongo $DOCKER_NAME no-cron +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 From f5423e98ab89d77b15ebd785ed71836bd8990929 Mon Sep 17 00:00:00 2001 From: Ivan Ermilov Date: Tue, 11 Aug 2015 14:15:56 +0200 Subject: [PATCH 5/8] moved crontab to separate file --- Dockerfile | 2 ++ crontab | 2 +- install-backup.sh | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7208f41..a9e75f1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,8 @@ 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 diff --git a/crontab b/crontab index 2f5d6b5..382e2a1 100644 --- a/crontab +++ b/crontab @@ -1,2 +1,2 @@ -* * * * * root /backup.sh +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 index b12c337..682e57e 100755 --- a/install-backup.sh +++ b/install-backup.sh @@ -3,4 +3,4 @@ BACKUP_DIR=/var/backups/mongodb CONTAINER_NAME=$1 DOCKER_NAME="istepanov/mongodump" -docker run -d --name mongodump_$CONTAINER_NAME -v $BACKUP_DIR:/backup -e 'CRON_SCHEDULE=* * * * *' --link $CONTAINER_NAME:mongo $DOCKER_NAME +docker run -d --name mongodump_$CONTAINER_NAME -v $BACKUP_DIR:/backup --link $CONTAINER_NAME:mongo $DOCKER_NAME From 442e5ac0359e31a6bfdace0eaa66346f99e353d2 Mon Sep 17 00:00:00 2001 From: Ivan Ermilov Date: Tue, 11 Aug 2015 14:29:14 +0200 Subject: [PATCH 6/8] Update README.md --- README.md | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 3062212..0558291 100644 --- a/README.md +++ b/README.md @@ -3,19 +3,21 @@ docker-mongodump Docker image with mongodump running as a cron task -### 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! +For configuration options, please, take a look inside the scripts. Crontab schedule is available in crontab file and `0 1 * * *` by default. -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). - - 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 +``` From a666364354d1aea3d28bc46d05c6b74998c18c62 Mon Sep 17 00:00:00 2001 From: Ivan Ermilov Date: Tue, 11 Aug 2015 14:29:44 +0200 Subject: [PATCH 7/8] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0558291..c3e6438 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ ea3a814c3720 campus2020portal_web:latest "node app.js" 6 days $ ./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! + For configuration options, please, take a look inside the scripts. Crontab schedule is available in crontab file and `0 1 * * *` by default. To run backup once you can use the following script: From adbfcb0991d095f55d962398639d4d587db7e751 Mon Sep 17 00:00:00 2001 From: Ivan Ermilov Date: Tue, 11 Aug 2015 14:32:02 +0200 Subject: [PATCH 8/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c3e6438..4240ee0 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ 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. ### How To Use First you have to build docker image from the repository: