Last active
February 1, 2019 14:28
-
-
Save makasim/a1d9745d63770c1c9d0a0b5aba375550 to your computer and use it in GitHub Desktop.
Dump any mongo database and restore it with only Docker. restore-local-db could restore a dump to mongo running in a container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (( "$#" != 1 )) | |
then | |
echo Host is required! | |
exit | |
fi | |
PROJECT_NAME=${PWD##*/} | |
mkdir -p dump | |
docker run --rm mongo mongodump --host $1 --archive --gzip | \ | |
cat > dump/dump_${PROJECT_NAME}_$(date '+%d-%m-%Y_%H-%M-%S').gz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
set -x | |
if [[ ! "$1" ]] | |
then | |
echo Host is required! | |
exit | |
fi | |
HOST=$1 | |
if [[ ! "$2" ]] | |
then | |
LATEST_DUMP=`ls -rt dump | tail -n 1` | |
if [ ! -z "$LATEST_DUMP" ] | |
then | |
read -p "Would you like restore the latest dump ${LATEST_DUMP}? [Y/n]" USE_LATEST_DUMP | |
if [[ $USE_LATEST_DUMP == "n" ]] | |
then | |
exit | |
fi | |
FILEPATH="${PWD}/dump/${LATEST_DUMP}" | |
fi | |
else | |
FILEPATH=$2 | |
fi | |
if [[ ! -f "$FILEPATH" ]] | |
then | |
echo "Such file doesn't exists!" | |
exit | |
fi | |
read -p "The database will be DROPPED. Proceed? [Y/n]" DROPDB | |
if [[ $DROPDB == "n" ]] | |
then | |
exit | |
fi | |
cat $FILEPATH | docker run --rm -i mongo mongorestore --host $HOST --archive --gzip --drop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
HOST=`docker network inspect bridge --format='{{range $p, $conf := .IPAM.Config}} {{(index $conf).Gateway}} {{end}}'` | |
./bin/restore-db.sh $HOST |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment