Skip to content

Commit

Permalink
Add gzip and archive functionality to mongo backup
Browse files Browse the repository at this point in the history
  • Loading branch information
tsangste committed Feb 6, 2018
1 parent 228384f commit 7356170
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM mongo:latest
MAINTAINER Steven Tsang <[email protected]>
FROM library/mongo:3.4
LABEL maintainer="Steven Tsang <[email protected]>"

RUN apt-get update && apt-get -y install cron && mkdir -p /backup

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Moreover, if you link `tsangste/mongodb-backup` to a mongodb container(e.g. offi
CRON_TIME the interval of cron job to run mongodump. `0 0 * * *` by default, which is every day at 00:00
MAX_BACKUPS the number of backups to keep. When reaching the limit, the old backup will be discarded. No limit, by default
INIT_BACKUP if set, create a backup when the container launched
USE_GZIP if set, sets dump and restore to use gzip
USE_ARCHIVE if set, dump and restore with a single file format and can be used with gzip

## Restore from a backup

Expand All @@ -39,4 +41,4 @@ See the list of backups, you can run:

To restore database from a certain backup, simply run:

docker exec mongodb-backup /restore.sh /backup/2015.08.06.171901
docker exec mongodb-backup /restore.sh 2015.08.06.171901
6 changes: 6 additions & 0 deletions docker-compose.development.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3.2'
services:
mongodbbackup:
build:
context: .
dockerfile: Dockerfile
38 changes: 21 additions & 17 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
mongodb:
image: 'mongo:latest'
environment:
- MONGODB_PASS=mypass
ports:
- "27017:27017"
- "28017:28017"
mongodbbackup:
image: 'tsangste/mongodb-backup:latest'
links:
- mongodb
environment:
- CRON_TIME=20 3 * * * #Change to your favorate cron job schedule
- MAX_BACKUPS=10
- INIT_BACKUP=yes
volumes:
- /host/backup:/backup #Change to the host folder where you want to store the backups
version: '3.2'
services:
mongodb:
image: 'library/mongo:3.4'
environment:
- MONGODB_PASS=mypass
ports:
- "27017:27017"
- "28017:28017"
mongodbbackup:
image: 'tsangste/mongodb-backup:latest'
links:
- mongodb
environment:
- CRON_TIME=20 3 * * * #Change to your favourite cron job schedule
- MAX_BACKUPS=10
- INIT_BACKUP=yes
- USE_GZIP=yes
- USE_ARCHIVE=yes
volumes:
- /srv/backup:/backup #Change to the host folder where you want to store the backups
38 changes: 34 additions & 4 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,33 @@ MONGODB_PASS=${MONGODB_PASS:-${MONGODB_ENV_MONGODB_PASS}}
[[ ( -n "${MONGODB_PASS}" ) ]] && PASS_STR=" --password ${MONGODB_PASS}"
[[ ( -n "${MONGODB_DB}" ) ]] && DB_STR=" --db ${MONGODB_DB}"

BACKUP_CMD="mongodump --out /backup/"'${BACKUP_NAME}'" --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR}${DB_STR} ${EXTRA_OPTS}"
use_gzip_backup () {
if [ -n "${USE_GZIP}" ]; then
echo ".gz --gzip"
else
echo ""
fi
}

use_archive_backup () {
if [ -n "${USE_ARCHIVE}" ]; then
echo "--archive="
else
echo "--out "
fi
}

BACKUP_CMD="mongodump $(use_archive_backup)/backup/"'${BACKUP_NAME}'"$(use_gzip_backup) --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR}${DB_STR} ${EXTRA_OPTS}"

echo "=> Creating backup script"
rm -f /backup.sh
cat <<EOF >> /backup.sh
#!/bin/bash
MAX_BACKUPS=${MAX_BACKUPS}
BACKUP_NAME=\$(date +\%Y.\%m.\%d.\%H\%M\%S)
BACKUP_NAME=\$(date +\%Y.\%m.\%d.\%H\%M\c%S)
echo "=> Backup started"
if ${BACKUP_CMD} ;then
if ${BACKUP_CMD}; then
echo " Backup succeeded"
else
echo " Backup failed"
Expand All @@ -44,6 +60,20 @@ echo "=> Backup done"
EOF
chmod +x /backup.sh

use_gzip_restore () {
if [ -n "${USE_GZIP}" ]; then
echo "--gzip"
fi
}

use_archive_restore () {
if [ -n "${USE_ARCHIVE}" ]; then
echo "--archive="
fi
}

RESTORE_CMD="mongorestore --objcheck --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR} $(use_gzip_restore) $(use_archive_restore)"

echo "=> Creating restore script"
rm -f /restore.sh
cat <<EOF >> /restore.sh
Expand All @@ -53,7 +83,7 @@ name="\$1"
shift 1
echo "=> Restore database from \$name"
echo \$*
if mongorestore --objcheck --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR} \$* /backup/\$name; then
if ${RESTORE_CMD}\$*/backup/\$name; then
echo " Restore succeeded"
else
echo " Restore failed"
Expand Down

0 comments on commit 7356170

Please sign in to comment.