Skip to content

Commit

Permalink
Merge pull request #2 from silinternational/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
dalenewby authored Mar 20, 2023
2 parents 5a8132b + f94189c commit c3e13b2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
19 changes: 10 additions & 9 deletions application/backup.sh
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
#!/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)
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)
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"
6 changes: 4 additions & 2 deletions application/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
39 changes: 20 additions & 19 deletions application/restore.sh
Original file line number Diff line number Diff line change
@@ -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}"
result=$(psql --host=${DB_HOST} --dbname=postgres --username=${DB_ROOTUSER} --command="DROP DATABASE ${DB_NAME};")
echo "${MYNAME}: deleting database ${DB_NAME}"
result=$(psql --host=${DB_HOST} --dbname=postgres --username=${DB_USER} --command="DROP DATABASE ${DB_NAME};")
if [ "${result}" != "DROP DATABASE" ]; then
message="Create database command failed: ${result}"
echo "postgresql-backup-restore-fs: FATAL: ${message}"
message="Drop database command failed: ${result}"
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

0 comments on commit c3e13b2

Please sign in to comment.