Last active
February 10, 2023 16:34
-
-
Save stanislavb/5575d160e46885e87fa4 to your computer and use it in GitHub Desktop.
Backup and restore MongoDB running in a Docker container
This file contains hidden or 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
#!/bin/bash | |
usage() { | |
echo "Usage $0 -c mongo_docker_container_name" | |
} | |
while [[ $# > 1 ]] | |
do | |
key="$1" | |
case $key in | |
-c|--container) | |
CONTAINERNAME="$2" | |
shift # past argument | |
;; | |
*) | |
usage | |
exit 1 | |
;; | |
esac | |
shift # past argument or value | |
done | |
if [ -z "${CONTAINERNAME}" ]; then | |
usage | |
exit 1 | |
fi | |
# -i interactive | |
# -t open TTY | |
# --rm remove container after use | |
# --name name of our temporary container | |
# --link map server container to hostname within client container | |
# mongo use official mongo image | |
# command line to run: | |
# mongo --host <hostname> | |
docker run -it --rm --name mongoconsole --link ${CONTAINERNAME}:${CONTAINERNAME} mongo mongo --host ${CONTAINERNAME} |
This file contains hidden or 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
#!/bin/bash | |
usage() { | |
echo "Usage $0 -c mongo_docker_container_name -db db_name" | |
} | |
while [[ $# > 1 ]] | |
do | |
key="$1" | |
case $key in | |
-db|--database) | |
DBNAME="$2" | |
shift # past argument | |
;; | |
-c|--container) | |
CONTAINERNAME="$2" | |
shift # past argument | |
;; | |
*) | |
usage | |
exit 1 | |
;; | |
esac | |
shift # past argument or value | |
done | |
if [ -z "${DBNAME}" -o -z "${CONTAINERNAME}" ]; then | |
usage | |
exit 1 | |
fi | |
docker run -it --name mongodump --link ${CONTAINERNAME}:${CONTAINERNAME} mongo mongodump --db ${DBNAME} --host ${CONTAINERNAME} | |
docker cp mongodump:dump ./mongodump-$(date --iso-8601) | |
if [ $? -ne 0 ]; then | |
echo "MongoDB did not succeed taking a backup. Did you specify the correct database name?" | |
fi | |
# Clean up container | |
docker rm mongodump |
This file contains hidden or 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
#!/bin/bash | |
# Use > 1 to consume two arguments per pass in the loop (e.g. each | |
# argument has a corresponding value to go with it). | |
# Use > 0 to consume one or more arguments per pass in the loop (e.g. | |
# some arguments don't have a corresponding value to go with it) | |
usage() { | |
echo "Usage $0 -d /mongodump/dir -c mongo_docker_container_name" | |
} | |
while [[ $# > 1 ]] | |
do | |
key="$1" | |
case $key in | |
-d|--dump) | |
DUMPDIR="$2" | |
shift # past argument | |
;; | |
-c|--container) | |
CONTAINERNAME="$2" | |
shift # past argument | |
;; | |
*) | |
usage | |
exit 1 | |
;; | |
esac | |
shift # past argument or value | |
done | |
if [ -z "${DUMPDIR}" -o -z "${CONTAINERNAME}" ]; then | |
usage | |
exit 1 | |
fi | |
echo "Attempting to restore mongodb dump at ${DUMPDIR} into container ${CONTAINERNAME}" | |
read -r -p "Is this what you want? [y/N] " response | |
case $response in | |
[yY][eE][sS]|[yY]) | |
docker run -it --rm --name mongorestore -v ${DUMPDIR}:/var/dump --link ${CONTAINERNAME}:${CONTAINERNAME} mongo mongorestore --host ${CONTAINERNAME} /var/dump | |
;; | |
*) | |
echo "Nevermind then" | |
;; | |
esac |
I was getting the same error doing the docker dump but when adding the -o /tmp/dump worked.
I used the docker restore script without issues! 👍
the restore script needed a little tweaking on our environment to accomodate a separate Docker network for the MongoDB instance and also because since Mongo 3.0 you have to pass the --db
flag to mongorestore. Updated script is:
#!/bin/bash
# Use > 1 to consume two arguments per pass in the loop (e.g. each
# argument has a corresponding value to go with it).
# Use > 0 to consume one or more arguments per pass in the loop (e.g.
# some arguments don't have a corresponding value to go with it)
usage() {
echo "Usage $0 -d /mongodump/dir -c mongo_docker_container_name -n network_name -D dest_database"
}
while [[ $# > 1 ]]
do
key="$1"
case $key in
-d|--dump)
DUMPDIR="$2"
shift # past argument
;;
-c|--container)
CONTAINERNAME="$2"
shift # past argument
;;
-n|--network)
NETWORK="$2"
shift # past argument
;;
-D|--database)
DATABASE="$2"
shift # past argument
;;
*)
usage
exit 1
;;
esac
shift # past argument or value
done
if [ -z "${DUMPDIR}" -o -z "${CONTAINERNAME}" ]; then
usage
exit 1
fi
echo "Attempting to restore mongodb dump at ${DUMPDIR} into container ${CONTAINERNAME}"
read -r -p "Is this what you want? [y/N] " response
case $response in
[yY][eE][sS]|[yY])
docker run -it --rm --name mongorestore -v ${DUMPDIR}:/var/dump --link ${CONTAINERNAME}:${CONTAINERNAME} --network ${NETWORK} mongo mongorestore --host ${CONTAINERNAME} --db ${DATABASE} /var/dump
;;
*)
echo "Nevermind then"
;;
esac
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi @stanislvb
helpful scripts! thx.
getting a permission denied error on
mongodump
tried running it with
--privileged
but same effect.using
-o /tmp/dump
for themongorestore
command worked, but i wonder why it did not work in my case... i am running aminikube
(local kubernetes cluster) - maybe that's got something to do with it?cheers
_f
@