Skip to content

Add ability to backup only single database #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mongo:3.0
FROM mongo:3.0.11
MAINTAINER Ilya Stepanov <dev@ilyastepanov.com>

RUN apt-get update && \
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -19,3 +19,15 @@ To run backup once without cron job, add `no-cron` parameter:
-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 backup only one database, set environment variable `MONGO_DB_NAME`

docker run -d \
-v /path/to/target/folder:/backup \ # where to put backups
-e 'CRON_SCHEDULE=0 1 * * *' \ # cron job schedule
-e 'MONGO_DB_NAME=<db_name>' \ # set database name
--link my-mongo-container:mongo \ # linked container with running mongo
istepanov/mongodump

By default, backups available for last 30 days only. To manage it, set environment
variable `MONGO_BACKUP_EXPIRE_DAYS`
26 changes: 24 additions & 2 deletions backup.sh
Original file line number Diff line number Diff line change
@@ -7,8 +7,30 @@ echo "Job started: $(date)"
DATE=$(date +%Y%m%d_%H%M%S)
FILE="/backup/backup-$DATE.tar.gz"

mongodump --quiet -h $MONGO_PORT_27017_TCP_ADDR -p $MONGO_PORT_27017_TCP_PORT
arr=("--quiet" "-h" "$MONGO_PORT_27017_TCP_ADDR" "-p" "$MONGO_PORT_27017_TCP_PORT")
if [ ! -z $MONGO_DB_NAME ]; then
arr+=("-d")
arr+=("$MONGO_DB_NAME")
fi
mongodump "${arr[@]}"
tar -zcvf $FILE dump/
rm -rf dump/

echo "Job finished: $(date)"
echo "Start autoclean"
FILES=/backup/*.tar.gz
currtime=`date +%s`
for f in $FILES
do
if [ -f $f ];
then
filemtime=`stat -c %Y $f`
diff=$(( (currtime - filemtime)/86400))
if (($diff > $MONGO_BACKUP_EXPIRE_DAYS));
then
echo "deleting '$f' ..."
rm $f
echo "deleted."
fi
fi
done
echo "End autoclean"
5 changes: 4 additions & 1 deletion start.sh
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
set -e

CRON_SCHEDULE=${CRON_SCHEDULE:-0 1 * * *}

MONGO_BACKUP_EXPIRE_DAYS=${MONGO_BACKUP_EXPIRE_DAYS:-30}
if [[ "$1" == 'no-cron' ]]; then
exec /backup.sh
else
@@ -13,6 +13,9 @@ else
fi
CRON_ENV="MONGO_PORT_27017_TCP_ADDR='$MONGO_PORT_27017_TCP_ADDR'"
CRON_ENV="$CRON_ENV\nMONGO_PORT_27017_TCP_PORT='$MONGO_PORT_27017_TCP_PORT'"
CRON_ENV="$CRON_ENV\nMONGO_DB_NAME='$MONGO_DB_NAME'"
CRON_ENV="$CRON_ENV\nMONGO_BACKUP_EXPIRE_DAYS=$MONGO_BACKUP_EXPIRE_DAYS"
CRON_ENV="$CRON_ENV\nTZ=$TZ"
echo -e "$CRON_ENV\n$CRON_SCHEDULE /backup.sh > $LOGFIFO 2>&1" | crontab -
crontab -l
cron