Scripts to migrate MongoDB with Docker
At hola.cloud we migrated a MongoDB 3.6 between machines. To ensure we don't have any issues with versions, we used exactly the same version with the official Docker images. Here are a couple of scripts that have come in handy.
export.sh
#!/bin/bash
# check argument
if [ -z "${1:-}" ]; then
echo "Use: $0 <folder_name>"
exit 1
fi
BACKUP_DIR="$1"
mkdir -p "$BACKUP_DIR"
docker run --rm --network host \
-v $(pwd)/$BACKUP_DIR:/backup \
mongo:3.6 \
bash -c 'mongodump --host 127.0.0.1 --port 27017 --out /backup'
import.sh
#!/bin/bash
set -euo pipefail
# check argument
if [ -z "${1:-}" ]; then
echo "Use: $0 <backup_folder>"
exit 1
fi
BACKUP_DIR="$1"
if [ ! -d "$BACKUP_DIR" ]; then
echo "Error: folder '$BACKUP_DIR' does NOT exist"
exit 1
fi
docker run --rm --network host \
-v "$(pwd)/$BACKUP_DIR":/backup \
mongo:3.6 \
bash -c "mongorestore --host 127.0.0.1 --port 27017 /backup"
shell.sh
#!/bin/bash
set -euo pipefail
docker run -ti --rm --network host \
-v "$(pwd)/shell":/shell \
mongo:3.6 \
bash -c "mongo --host 127.0.0.1 --port 27017"
Comentarios