Skip to content
This repository has been archived by the owner on Nov 29, 2020. It is now read-only.

Add INIT_RESTORE option #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Moreover, if you link `tutum/mongodb-backup` to a mongodb container(e.g. `tutum/
EXTRA_OPTS the extra options to pass to mongodump command
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_RESTORE if set, restore latest backup when the container launched (before initial backup)
INIT_BACKUP if set, create a backup when the container launched

## Restore from a backup
Expand Down
12 changes: 10 additions & 2 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ MONGODB_PASS=${MONGODB_PASS:-${MONGODB_ENV_MONGODB_PASS}}

[[ ( -n "${MONGODB_USER}" ) ]] && USER_STR=" --username ${MONGODB_USER}"
[[ ( -n "${MONGODB_PASS}" ) ]] && PASS_STR=" --password ${MONGODB_PASS}"
[[ ( -n "${MONGODB_DB}" ) ]] && USER_STR=" --db ${MONGODB_DB}"
[[ ( -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}"

Expand Down Expand Up @@ -47,7 +47,7 @@ rm -f /restore.sh
cat <<EOF >> /restore.sh
#!/bin/bash
echo "=> Restore database from \$1"
if mongorestore --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR} \$1; then
if mongorestore --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR}${DB_STR} \$1; then
echo " Restore succeeded"
else
echo " Restore failed"
Expand All @@ -59,6 +59,14 @@ chmod +x /restore.sh
touch /mongo_backup.log
tail -F /mongo_backup.log &

if [ -n "${INIT_RESTORE}" ]; then
echo "=> Restore on the startup"
latest_backup=$(ls /backup/ -1 | sort -r | head -1)
if [ -n "$latest_backup" ]; then
/restore.sh /backup/$latest_backup/${MONGODB_DB}
fi
fi

if [ -n "${INIT_BACKUP}" ]; then
echo "=> Create a backup on the startup"
/backup.sh
Expand Down