9 changed files with 60 additions and 37 deletions
@ -0,0 +1,37 @@ |
|||||
|
#!/bin/bash |
||||
|
|
||||
|
set -Eeuo pipefail |
||||
|
|
||||
|
last_backup="" |
||||
|
for f in /var/lib/postgresql/backup/*/backup_manifest; do |
||||
|
# If there are no backups, the glob pattern above won't match any files |
||||
|
if [ ! -f "$f" ]; then |
||||
|
continue |
||||
|
fi |
||||
|
|
||||
|
# Check if this is the most recent backup |
||||
|
if [ -z "$last_backup" ] || [ "$f" -nt "$last_backup" ]; then |
||||
|
last_backup="$f" |
||||
|
fi |
||||
|
done |
||||
|
|
||||
|
if [ -n "$last_backup" ]; then |
||||
|
last_backup=$(dirname "$last_backup") |
||||
|
echo "Restoring from last backup: $last_backup..." |
||||
|
mkdir -p "$PGDATA" |
||||
|
tar -xvf "$last_backup/base.tar" -C "$PGDATA" |
||||
|
if [ -f "$last_backup/pg_wal.tar" ]; then |
||||
|
mkdir -p "$PGDATA/pg_wal" |
||||
|
tar -xvf "$last_backup/pg_wal.tar" -C "$PGDATA/pg_wal" |
||||
|
fi |
||||
|
echo "Verifying backup integrity..." |
||||
|
pg_verifybackup -m "$last_backup/backup_manifest" "$PGDATA" |
||||
|
echo "Setting ownership and permissions..." |
||||
|
chown -R postgres:postgres "$PGDATA" |
||||
|
chmod 700 "$PGDATA" |
||||
|
echo "Restoration complete." |
||||
|
exit 0 |
||||
|
fi |
||||
|
|
||||
|
echo "No previous backup found, initializing an empty database!" |
||||
|
exec /usr/local/bin/docker-ensure-initdb.sh |
||||
@ -1,17 +0,0 @@ |
|||||
[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