From b99086b922c121c89a99540f9c01a4d98db347e3 Mon Sep 17 00:00:00 2001 From: Arnie Date: Fri, 19 Feb 2021 10:13:52 +0100 Subject: [PATCH] Update run script to include import command --- pgsql-db/run | 59 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/pgsql-db/run b/pgsql-db/run index 6d49318..bff772b 100755 --- a/pgsql-db/run +++ b/pgsql-db/run @@ -59,9 +59,45 @@ init() { __ask_to_start } +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} pg_restore --no-owner -Fc --host=${SERVICE_NAME} --password --dbname=${dbname} "/import/${name}" +} + migrate9to10() { local migrationVolume="pgsql-9-10-dump-migration" - local migrationpath="/pgsql-data-dump" + local migrationPath="/pgsql-data-dump" stop docker volume create ${migrationVolume} @@ -69,16 +105,20 @@ migrate9to10() { --name postgres_migration_from_9 \ -v ${DB_VOLUME}:/var/lib/postgresql/data \ -v ${PGSQL_DB_BIN}:/usr/lib/postgresql/${IMAGE_VERSION}/bin \ - -v ${migrationVolume}:${migrationpath} \ + -v ${migrationVolume}:${migrationPath} \ -e POSTGRES_USER=${DB_USER} \ -e POSTGRES_PASSWORD=${DB_PASSWORD} \ - mdillon/postgis:9 + mdillon/postgis:9.5 + + echo "Waiting for postgres to initialize" + sleep 10 docker exec -it \ postgres_migration_from_9 \ - bash -c "pg_dumpall > /migration/full.dump" + bash -c "pg_dumpall > ${migrationPath}/full.dump" if [[ $? -ne 0 ]]; then + docker stop postgres_migration_from_9 __err "Bad thing, don't continue, quitsies, bye" exit 137 fi @@ -92,15 +132,16 @@ migrate9to10() { --name postgres_migration_to_10 \ -v ${DB_VOLUME}:/var/lib/postgresql/data \ -v ${PGSQL_DB_BIN}:/usr/lib/postgresql/${IMAGE_VERSION}/bin \ - -v ${migrationVolume}:${migrationpath} \ + -v ${migrationVolume}:${migrationPath} \ -e POSTGRES_USER=${DB_USER} \ -e POSTGRES_PASSWORD=${DB_PASSWORD} \ - mdillon/postgis:10 \ - psql -d postgres -f ${migrationpath}/full.dump + mdillon/postgis:10 - sudo docker exec -it \ + echo "Waiting for postgres to initialize" + sleep 30 + docker exec -it \ postgres_migration_to_10 \ - psql -f /migration/full.dump + psql -f ${migrationPath}/full.dump docker stop postgres_migration_to_10