Skip to content

Commit

Permalink
Implement hooks with run-parts prodrigestivill#58
Browse files Browse the repository at this point in the history
  • Loading branch information
prodrigestivill committed Aug 20, 2022
1 parent 1f46aba commit 67ecbb1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ Most variables are the same as in the [official postgres image](https://hub.dock
| POSTGRES_USER_FILE | Alternative to POSTGRES_USER, for usage with docker secrets. |
| SCHEDULE | [Cron-schedule](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules) specifying the interval between postgres backups. Defaults to `@daily`. |
| TZ | [POSIX TZ variable](https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html) specifying the timezone used to evaluate SCHEDULE cron (example "Europe/Paris"). |
| WEBHOOK_EXTRA_URL | Extra arguments for the webhook. |
| WEBHOOK_URL | URL to be called after a successful backup. |
| WEBHOOK_URL | URL to be called after an error or after a successful backup (POST with a JSON payload, check `hooks/00-webhook.sh` file for more info). Default disabled. |
| WEBHOOK_EXTRA_ARGS | Extra arguments for the `curl` execution in the webhook (check `hooks/00-webhook.sh` file for more info). |

#### Special Environment Variables

Expand Down Expand Up @@ -132,6 +132,13 @@ To do so it is using the following independent variables:
* BACKUP_KEEP_WEEKS: will remove files from the `weekly` folder that are older than its value in weeks after a new successfull backup (remember that it starts counting from the end of each week not the beggining).
* BACKUP_KEEP_MONTHS: will remove files from the `monthly` folder that are older than its value in months (of 31 days) after a new successfull backup (remember that it starts counting from the end of each month not the beggining).

### Hooks

The folder `hooks` inside the container can contain hooks/scripts to be run in differrent cases getting the exact situation as a first argument (`error`, `pre-backup` or `post-backup`).

Just create an script in that folder with execution permission so that [run-parts](https://manpages.debian.org/stable/debianutils/run-parts.8.en.html) can execute it on each state change.

Please, as an example take a look in the script already present there that implements the `WEBHOOK_URL` functionality.

### Manual Backups

Expand Down
2 changes: 1 addition & 1 deletion alpine.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ ENV POSTGRES_DB="**None**" \
WEBHOOK_URL="**None**" \
WEBHOOK_EXTRA_ARGS=""

COPY backup.sh /backup.sh
COPY backup.sh hooks /

VOLUME /backups

Expand Down
24 changes: 16 additions & 8 deletions backup.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#!/usr/bin/env bash
set -Eeo pipefail

HOOKS_DIR="/hooks"
if [ -d "${HOOKS_DIR}" ]; then
on_error(){
run-parts -a "error" "${HOOKS_DIR}"
}
trap 'on_error' ERR
fi

if [ "${POSTGRES_DB}" = "**None**" -a "${POSTGRES_DB_FILE}" = "**None**" ]; then
echo "You need to set the POSTGRES_DB or POSTGRES_DB_FILE environment variable."
exit 1
Expand Down Expand Up @@ -60,6 +68,11 @@ KEEP_DAYS=${BACKUP_KEEP_DAYS}
KEEP_WEEKS=`expr $(((${BACKUP_KEEP_WEEKS} * 7) + 1))`
KEEP_MONTHS=`expr $(((${BACKUP_KEEP_MONTHS} * 31) + 1))`

# Pre-backup hook
if [ -d "${HOOKS_DIR}" ]; then
run-parts -a "pre-backup" --exit-on-error "${HOOKS_DIR}"
fi

#Initialize dirs
mkdir -p "${BACKUP_DIR}/last/" "${BACKUP_DIR}/daily/" "${BACKUP_DIR}/weekly/" "${BACKUP_DIR}/monthly/"

Expand Down Expand Up @@ -126,12 +139,7 @@ done

echo "SQL backup created successfully"

if [ "${WEBHOOK_URL}" != "**None**" ]; then
echo "Execute post-backup webhook call to ${WEBHOOK_URL}"
curl --request POST \
--url "${WEBHOOK_URL}" \
--max-time 10 \
--retry 5 \
${WEBHOOK_EXTRA_ARGS}
exit 1
# Post-backup hook
if [ -d "${HOOKS_DIR}" ]; then
run-parts -a "post-backup" --reverse --exit-on-error "${HOOKS_DIR}"
fi
2 changes: 1 addition & 1 deletion debian.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ENV POSTGRES_DB="**None**" \
WEBHOOK_URL="**None**" \
WEBHOOK_EXTRA_ARGS=""

COPY backup.sh /backup.sh
COPY backup.sh hooks /

VOLUME /backups

Expand Down
34 changes: 34 additions & 0 deletions hooks/00-webhook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

set -e

# Possible actions: error, pre-backup, post-backup
ACTION="${1}"

if [ "${WEBHOOK_URL}" != "**None**" ]; then
case "${ACTION}" in
"error")
echo "Execute error webhook call to ${WEBHOOK_URL}"
curl --request POST \
--url "${WEBHOOK_URL}" \
--header 'Content-Type: application/json' \
--data '{"status": "error"}' \
--max-time 10 \
--retry 5 \
${WEBHOOK_EXTRA_ARGS}
;;
# "pre-backup")
# echo "Nothing to do"
# ;;
"post-backup")
echo "Execute post-backup webhook call to ${WEBHOOK_URL}"
curl --request POST \
--url "${WEBHOOK_URL}" \
--header 'Content-Type: application/json' \
--data '{"status": "post-backup"}' \
--max-time 10 \
--retry 5 \
${WEBHOOK_EXTRA_ARGS}
;;
esac
fi

0 comments on commit 67ecbb1

Please sign in to comment.