Skip to content

Instantly share code, notes, and snippets.

@makasim
Last active February 1, 2019 14:28
Show Gist options
  • Save makasim/a1d9745d63770c1c9d0a0b5aba375550 to your computer and use it in GitHub Desktop.
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
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
#!/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
#!/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