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