Skip to content

Commit

Permalink
Merge pull request #3 from silinternational/develop
Browse files Browse the repository at this point in the history
Remove rsyslog and Logentries
  • Loading branch information
dalenewby authored Mar 11, 2020
2 parents 54b35cb + 24b6778 commit 5d6aac7
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 66 deletions.
22 changes: 7 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
FROM alpine:3.8
FROM alpine:3.11

RUN apk update \
&& apk add --no-cache rsyslog rsyslog-tls \
ca-certificates openssl \
bash \
postgresql \
postgresql-client \
python py-pip \
&& update-ca-certificates \
&& apk add --no-cache \
bash \
postgresql \
postgresql-client \
python py-pip \
&& pip install s3cmd python-magic

COPY dockerbuild/rsyslog.conf /etc/rsyslog.conf

RUN wget https://raw.githubusercontent.com/silinternational/runny/0.2/runny -O /usr/local/bin/runny \
&& chmod +x /usr/local/bin/runny

COPY application/ /data/
WORKDIR /data

ENTRYPOINT ["./entrypoint.sh"]
CMD ["crond -f"]
CMD ["./entrypoint.sh"]
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ Service to backup and/or restore a PostgreSQL database using S3
4. Run a backup and check your bucket for that backup

### Environment variables
`LOGENTRIES_KEY`

`MODE=[backup|restore]`

`CRON_SCHEDULE="0 2 * * *"` _defaults to every day at 2:00 AM_ [syntax reference](https://en.wikipedia.org/wiki/Cron)

`DB_HOST=` hostname of the database server

`DB_NAME=` name of the database
Expand Down
45 changes: 36 additions & 9 deletions application/backup.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
#!/usr/bin/env sh

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

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

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

runny gzip -f /tmp/${DB_NAME}.sql
runny s3cmd put /tmp/${DB_NAME}.sql.gz ${S3_BUCKET}
# runny aws s3 cp /tmp/${DB_NAME}.sql.gz ${S3_BUCKET}
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)

logger -p user.info "${DB_NAME} backup stored in ${S3_BUCKET}."
if [ $STATUS -ne 0 ]; then
echo "postgresql-backup-restore: FATAL: Backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
exit $STATUS
else
echo "postgresql-backup-restore: 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: FATAL: Compressing backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
exit $STATUS
else
echo "postgresql-backup-restore: Compressing backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
fi

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

if [ $STATUS -ne 0 ]; then
echo "postgresql-backup-restore: FATAL: Copy backup to ${S3_BUCKET} of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
exit $STATUS
else
echo "postgresql-backup-restore: Copy backup to ${S3_BUCKET} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
fi

echo "postgresql-backup-restore: backup: Completed"
24 changes: 14 additions & 10 deletions application/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ echo ${DB_HOST}:*:*:${DB_USER}:${DB_USERPASSWORD} > /root/.pgpass
echo ${DB_HOST}:*:*:${DB_ROOTUSER}:${DB_ROOTPASSWORD} >> /root/.pgpass
chmod 600 /root/.pgpass

if [ "${LOGENTRIES_KEY}" ]; then
sed -i /etc/rsyslog.conf -e "s/LOGENTRIESKEY/${LOGENTRIES_KEY}/"
rsyslogd
sleep 10 # ensure rsyslogd is running before we may need to send logs to it
else
logger -p user.error "Missing LOGENTRIES_KEY environment variable"
fi
STATUS=0

case "${MODE}" in
backup|restore)
/data/${MODE}.sh || STATUS=$?
;;
*)
echo postgresql-backup-restore: FATAL: Unknown MODE: ${MODE}
exit 1
esac

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

runny $1
exit $STATUS
53 changes: 42 additions & 11 deletions application/restore.sh
Original file line number Diff line number Diff line change
@@ -1,41 +1,72 @@
#!/usr/bin/env sh

STATUS=0

echo "postgresql-backup-restore: restore: Started"

# Does the database exist?
logger -p user.info "checking for DB ${DB_NAME}..."
echo "postgresql-backup-restore: 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."
logger -p 1 -t application.crit "${message}"
echo "postgresql-backup-restore: FATAL: ${message}"
exit 1
fi

# Ensure the database user exists.
logger -p user.info "checking for DB user ${DB_USER}..."
echo "postgresql-backup-restore: 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}"
logger -p 1 -t application.crit "${message}"
echo "postgresql-backup-restore: FATAL: ${message}"
exit 1
fi
fi

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

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

runny s3cmd get -f ${S3_BUCKET}/${DB_NAME}.sql.gz /tmp/${DB_NAME}.sql.gz
runny gunzip -f /tmp/${DB_NAME}.sql.gz
start=$(date +%s)
s3cmd get -f ${S3_BUCKET}/${DB_NAME}.sql.gz /tmp/${DB_NAME}.sql.gz || STATUS=$?
end=$(date +%s)

if [ $STATUS -ne 0 ]; then
echo "postgresql-backup-restore: FATAL: Copy backup of ${DB_NAME} from ${S3_BUCKET} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
exit $STATUS
else
echo "postgresql-backup-restore: Copy backup of ${DB_NAME} from ${S3_BUCKET} completed in $(expr ${end} - ${start}) seconds."
fi

start=$(date +%s)
runny psql --host=${DB_HOST} --username=${DB_USER} --dbname=${DB_NAME} ${DB_OPTIONS} < /tmp/${DB_NAME}.sql
gunzip -f /tmp/${DB_NAME}.sql.gz || STATUS=$?
end=$(date +%s)

logger -p user.info "${DB_NAME} restored in $(expr ${end} - ${start}) seconds."
if [ $STATUS -ne 0 ]; then
echo "postgresql-backup-restore: FATAL: Decompressing backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
exit $STATUS
else
echo "postgresql-backup-restore: Decompressing backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
fi

start=$(date +%s)
psql --host=${DB_HOST} --username=${DB_USER} --dbname=${DB_NAME} ${DB_OPTIONS} < /tmp/${DB_NAME}.sql || STATUS=$?
end=$(date +%s)

if [ $STATUS -ne 0 ]; then
echo "postgresql-backup-restore: FATAL: Restore of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
exit $STATUS
else
echo "postgresql-backup-restore: Restore of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
fi

echo "postgresql-backup-restore: restore: Completed"
exit $STATUS
16 changes: 0 additions & 16 deletions dockerbuild/rsyslog.conf

This file was deleted.

1 change: 0 additions & 1 deletion local.env.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
LOGENTRIES_KEY=
AWS_ACCESS_KEY=
AWS_SECRET_KEY=
S3_BUCKET=

0 comments on commit 5d6aac7

Please sign in to comment.