Skip to content
This repository has been archived by the owner on May 20, 2022. It is now read-only.

Commit

Permalink
Fix wal-e for existing database
Browse files Browse the repository at this point in the history
  • Loading branch information
pichouk committed Feb 5, 2018
1 parent bce7fd0 commit 6244f03
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
2 changes: 2 additions & 0 deletions db/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM postgres:9.4-alpine

ENV DEFAULT_TIMEZONE UTC

# Install some packages to use WAL
RUN apk add --no-cache \
build-base \
Expand Down
50 changes: 34 additions & 16 deletions db/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,57 @@ export ARCHIVE_MODE=${ARCHIVE_MODE:-off}
export ARCHIVE_TIMEOUT=${ARCHIVE_TIMEOUT:-60}

function update_conf () {
wal=$1
# PGDATA is defined in upstream postgres dockerfile
if [ -f $PGDATA/postgresql.conf ]; then
sed -i "s/wal_level =.*$/wal_level = $WAL_LEVEL/g" $PGDATA/postgresql.conf
sed -i "s/archive_mode =.*$/archive_mode = $ARCHIVE_MODE/g" $PGDATA/postgresql.conf
sed -i "s/archive_timeout =.*$/archive_timeout = $ARCHIVE_TIMEOUT/g" $PGDATA/postgresql.conf
config_file=$PGDATA/postgresql.conf

# Check if configuration file exists. If not, it probably means that database is not initialized yet
if [ ! -f $config_file ]; then
return
fi
# Reinitialize config
sed -i "s/log_timezone =.*$//g" $PGDATA/postgresql.conf
sed -i "s/timezone =.*$//g" $PGDATA/postgresql.conf
sed -i "s/wal_level =.*$//g" $config_file
sed -i "s/archive_mode =.*$//g" $config_file
sed -i "s/archive_timeout =.*$//g" $config_file
sed -i "s/archive_command =.*$//g" $config_file

# Configure wal-e
if [ "$wal" = true ] ; then
/docker-entrypoint-initdb.d/setup-wale.sh
fi
echo "log_timezone = $DEFAULT_TIMEZONE" >> $config_file
echo "timezone = $DEFAULT_TIMEZONE" >> $config_file
}

if [ "${1:0:1}" = '-' ]; then
set -- postgres "$@"
fi

if [ "$1" = 'postgres' ]; then
# Get wal-e variables
# Check wal-e variables
wal_enable=true
VARS=(AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY WALE_S3_PREFIX AWS_REGION)
for v in ${VARS[@]}; do
if [ "${!v}" = "" ]; then
echo "$v is required for Wal-E but not set. Skipping Wal-E setup."
update_conf
# Run the postgresql entrypoint
. /docker-entrypoint.sh
exit
fi
wal_enable=false
fi
done

# Setup wal-e env variables
for v in ${VARS[@]}; do
export $v="${!v}"
done
WAL_LEVEL=archive
ARCHIVE_MODE=on
if [ "$wal_enable" = true ] ; then
for v in ${VARS[@]}; do
export $v="${!v}"
done
WAL_LEVEL=archive
ARCHIVE_MODE=on
fi

# Update postgresql configuration
update_conf $wal_enable

update_conf
# Run the postgresql entrypoint
. /docker-entrypoint.sh
fi
2 changes: 1 addition & 1 deletion db/setup-wale.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

# wal-e specific
# wal-e specific configuration
echo "wal_level = $WAL_LEVEL" >> $PGDATA/postgresql.conf
echo "archive_mode = $ARCHIVE_MODE" >> $PGDATA/postgresql.conf
echo "archive_command = '/usr/bin/wal-e wal-push %p'" >> $PGDATA/postgresql.conf
Expand Down

0 comments on commit 6244f03

Please sign in to comment.