mirror of
https://github.com/prodrigestivill/docker-postgres-backup-local.git
synced 2026-06-09 21:17:59 +00:00
Implement hooks with run-parts #58
This commit is contained in:
@@ -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. |
|
| 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`. |
|
| 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"). |
|
| 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 an error or after a successful backup (POST with a JSON payload, check `hooks/00-webhook.sh` file for more info). Default disabled. |
|
||||||
| WEBHOOK_URL | URL to be called after a successful backup. |
|
| WEBHOOK_EXTRA_ARGS | Extra arguments for the `curl` execution in the webhook (check `hooks/00-webhook.sh` file for more info). |
|
||||||
|
|
||||||
#### Special Environment Variables
|
#### Special Environment Variables
|
||||||
|
|
||||||
@@ -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_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).
|
* 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
|
### Manual Backups
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -31,7 +31,7 @@ ENV POSTGRES_DB="**None**" \
|
|||||||
WEBHOOK_URL="**None**" \
|
WEBHOOK_URL="**None**" \
|
||||||
WEBHOOK_EXTRA_ARGS=""
|
WEBHOOK_EXTRA_ARGS=""
|
||||||
|
|
||||||
COPY backup.sh /backup.sh
|
COPY backup.sh hooks /
|
||||||
|
|
||||||
VOLUME /backups
|
VOLUME /backups
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -Eeo pipefail
|
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
|
if [ "${POSTGRES_DB}" = "**None**" -a "${POSTGRES_DB_FILE}" = "**None**" ]; then
|
||||||
echo "You need to set the POSTGRES_DB or POSTGRES_DB_FILE environment variable."
|
echo "You need to set the POSTGRES_DB or POSTGRES_DB_FILE environment variable."
|
||||||
exit 1
|
exit 1
|
||||||
@@ -60,6 +68,11 @@ KEEP_DAYS=${BACKUP_KEEP_DAYS}
|
|||||||
KEEP_WEEKS=`expr $(((${BACKUP_KEEP_WEEKS} * 7) + 1))`
|
KEEP_WEEKS=`expr $(((${BACKUP_KEEP_WEEKS} * 7) + 1))`
|
||||||
KEEP_MONTHS=`expr $(((${BACKUP_KEEP_MONTHS} * 31) + 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
|
#Initialize dirs
|
||||||
mkdir -p "${BACKUP_DIR}/last/" "${BACKUP_DIR}/daily/" "${BACKUP_DIR}/weekly/" "${BACKUP_DIR}/monthly/"
|
mkdir -p "${BACKUP_DIR}/last/" "${BACKUP_DIR}/daily/" "${BACKUP_DIR}/weekly/" "${BACKUP_DIR}/monthly/"
|
||||||
|
|
||||||
@@ -126,12 +139,7 @@ done
|
|||||||
|
|
||||||
echo "SQL backup created successfully"
|
echo "SQL backup created successfully"
|
||||||
|
|
||||||
if [ "${WEBHOOK_URL}" != "**None**" ]; then
|
# Post-backup hook
|
||||||
echo "Execute post-backup webhook call to ${WEBHOOK_URL}"
|
if [ -d "${HOOKS_DIR}" ]; then
|
||||||
curl --request POST \
|
run-parts -a "post-backup" --reverse --exit-on-error "${HOOKS_DIR}"
|
||||||
--url "${WEBHOOK_URL}" \
|
|
||||||
--max-time 10 \
|
|
||||||
--retry 5 \
|
|
||||||
${WEBHOOK_EXTRA_ARGS}
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
|
|||||||
+1
-1
@@ -46,7 +46,7 @@ ENV POSTGRES_DB="**None**" \
|
|||||||
WEBHOOK_URL="**None**" \
|
WEBHOOK_URL="**None**" \
|
||||||
WEBHOOK_EXTRA_ARGS=""
|
WEBHOOK_EXTRA_ARGS=""
|
||||||
|
|
||||||
COPY backup.sh /backup.sh
|
COPY backup.sh hooks /
|
||||||
|
|
||||||
VOLUME /backups
|
VOLUME /backups
|
||||||
|
|
||||||
|
|||||||
Executable
+34
@@ -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
|
||||||
Reference in New Issue
Block a user