Skip to content

Commit

Permalink
backup script
Browse files Browse the repository at this point in the history
  • Loading branch information
EnigmaCurry committed Oct 28, 2023
1 parent 6519f6e commit 936a312
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 16 deletions.
24 changes: 22 additions & 2 deletions _scripts/funcs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ get_root_domain() {
}

docker_compose() {
local ENV_FILE=${ENV_FILE:-.env_$(${BIN}/docker_context)}
local PROJECT_NAME=$(basename ${PWD})
check_var PROJECT_NAME ENV_FILE
echo "PROJECT_NAME=${PROJECT_NAME}"
echo "ENV_FILE=${ENV_FILE}"
if [[ -n "${instance:-${INSTANCE}}" ]] && [[ "${ENV_FILE}" != ".env_${DOCKER_CONTEXT}_${instance:-${INSTANCE}}" ]]; then
ENV_FILE="${ENV_FILE}_${instance:-${INSTANCE}}"
PROJECT_NAME="$(basename $PWD)_${instance:-${INSTANCE}}"
Expand Down Expand Up @@ -131,6 +131,26 @@ docker_exec() {
(set -ex; docker exec --env-file=${ENV_FILE} "$@")
}

docker_wait_for_healthcheck() {
local container_id=$1
check_var container_id
local attempts=150;
echo "## Waiting for container healthcheck: ${container_id}"
while [[ "${attempts}" -gt 0 ]]; do
if [[ "$(docker inspect -f {{.State.Health.Status}} $container_id)" == "healthy" ]]; then
echo "## HEALTHY - Container ${container_id} healthcheck passed."
exit 0
fi
if [ $(( attempts % 10 )) -eq 9 ]; then
echo "## Still waiting for container ${container_id} to start ..."
fi
attempts=$((attempts-1))
sleep 2;
done
echo "## UNHEALTHY - Container ${container_id} still has not started yet." >/dev/stderr
exit 1
}

ytt() {
set -e
docker image inspect localhost/ytt >/dev/null || docker build -t localhost/ytt -f- . >/dev/null <<'EOF'
Expand Down
3 changes: 3 additions & 0 deletions postgresql/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ RUN apt-get update && \
apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y pgbackrest && \

rm /etc/pgbackrest.conf && \
ln -s /etc/postgresql/pgbackrest.conf /etc/pgbackrest.conf && \

## Cleanup
cd /root && \
# rm -rf /src && \
Expand Down
2 changes: 1 addition & 1 deletion postgresql/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ psql-alias:

.PHONY: backup # Enable backups
backup:
@./backup.sh
@ENV_FILE="${ENV_FILE}" PROJECT_NAME="${PROJECT_NAME}" INSTANCE_SUFFIX="${INSTANCE_SUFFIX}" ./backup.sh
31 changes: 25 additions & 6 deletions postgresql/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,33 @@
BIN=../_scripts
source ${BIN}/funcs.sh

configure_pgbackrest() {
}
check_var ENV_FILE
echo ENV_FILE=${ENV_FILE}
echo INSTANCE=${INSTANCE}
echo INSTANCE_SUFFIX=${INSTANCE_SUFFIX}
export PROJECT_NAME
export INSTANCE
export INSTNACE_SUFFIX

This comment has been minimized.

Copy link
@mcmikemn

mcmikemn Oct 28, 2023

Collaborator

Typo in var name

export ENV_FILE

create_pgbackrest_stanza() {
docker_compose exec postgres sh -c "pgbackrest --stanza=apps --log-level-console=info stanza-create"
pgbackrest_cmd() {
local EXTRA_ARGS=("--stanza=apps" "--log-level-console=info")
if [[ "backup" == "$1" ]]; then
EXTRA_ARGS+=("--annotation=context=${DOCKER_CONTEXT}" "--annotation=instance=${INSTANCE}")
fi
EXTRA_ARGS="${EXTRA_ARGS[@]}"
docker_compose exec -u postgres postgres sh -c "set -x; pgbackrest ${EXTRA_ARGS} $@"
}

restart() {
make --no-print-directory restart
docker_wait_for_healthcheck $(make --no-print-directory docker-compose-lifecycle-cmd EXTRA_ARGS="ps -a postgres -q")
}

set -e
configure_pgbackrest
create_pgbackrest_stanza
pgbackrest_cmd stanza-create
pgbackrest_cmd check
pgbackrest_cmd backup
pgbackrest_cmd info

#pgbackrest_cmd restore
17 changes: 10 additions & 7 deletions postgresql/config/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,32 @@ create_config() {
echo "[ ! ] GENERATED NEW CONFIG FILE ::: ${CONFIG_DIR}/pg_hba.conf"

if [[ "${POSTGRES_PGBACKREST}" == "true" ]]; then
cat <<'EOF' >> /etc/postgresql/postgresql.conf
cat <<'EOF' >> postgresql.conf
archive_command = 'pgbackrest --stanza=demo archive-push %p'
archive_command = 'pgbackrest --stanza=apps archive-push %p'
archive_mode = on
max_wal_senders = 3
wal_level = replica
EOF
cat <<EOF > /etc/pgbackrest.conf
cat <<EOF > pgbackrest.conf
[apps]
pg1-path=/var/lib/postgresql/data
[global]
start-fast=y
repo1-block=y
repo1-bundle=y
repo1-path=/var/lib/pgbackrest
repo1-retention-full=2
EOF
if [[ -n "${POSTGRES_PGBACKREST_ENCRYPTION_PASSPHRASE}" ]]; then
cat <<EOF >> /etc/pgbackrest.conf
cat <<EOF >> pgbackrest.conf
repo1-cipher-pass=${POSTGRES_PGBACKREST_ENCRYPTION_PASSPHRASE}
repo1-cipher-type=aes-256-cbc
EOF
fi
cat <<EOF >> /etc/pgbackrest.conf
repo1-path=/var/lib/pgbackrest
repo1-retention-full=2
cat <<EOF >> pgbackrest.conf
[global:archive-push]
compress-level=3
Expand Down

0 comments on commit 936a312

Please sign in to comment.