diff --git a/README.md b/README.md index ed3fdb1..42aecda 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/run.sh b/run.sh index 2643cb0..372971b 100755 --- a/run.sh +++ b/run.sh @@ -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}" @@ -47,7 +47,7 @@ rm -f /restore.sh cat <> /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" @@ -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