Add import script for mysql

This commit is contained in:
Arnie 2020-07-20 10:51:43 +02:00
parent 70cf118ddb
commit bb10ae775e

View File

@ -47,6 +47,44 @@ cmd() {
docker run --rm -it -v $PWD:/current --net ${DB_NETWORK} ${IMAGE_NAME}:${IMAGE_VERSION} $@ docker run --rm -it -v $PWD:/current --net ${DB_NETWORK} ${IMAGE_NAME}:${IMAGE_VERSION} $@
} }
import() {
local dbname="$1"
shift
local filename="$1"
shift
if [[ "${dbname}" == "" ]] || [[ "${filename}" == "" ]]; then
__err "You must provide database name and filename of the file you want to import"
__msg "E.g. ./run import my-database-name \"\$PWD/my-file.sql\""
return 137
fi
if [[ ! -f "${filename}" ]]; then
__err "The filename was not located at ${filename}"
return 137
fi
echo -e "${C_WARN}This will import the file located at \"${C_WHITE}${filename}${C_WARN}\" to a database named \"${C_WHITE}${dbname}${C_WARN}\"${C_NONE}"
echo
__warn "Do you want to continue? [y/(n)] "
read CONTINUE
if [[ "${CONTINUE}" != "y" ]]; then
__msg "Quiting..."
return 0
fi
local path=$(realpath "${filename}")
local name=$(basename "${path}")
docker run --rm -it -v "${path}":"/import/${name}" --net ${DB_NETWORK} ${IMAGE_NAME}:${IMAGE_VERSION} mysql -h ${SERVICE_NAME} -u root -p${DB_PASSWORD} "${dbname}" -e "source /import/${name}"
}
bash() { bash() {
cmd bash cmd bash
} }