From e8be68ec5993ce73d998b9d1818b346bbb34b03d Mon Sep 17 00:00:00 2001 From: Dale Newby Date: Thu, 16 Mar 2023 11:34:41 -0400 Subject: [PATCH 1/3] Use a variable instead of hard-coded strings --- application/backup.sh | 19 ++++++++++--------- application/entrypoint.sh | 6 ++++-- application/restore.sh | 35 ++++++++++++++++++----------------- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/application/backup.sh b/application/backup.sh index 9ddbd7b..5049cd3 100755 --- a/application/backup.sh +++ b/application/backup.sh @@ -1,20 +1,21 @@ #!/usr/bin/env sh +MYNAME="postgresql-backup-restore-fs" STATUS=0 -echo "postgresql-backup-restore-fs: backup: Started" +echo "${MYNAME}: backup: Started" -echo "postgresql-backup-restore-fs: Backing up ${DB_NAME}" +echo "${MYNAME}: Backing up ${DB_NAME}" start=$(date +%s) $(PGPASSWORD=${DB_USERPASSWORD} pg_dump --host=${DB_HOST} --username=${DB_USER} --create --clean ${DB_OPTIONS} --dbname=${DB_NAME} > /tmp/${DB_NAME}.sql) || STATUS=$? end=$(date +%s) if [ $STATUS -ne 0 ]; then - echo "postgresql-backup-restore-fs: FATAL: Backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." + echo "${MYNAME}: FATAL: Backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." exit $STATUS else - echo "postgresql-backup-restore-fs: Backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds, ($(stat -c %s /tmp/${DB_NAME}.sql) bytes)." + echo "${MYNAME}: Backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds, ($(stat -c %s /tmp/${DB_NAME}.sql) bytes)." fi start=$(date +%s) @@ -22,10 +23,10 @@ gzip -f /tmp/${DB_NAME}.sql || STATUS=$? end=$(date +%s) if [ $STATUS -ne 0 ]; then - echo "postgresql-backup-restore-fs: FATAL: Compressing backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." + echo "${MYNAME}: FATAL: Compressing backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." exit $STATUS else - echo "postgresql-backup-restore-fs: Compressing backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds." + echo "${MYNAME}: Compressing backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds." fi start=$(date +%s) @@ -33,10 +34,10 @@ mv /tmp/${DB_NAME}.sql.gz ${BACKUP_DIR} || STATUS=$? end=$(date +%s) if [ $STATUS -ne 0 ]; then - echo "postgresql-backup-restore-fs: FATAL: Copy backup to ${BACKUP_DIR} of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." + echo "${MYNAME}: FATAL: Copy backup to ${BACKUP_DIR} of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." exit $STATUS else - echo "postgresql-backup-restore-fs: Copy backup to ${BACKUP_DIR} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds." + echo "${MYNAME}: Copy backup to ${BACKUP_DIR} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds." fi -echo "postgresql-backup-restore-fs: backup: Completed" +echo "${MYNAME}: backup: Completed" diff --git a/application/entrypoint.sh b/application/entrypoint.sh index 58b1109..808a763 100755 --- a/application/entrypoint.sh +++ b/application/entrypoint.sh @@ -1,5 +1,7 @@ #!/usr/bin/env sh +MYNAME="postgresql-backup-restore-fs" + # hostname:port:database:username:password echo ${DB_HOST}:*:*:${DB_USER}:${DB_USERPASSWORD} > /root/.pgpass echo ${DB_HOST}:*:*:${DB_ROOTUSER}:${DB_ROOTPASSWORD} >> /root/.pgpass @@ -12,12 +14,12 @@ case "${MODE}" in /data/${MODE}.sh || STATUS=$? ;; *) - echo postgresql-backup-restore-fs: FATAL: Unknown MODE: ${MODE} + echo ${MYNAME}: FATAL: Unknown MODE: ${MODE} exit 1 esac if [ $STATUS -ne 0 ]; then - echo postgresql-backup-restore-fs: Non-zero exit: $STATUS + echo ${MYNAME}: Non-zero exit: $STATUS fi exit $STATUS diff --git a/application/restore.sh b/application/restore.sh index 75fef7e..32b02a7 100755 --- a/application/restore.sh +++ b/application/restore.sh @@ -1,72 +1,73 @@ #!/usr/bin/env sh +MYNAME="postgresql-backup-restore-fs" STATUS=0 -echo "postgresql-backup-restore-fs: restore: Started" +echo "${MYNAME}: restore: Started" # Ensure the database user exists. -echo "postgresql-backup-restore-fs: checking for DB user ${DB_USER}" +echo "${MYNAME}: checking for DB user ${DB_USER}" result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command='\du' | grep ${DB_USER}) if [ -z "${result}" ]; then result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command="create role ${DB_USER} with login password '${DB_USERPASSWORD}' inherit;") if [ "${result}" != "CREATE ROLE" ]; then message="Create role command failed: ${result}" - echo "postgresql-backup-restore-fs: FATAL: ${message}" + echo "${MYNAME}: FATAL: ${message}" exit 1 fi fi # Delete database if it exists. -echo "postgresql-backup-restore-fs: checking for DB ${DB_NAME}" +echo "${MYNAME}: checking for DB ${DB_NAME}" result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --list | grep ${DB_NAME}) if [ -z "${result}" ]; then message="Database "${DB_NAME}" on host "${DB_HOST}" does not exist." - echo "postgresql-backup-restore-fs: INFO: ${message}" + echo "${MYNAME}: INFO: ${message}" else - echo "postgresql-backup-restore-fs: deleting database ${DB_NAME}" + echo "${MYNAME}: deleting database ${DB_NAME}" result=$(psql --host=${DB_HOST} --dbname=postgres --username=${DB_ROOTUSER} --command="DROP DATABASE ${DB_NAME};") if [ "${result}" != "DROP DATABASE" ]; then message="Create database command failed: ${result}" - echo "postgresql-backup-restore-fs: FATAL: ${message}" + echo "${MYNAME}: FATAL: ${message}" exit 1 fi fi -echo "postgresql-backup-restore-fs: copying database ${DB_NAME} backup from ${BACKUP_DIR}" +echo "${MYNAME}: copying database ${DB_NAME} backup from ${BACKUP_DIR}" start=$(date +%s) cp ${BACKUP_DIR}/${DB_NAME}.sql.gz /tmp/${DB_NAME}.sql.gz || STATUS=$? end=$(date +%s) if [ $STATUS -ne 0 ]; then - echo "postgresql-backup-restore-fs: FATAL: Copy backup of ${DB_NAME} from ${BACKUP_DIR} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." + echo "${MYNAME}: FATAL: Copy backup of ${DB_NAME} from ${BACKUP_DIR} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." exit $STATUS else - echo "postgresql-backup-restore-fs: Copy backup of ${DB_NAME} from ${BACKUP_DIR} completed in $(expr ${end} - ${start}) seconds." + echo "${MYNAME}: Copy backup of ${DB_NAME} from ${BACKUP_DIR} completed in $(expr ${end} - ${start}) seconds." fi -echo "postgresql-backup-restore-fs: decompressing backup of ${DB_NAME}" +echo "${MYNAME}: decompressing backup of ${DB_NAME}" start=$(date +%s) gunzip -f /tmp/${DB_NAME}.sql.gz || STATUS=$? end=$(date +%s) if [ $STATUS -ne 0 ]; then - echo "postgresql-backup-restore-fs: FATAL: Decompressing backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." + echo "${MYNAME}: FATAL: Decompressing backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." exit $STATUS else - echo "postgresql-backup-restore-fs: Decompressing backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds." + echo "${MYNAME}: Decompressing backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds." fi -echo "postgresql-backup-restore-fs: restoring ${DB_NAME}" +echo "${MYNAME}: restoring ${DB_NAME}" start=$(date +%s) psql --host=${DB_HOST} --username=${DB_ROOTUSER} --dbname=postgres ${DB_OPTIONS} < /tmp/${DB_NAME}.sql || STATUS=$? end=$(date +%s) if [ $STATUS -ne 0 ]; then - echo "postgresql-backup-restore-fs: FATAL: Restore of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." + echo "${MYNAME}: FATAL: Restore of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." exit $STATUS else - echo "postgresql-backup-restore-fs: Restore of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds." + echo "${MYNAME}: Restore of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds." fi -echo "postgresql-backup-restore-fs: restore: Completed" +echo "${MYNAME}: restore: Completed" exit $STATUS From 4bbba0026e554d4e7bc0e24699ecf7aa362173a9 Mon Sep 17 00:00:00 2001 From: Dale Newby Date: Thu, 16 Mar 2023 11:37:28 -0400 Subject: [PATCH 2/3] Typo in message --- application/restore.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/restore.sh b/application/restore.sh index 32b02a7..07224bf 100755 --- a/application/restore.sh +++ b/application/restore.sh @@ -27,7 +27,7 @@ else echo "${MYNAME}: deleting database ${DB_NAME}" result=$(psql --host=${DB_HOST} --dbname=postgres --username=${DB_ROOTUSER} --command="DROP DATABASE ${DB_NAME};") if [ "${result}" != "DROP DATABASE" ]; then - message="Create database command failed: ${result}" + message="Drop database command failed: ${result}" echo "${MYNAME}: FATAL: ${message}" exit 1 fi From f94189c8c056acc7bd99eddad80cd5d4f3f6d6a8 Mon Sep 17 00:00:00 2001 From: Dale Newby Date: Thu, 16 Mar 2023 11:48:18 -0400 Subject: [PATCH 3/3] Fix 'drop database' command The 'drop database' command in restore.sh was using user ${DB_ROOTUSER} and has been changed to use ${DB_USER}. --- application/restore.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/restore.sh b/application/restore.sh index 07224bf..07627d8 100755 --- a/application/restore.sh +++ b/application/restore.sh @@ -25,7 +25,7 @@ if [ -z "${result}" ]; then echo "${MYNAME}: INFO: ${message}" else echo "${MYNAME}: deleting database ${DB_NAME}" - result=$(psql --host=${DB_HOST} --dbname=postgres --username=${DB_ROOTUSER} --command="DROP DATABASE ${DB_NAME};") + result=$(psql --host=${DB_HOST} --dbname=postgres --username=${DB_USER} --command="DROP DATABASE ${DB_NAME};") if [ "${result}" != "DROP DATABASE" ]; then message="Drop database command failed: ${result}" echo "${MYNAME}: FATAL: ${message}"