Skip to content

Commit 5d6aac7

Browse files
authored
Merge pull request #3 from silinternational/develop
Remove rsyslog and Logentries
2 parents 54b35cb + 24b6778 commit 5d6aac7

File tree

7 files changed

+99
-66
lines changed

7 files changed

+99
-66
lines changed

Dockerfile

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
FROM alpine:3.8
1+
FROM alpine:3.11
22

33
RUN apk update \
4-
&& apk add --no-cache rsyslog rsyslog-tls \
5-
ca-certificates openssl \
6-
bash \
7-
postgresql \
8-
postgresql-client \
9-
python py-pip \
10-
&& update-ca-certificates \
4+
&& apk add --no-cache \
5+
bash \
6+
postgresql \
7+
postgresql-client \
8+
python py-pip \
119
&& pip install s3cmd python-magic
1210

13-
COPY dockerbuild/rsyslog.conf /etc/rsyslog.conf
14-
15-
RUN wget https://raw.githubusercontent.com/silinternational/runny/0.2/runny -O /usr/local/bin/runny \
16-
&& chmod +x /usr/local/bin/runny
17-
1811
COPY application/ /data/
1912
WORKDIR /data
2013

21-
ENTRYPOINT ["./entrypoint.sh"]
22-
CMD ["crond -f"]
14+
CMD ["./entrypoint.sh"]

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@ Service to backup and/or restore a PostgreSQL database using S3
88
4. Run a backup and check your bucket for that backup
99

1010
### Environment variables
11-
`LOGENTRIES_KEY`
12-
1311
`MODE=[backup|restore]`
1412

15-
`CRON_SCHEDULE="0 2 * * *"` _defaults to every day at 2:00 AM_ [syntax reference](https://en.wikipedia.org/wiki/Cron)
16-
1713
`DB_HOST=` hostname of the database server
1814

1915
`DB_NAME=` name of the database

application/backup.sh

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

3-
logger -p user.info "backing up ${DB_NAME}..."
3+
STATUS=0
44

5-
start=$(date +%s)
6-
runny $(PGPASSWORD=${DB_USERPASSWORD} pg_dump --host=${DB_HOST} --username=${DB_USER} --create --clean ${DB_OPTIONS} --dbname=${DB_NAME} > /tmp/${DB_NAME}.sql)
7-
end=$(date +%s)
5+
echo "postgresql-backup-restore: backup: Started"
86

9-
logger -p user.info "${DB_NAME} backed up ($(stat -c %s /tmp/${DB_NAME}.sql) bytes) in $(expr ${end} - ${start}) seconds."
7+
echo "postgresql-backup-restore: Backing up ${DB_NAME}"
108

11-
runny gzip -f /tmp/${DB_NAME}.sql
12-
runny s3cmd put /tmp/${DB_NAME}.sql.gz ${S3_BUCKET}
13-
# runny aws s3 cp /tmp/${DB_NAME}.sql.gz ${S3_BUCKET}
9+
start=$(date +%s)
10+
$(PGPASSWORD=${DB_USERPASSWORD} pg_dump --host=${DB_HOST} --username=${DB_USER} --create --clean ${DB_OPTIONS} --dbname=${DB_NAME} > /tmp/${DB_NAME}.sql) || STATUS=$?
11+
end=$(date +%s)
1412

15-
logger -p user.info "${DB_NAME} backup stored in ${S3_BUCKET}."
13+
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+
exit $STATUS
16+
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+
fi
19+
20+
start=$(date +%s)
21+
gzip -f /tmp/${DB_NAME}.sql || STATUS=$?
22+
end=$(date +%s)
23+
24+
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+
exit $STATUS
27+
else
28+
echo "postgresql-backup-restore: Compressing backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
29+
fi
30+
31+
start=$(date +%s)
32+
s3cmd put /tmp/${DB_NAME}.sql.gz ${S3_BUCKET} || STATUS=$?
33+
end=$(date +%s)
34+
35+
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+
exit $STATUS
38+
else
39+
echo "postgresql-backup-restore: Copy backup to ${S3_BUCKET} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
40+
fi
41+
42+
echo "postgresql-backup-restore: backup: Completed"

application/entrypoint.sh

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ echo ${DB_HOST}:*:*:${DB_USER}:${DB_USERPASSWORD} > /root/.pgpass
55
echo ${DB_HOST}:*:*:${DB_ROOTUSER}:${DB_ROOTPASSWORD} >> /root/.pgpass
66
chmod 600 /root/.pgpass
77

8-
if [ "${LOGENTRIES_KEY}" ]; then
9-
sed -i /etc/rsyslog.conf -e "s/LOGENTRIESKEY/${LOGENTRIES_KEY}/"
10-
rsyslogd
11-
sleep 10 # ensure rsyslogd is running before we may need to send logs to it
12-
else
13-
logger -p user.error "Missing LOGENTRIES_KEY environment variable"
14-
fi
8+
STATUS=0
9+
10+
case "${MODE}" in
11+
backup|restore)
12+
/data/${MODE}.sh || STATUS=$?
13+
;;
14+
*)
15+
echo postgresql-backup-restore: FATAL: Unknown MODE: ${MODE}
16+
exit 1
17+
esac
1518

16-
# default to every day at 2 am when no schedule is provided
17-
echo "${CRON_SCHEDULE:=0 2 * * *} runny /data/${MODE}.sh" >> /etc/crontabs/root
19+
if [ $STATUS -ne 0 ]; then
20+
echo postgresql-backup-restore: Non-zero exit: $STATUS
21+
fi
1822

19-
runny $1
23+
exit $STATUS

application/restore.sh

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

3+
STATUS=0
4+
5+
echo "postgresql-backup-restore: restore: Started"
6+
37
# Does the database exist?
4-
logger -p user.info "checking for DB ${DB_NAME}..."
8+
echo "postgresql-backup-restore: checking for DB ${DB_NAME}"
59
result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --list | grep ${DB_NAME})
610
if [ -z "${result}" ]; then
711
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}"
913
exit 1
1014
fi
1115

1216
# 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}"
1418
result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command='\du' | grep ${DB_USER})
1519
if [ -z "${result}" ]; then
1620
result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command="create role ${DB_USER} with login password '${DB_USERPASSWORD}' inherit;")
1721
if [ "${result}" != "CREATE ROLE" ]; then
1822
message="Create role command failed: ${result}"
19-
logger -p 1 -t application.crit "${message}"
23+
echo "postgresql-backup-restore: FATAL: ${message}"
2024
exit 1
2125
fi
2226
fi
2327

24-
logger -p user.info "changing DB ownership to ${DB_USER}..."
28+
echo "postgresql-backup-restore: changing DB ownership to ${DB_USER}"
2529
result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command="alter database ${DB_NAME} owner to ${DB_USER};")
2630
if [ "${result}" != "ALTER DATABASE" ]; then
2731
message="Alter database command failed: ${result}"
28-
logger -p 1 -t application.crit "${message}"
32+
echo "postgresql-backup-restore: FATAL: ${message}"
2933
exit 1
3034
fi
3135

32-
logger -p user.info "restoring ${DB_NAME}..."
36+
echo "postgresql-backup-restore: restoring ${DB_NAME}"
3337

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
3648

3749
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=$?
3951
end=$(date +%s)
4052

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

dockerbuild/rsyslog.conf

Lines changed: 0 additions & 16 deletions
This file was deleted.

local.env.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
LOGENTRIES_KEY=
21
AWS_ACCESS_KEY=
32
AWS_SECRET_KEY=
43
S3_BUCKET=

0 commit comments

Comments
 (0)