|
1 | 1 | #!/usr/bin/env sh
|
2 | 2 |
|
| 3 | +MYNAME="postgresql-backup-restore" |
3 | 4 | STATUS=0
|
4 | 5 |
|
5 |
| -echo "postgresql-backup-restore: restore: Started" |
| 6 | +echo "${MYNAME}: restore: Started" |
6 | 7 |
|
7 | 8 | # Ensure the database user exists.
|
8 |
| -echo "postgresql-backup-restore: checking for DB user ${DB_USER}" |
| 9 | +echo "${MYNAME}: checking for DB user ${DB_USER}" |
9 | 10 | result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command='\du' | grep ${DB_USER})
|
10 | 11 | if [ -z "${result}" ]; then
|
11 | 12 | result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command="create role ${DB_USER} with login password '${DB_USERPASSWORD}' inherit;")
|
12 | 13 | if [ "${result}" != "CREATE ROLE" ]; then
|
13 | 14 | message="Create role command failed: ${result}"
|
14 |
| - echo "postgresql-backup-restore: FATAL: ${message}" |
| 15 | + echo "${MYNAME}: FATAL: ${message}" |
15 | 16 | exit 1
|
16 | 17 | fi
|
17 | 18 | fi
|
18 | 19 |
|
19 | 20 | # Delete database if it exists.
|
20 |
| -echo "postgresql-backup-restore: checking for DB ${DB_NAME}" |
| 21 | +echo "${MYNAME}: checking for DB ${DB_NAME}" |
21 | 22 | result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --list | grep ${DB_NAME})
|
22 | 23 | if [ -z "${result}" ]; then
|
23 | 24 | message="Database "${DB_NAME}" on host "${DB_HOST}" does not exist."
|
24 |
| - echo "postgresql-backup-restore: INFO: ${message}" |
| 25 | + echo "${MYNAME}: INFO: ${message}" |
25 | 26 | else
|
26 |
| - echo "postgresql-backup-restore: deleting database ${DB_NAME}" |
27 |
| - result=$(psql --host=${DB_HOST} --dbname=postgres --username=${DB_ROOTUSER} --command="DROP DATABASE ${DB_NAME};") |
| 27 | + echo "${MYNAME}: deleting database ${DB_NAME}" |
| 28 | + result=$(psql --host=${DB_HOST} --dbname=postgres --username=${DB_USER} --command="DROP DATABASE ${DB_NAME};") |
28 | 29 | if [ "${result}" != "DROP DATABASE" ]; then
|
29 |
| - message="Create database command failed: ${result}" |
30 |
| - echo "postgresql-backup-restore: FATAL: ${message}" |
| 30 | + message="Drop database command failed: ${result}" |
| 31 | + echo "${MYNAME}: FATAL: ${message}" |
31 | 32 | exit 1
|
32 | 33 | fi
|
33 | 34 | fi
|
34 | 35 |
|
35 |
| -echo "postgresql-backup-restore: copying database ${DB_NAME} backup from ${S3_BUCKET}" |
| 36 | +echo "${MYNAME}: copying database ${DB_NAME} backup from ${S3_BUCKET}" |
36 | 37 | start=$(date +%s)
|
37 | 38 | s3cmd get -f ${S3_BUCKET}/${DB_NAME}.sql.gz /tmp/${DB_NAME}.sql.gz || STATUS=$?
|
38 | 39 | end=$(date +%s)
|
39 | 40 |
|
40 | 41 | if [ $STATUS -ne 0 ]; then
|
41 |
| - echo "postgresql-backup-restore: FATAL: Copy backup of ${DB_NAME} from ${S3_BUCKET} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." |
| 42 | + echo "${MYNAME}: FATAL: Copy backup of ${DB_NAME} from ${S3_BUCKET} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." |
42 | 43 | exit $STATUS
|
43 | 44 | else
|
44 |
| - echo "postgresql-backup-restore: Copy backup of ${DB_NAME} from ${S3_BUCKET} completed in $(expr ${end} - ${start}) seconds." |
| 45 | + echo "${MYNAME}: Copy backup of ${DB_NAME} from ${S3_BUCKET} completed in $(expr ${end} - ${start}) seconds." |
45 | 46 | fi
|
46 | 47 |
|
47 |
| -echo "postgresql-backup-restore: decompressing backup of ${DB_NAME}" |
| 48 | +echo "${MYNAME}: decompressing backup of ${DB_NAME}" |
48 | 49 | start=$(date +%s)
|
49 | 50 | gunzip -f /tmp/${DB_NAME}.sql.gz || STATUS=$?
|
50 | 51 | end=$(date +%s)
|
51 | 52 |
|
52 | 53 | if [ $STATUS -ne 0 ]; then
|
53 |
| - echo "postgresql-backup-restore: FATAL: Decompressing backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." |
| 54 | + echo "${MYNAME}: FATAL: Decompressing backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." |
54 | 55 | exit $STATUS
|
55 | 56 | else
|
56 |
| - echo "postgresql-backup-restore: Decompressing backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds." |
| 57 | + echo "${MYNAME}: Decompressing backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds." |
57 | 58 | fi
|
58 | 59 |
|
59 |
| -echo "postgresql-backup-restore: restoring ${DB_NAME}" |
| 60 | +echo "${MYNAME}: restoring ${DB_NAME}" |
60 | 61 | start=$(date +%s)
|
61 | 62 | psql --host=${DB_HOST} --username=${DB_ROOTUSER} --dbname=postgres ${DB_OPTIONS} < /tmp/${DB_NAME}.sql || STATUS=$?
|
62 | 63 | end=$(date +%s)
|
63 | 64 |
|
64 | 65 | if [ $STATUS -ne 0 ]; then
|
65 |
| - echo "postgresql-backup-restore: FATAL: Restore of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." |
| 66 | + echo "${MYNAME}: FATAL: Restore of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." |
66 | 67 | exit $STATUS
|
67 | 68 | else
|
68 |
| - echo "postgresql-backup-restore: Restore of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds." |
| 69 | + echo "${MYNAME}: Restore of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds." |
69 | 70 | fi
|
70 | 71 |
|
71 |
| -echo "postgresql-backup-restore: restore: Completed" |
| 72 | +echo "${MYNAME}: restore: Completed" |
72 | 73 | exit $STATUS
|
0 commit comments