Skip to content

Commit edbf8a5

Browse files
authored
Merge pull request #6 from silinternational/develop
Develop
2 parents 3d96b56 + 1772c38 commit edbf8a5

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

application/backup.sh

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
11
#!/usr/bin/env sh
22

3+
MYNAME="postgresql-backup-restore"
34
STATUS=0
45

5-
echo "postgresql-backup-restore: backup: Started"
6+
echo "${MYNAME}: backup: Started"
67

7-
echo "postgresql-backup-restore: Backing up ${DB_NAME}"
8+
echo "${MYNAME}: Backing up ${DB_NAME}"
89

910
start=$(date +%s)
1011
$(PGPASSWORD=${DB_USERPASSWORD} pg_dump --host=${DB_HOST} --username=${DB_USER} --create --clean ${DB_OPTIONS} --dbname=${DB_NAME} > /tmp/${DB_NAME}.sql) || STATUS=$?
1112
end=$(date +%s)
1213

1314
if [ $STATUS -ne 0 ]; then
14-
echo "postgresql-backup-restore: FATAL: Backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
15+
echo "${MYNAME}: FATAL: Backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
1516
exit $STATUS
1617
else
17-
echo "postgresql-backup-restore: Backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds, ($(stat -c %s /tmp/${DB_NAME}.sql) bytes)."
18+
echo "${MYNAME}: Backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds, ($(stat -c %s /tmp/${DB_NAME}.sql) bytes)."
1819
fi
1920

2021
start=$(date +%s)
2122
gzip -f /tmp/${DB_NAME}.sql || STATUS=$?
2223
end=$(date +%s)
2324

2425
if [ $STATUS -ne 0 ]; then
25-
echo "postgresql-backup-restore: FATAL: Compressing backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
26+
echo "${MYNAME}: FATAL: Compressing backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
2627
exit $STATUS
2728
else
28-
echo "postgresql-backup-restore: Compressing backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
29+
echo "${MYNAME}: Compressing backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
2930
fi
3031

3132
start=$(date +%s)
3233
s3cmd put /tmp/${DB_NAME}.sql.gz ${S3_BUCKET} || STATUS=$?
3334
end=$(date +%s)
3435

3536
if [ $STATUS -ne 0 ]; then
36-
echo "postgresql-backup-restore: FATAL: Copy backup to ${S3_BUCKET} of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
37+
echo "${MYNAME}: FATAL: Copy backup to ${S3_BUCKET} of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
3738
exit $STATUS
3839
else
39-
echo "postgresql-backup-restore: Copy backup to ${S3_BUCKET} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
40+
echo "${MYNAME}: Copy backup to ${S3_BUCKET} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
4041
fi
4142

42-
echo "postgresql-backup-restore: backup: Completed"
43+
echo "${MYNAME}: backup: Completed"

application/entrypoint.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env sh
22

3+
MYNAME="postgresql-backup-restore"
4+
35
# hostname:port:database:username:password
46
echo ${DB_HOST}:*:*:${DB_USER}:${DB_USERPASSWORD} > /root/.pgpass
57
echo ${DB_HOST}:*:*:${DB_ROOTUSER}:${DB_ROOTPASSWORD} >> /root/.pgpass
@@ -12,12 +14,12 @@ case "${MODE}" in
1214
/data/${MODE}.sh || STATUS=$?
1315
;;
1416
*)
15-
echo postgresql-backup-restore: FATAL: Unknown MODE: ${MODE}
17+
echo ${MYNAME}: FATAL: Unknown MODE: ${MODE}
1618
exit 1
1719
esac
1820

1921
if [ $STATUS -ne 0 ]; then
20-
echo postgresql-backup-restore: Non-zero exit: $STATUS
22+
echo ${MYNAME}: Non-zero exit: $STATUS
2123
fi
2224

2325
exit $STATUS

application/restore.sh

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,73 @@
11
#!/usr/bin/env sh
22

3+
MYNAME="postgresql-backup-restore"
34
STATUS=0
45

5-
echo "postgresql-backup-restore: restore: Started"
6+
echo "${MYNAME}: restore: Started"
67

78
# 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}"
910
result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command='\du' | grep ${DB_USER})
1011
if [ -z "${result}" ]; then
1112
result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command="create role ${DB_USER} with login password '${DB_USERPASSWORD}' inherit;")
1213
if [ "${result}" != "CREATE ROLE" ]; then
1314
message="Create role command failed: ${result}"
14-
echo "postgresql-backup-restore: FATAL: ${message}"
15+
echo "${MYNAME}: FATAL: ${message}"
1516
exit 1
1617
fi
1718
fi
1819

1920
# Delete database if it exists.
20-
echo "postgresql-backup-restore: checking for DB ${DB_NAME}"
21+
echo "${MYNAME}: checking for DB ${DB_NAME}"
2122
result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --list | grep ${DB_NAME})
2223
if [ -z "${result}" ]; then
2324
message="Database "${DB_NAME}" on host "${DB_HOST}" does not exist."
24-
echo "postgresql-backup-restore: INFO: ${message}"
25+
echo "${MYNAME}: INFO: ${message}"
2526
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};")
2829
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}"
3132
exit 1
3233
fi
3334
fi
3435

35-
echo "postgresql-backup-restore: copying database ${DB_NAME} backup from ${S3_BUCKET}"
36+
echo "${MYNAME}: copying database ${DB_NAME} backup from ${S3_BUCKET}"
3637
start=$(date +%s)
3738
s3cmd get -f ${S3_BUCKET}/${DB_NAME}.sql.gz /tmp/${DB_NAME}.sql.gz || STATUS=$?
3839
end=$(date +%s)
3940

4041
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."
4243
exit $STATUS
4344
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."
4546
fi
4647

47-
echo "postgresql-backup-restore: decompressing backup of ${DB_NAME}"
48+
echo "${MYNAME}: decompressing backup of ${DB_NAME}"
4849
start=$(date +%s)
4950
gunzip -f /tmp/${DB_NAME}.sql.gz || STATUS=$?
5051
end=$(date +%s)
5152

5253
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."
5455
exit $STATUS
5556
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."
5758
fi
5859

59-
echo "postgresql-backup-restore: restoring ${DB_NAME}"
60+
echo "${MYNAME}: restoring ${DB_NAME}"
6061
start=$(date +%s)
6162
psql --host=${DB_HOST} --username=${DB_ROOTUSER} --dbname=postgres ${DB_OPTIONS} < /tmp/${DB_NAME}.sql || STATUS=$?
6263
end=$(date +%s)
6364

6465
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."
6667
exit $STATUS
6768
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."
6970
fi
7071

71-
echo "postgresql-backup-restore: restore: Completed"
72+
echo "${MYNAME}: restore: Completed"
7273
exit $STATUS

0 commit comments

Comments
 (0)