-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from silinternational/develop
Develop
- Loading branch information
Showing
3 changed files
with
34 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |