8 changed files with 106 additions and 6 deletions
@ -0,0 +1,19 @@ |
|||
#!/bin/bash |
|||
set -Eeuo pipefail |
|||
|
|||
export PGHOST=/var/run/postgresql |
|||
|
|||
BACKUP_DIR=/backup/$(date +%Y%m%d) |
|||
mkdir -p "$BACKUP_DIR" |
|||
|
|||
psql -c "SELECT datname FROM pg_database WHERE datname NOT IN ('template0', 'template1', 'postgres');" -t | while read db; do |
|||
if [ -z "$db" ]; then |
|||
continue |
|||
fi |
|||
|
|||
echo "Backup of database $db..." |
|||
pg_dump -c --if-exists "$db" | gzip -c > "$BACKUP_DIR/dump-$db.sql.gz" |
|||
done |
|||
echo "Complete backup of the whole PostgreSQL server..." |
|||
pg_basebackup -D "$BACKUP_DIR/pg_basebackup" |
|||
echo "Backups stored in $BACKUP_DIR" |
|||
@ -0,0 +1,46 @@ |
|||
[Unit] |
|||
Description=PostgreSQL Database Server - Backup |
|||
Documentation=https://hub.docker.com/_/postgres/ |
|||
After=network.target postgresql-server.service |
|||
Requires=postgresql-server.service |
|||
|
|||
# Start/stop this unit when the target is started/stopped |
|||
PartOf=postgresql.target |
|||
|
|||
[Container] |
|||
ContainerName=postgresql-backup-job |
|||
Image=docker.io/library/postgres:${PG_MAJOR}-alpine |
|||
|
|||
# Network configuration |
|||
Network=host |
|||
|
|||
# Those environment variables will be injected by podman into the container |
|||
EnvironmentFile=/etc/quadlets/postgresql/config.env |
|||
|
|||
# Use a custom backup script |
|||
Entrypoint=/usr/local/bin/backup.sh |
|||
User=postgres |
|||
|
|||
# Volume mounts |
|||
Volume=/var/lib/quadlets/postgresql:/var/lib/postgresql:z |
|||
Volume=/etc/quadlets/postgresql/backup.sh:/usr/local/bin/backup.sh:z,ro |
|||
|
|||
# This container is part of a Pod |
|||
Pod=postgresql.pod |
|||
|
|||
# Share /var/run/postgresql/ between containers in the pod for the Unix socket |
|||
Volume=/var/run/quadlets/postgresql:/var/run/postgresql:z |
|||
Volume=/var/lib/quadlets/postgresql/backup:/backup:z |
|||
|
|||
[Service] |
|||
Restart=no |
|||
TimeoutStartSec=600 |
|||
|
|||
# These environment variables are sourced to be used by systemd in the Exec* commands |
|||
EnvironmentFile=/etc/quadlets/postgresql/config.env |
|||
|
|||
# This container is a job - run once to completion |
|||
Type=oneshot |
|||
|
|||
# Skaffold filesystem + fix permissions |
|||
ExecStartPre=install -m 0700 -o 70 -g 70 -d /var/lib/quadlets/postgresql/backup |
|||
@ -0,0 +1,17 @@ |
|||
[Unit] |
|||
Description=PostgreSQL Database Server - Pod |
|||
Documentation=https://hub.docker.com/_/postgres/ |
|||
After=network.target |
|||
Before=postgresql.target |
|||
|
|||
# Only start if PostgreSQL has been configured |
|||
ConditionPathExists=/etc/quadlets/postgresql/config.env |
|||
|
|||
# Start/stop this unit when the target is started/stopped |
|||
PartOf=postgresql.target |
|||
|
|||
[Pod] |
|||
PodName=postgresql |
|||
|
|||
[Install] |
|||
WantedBy=postgresql.target |
|||
Loading…
Reference in new issue