Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mongo:2.6
FROM mongo
MAINTAINER Ilya Stepanov <dev@ilyastepanov.com>

RUN apt-get update && \
Expand All @@ -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"]
Expand Down
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
2 changes: 2 additions & 0 deletions crontab
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions install-backup.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions run-backup.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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