commit 4825fdb38fb09e8e70275ec5b2d6e10672a5403f Author: EmilCh Date: Wed Aug 19 13:18:09 2026 +0200 mer. 19 août 2026 13:18:09 CEST diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6c0827b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM alpine:latest +RUN apk add --no-cache postgresql16-client bash +COPY root / +RUN chmod +x /*.sh +EXPOSE 80 +WORKDIR /backup +VOLUME [ "/backup" ] +CMD ["/run.sh"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..b004184 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +Example docker-compose.yml file +``` +services: + backup: + image: ghcr.io/emilch/postgresql-container-backup:main + restart: always + volumes: + - ./backup:/backup + environment: + PGSQL_USER: postgres + PGSQL_PASS: postgrespassword + PGSQL_HOST: postgresdb +``` diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..c41af58 --- /dev/null +++ b/build.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +export TAG=16-$(date '+%s') +docker compose build --push +echo "The tag to use is: 16-$TAG" diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..4204d33 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,4 @@ +services: + ttrss: + build: ./ + image: registry.cheriches.eu/pgsql-backup:16-${TAG} diff --git a/root/backup.sh b/root/backup.sh new file mode 100644 index 0000000..4167db0 --- /dev/null +++ b/root/backup.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +# Postgres configuration (replace with your details) +PGUSER="your_username" +PGHOST="your_hostname" +PGPORT="5432" +export PGPASSWORD="your_password" +BACKUPDIR="/backup" +EXCLUDE_DBS="template0 template1 postgres" # Exclude these databases + +# Get date in YYYY-MM-DD format +CURRENTDATE=$(date +%Y-%m-%d) + +# Function to backup a single database +backup_database() { + local db_name="$1" + local backup_file="$BACKUPDIR/$CURRENTDATE-$db_name.sql.gz" + + # Skip excluded databases + if [[ "$EXCLUDE_DBS" =~ " $db_name " ]]; then + echo "Skipping excluded database: $db_name" + return + fi + + # Dump the database + pg_dump -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" "$db_name" | gzip > "$backup_file" + echo "Backup completed: $backup_file" +} + +# Ensure backup directory exists +mkdir -p "$BACKUPDIR" + +# Get list of databases (excluding template and postgres) +databases=$(psql -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;") + +# Loop through databases and backup each one +for db_name in $databases; do + backup_database "$db_name" +done + +# Delete backups older than 60 days +find "$BACKUPDIR" -type f -name "*.sql.gz" -mtime +30 -delete + +echo "Deleted old backups from $BACKUPDIR" diff --git a/root/run.sh b/root/run.sh new file mode 100644 index 0000000..69ec4e5 --- /dev/null +++ b/root/run.sh @@ -0,0 +1,11 @@ +#!/bin/sh +sed -i -e "s#your_username#$PGSQL_USER#" \ + -e "s#your_password#$PGSQL_PASS#" \ + -e "s#your_hostname#$PGSQL_HOST#" \ + /backup.sh + +while true; + do + /backup.sh + sleep 172800; + done