From e8c41d1f64624e3de70fa40e78dd6db69381e68f Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 12 Feb 2026 17:38:03 +0100 Subject: [PATCH] =?UTF-8?q?alles=20geht=20aber=20noch=20aufr=C3=A4umen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 15 +++++++++++++-- backup-container.sh | 29 +++++++++++++++++++++++++++++ backup.Dockerfile | 2 ++ compose.yml | 34 +++++++++++++++++++++++++++++----- example.env | 4 +++- redis/.gitignore | 2 ++ 6 files changed, 78 insertions(+), 8 deletions(-) create mode 100755 backup-container.sh create mode 100644 backup.Dockerfile create mode 100644 redis/.gitignore diff --git a/README.md b/README.md index 8d583b8..c8f9ed7 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,17 @@ After first `docker compose up`, install the **Redis Object Cache** plugin in WP The `wp-config.php` already has the `WP_REDIS_HOST` and `WP_REDIS_PORT` constants set. -## Cron +## Backups -Backup script in Cron einbinden nicht vergessen \ No newline at end of file +Backups laufen automatisch im `backup` Container (Standard: täglich um 2:00 Uhr). +Konfigurierbar über `.env`: + +- `BACKUP_SCHEDULE` – Cron-Ausdruck (Standard: `0 2 * * *`) +- `BACKUP_RETENTION_DAYS` – Alte Backups löschen nach X Tagen (Standard: `7`) + +Backups landen in `./backups/`. Manuelles Backup auslösen: +``` +docker compose exec backup /usr/local/bin/backup.sh +``` + +Das alte `backup.sh` auf dem Host funktioniert weiterhin für manuelle Backups. \ No newline at end of file diff --git a/backup-container.sh b/backup-container.sh new file mode 100755 index 0000000..c6f8e77 --- /dev/null +++ b/backup-container.sh @@ -0,0 +1,29 @@ +#!/bin/sh +set -e + +DATE=$(date +"%Y-%m-%d_%H-%M") +BACKUP_DIR="/backups" +BACKUP_NAME="${DATE}.tar.gz" + +mkdir -p "$BACKUP_DIR" + +echo "[$(date)] Starting backup..." + +echo "Dumping database..." +mariadb-dump --no-tablespaces \ + -h db \ + -u"$MYSQL_USER" \ + -p"$MYSQL_PASSWORD" \ + "$MYSQL_DATABASE" > "/tmp/db_dump_${DATE}.sql" + +echo "Creating tar.gz archive..." +tar -czf "${BACKUP_DIR}/${BACKUP_NAME}" \ + -C /source/wp . \ + -C /tmp "db_dump_${DATE}.sql" + +rm -f "/tmp/db_dump_${DATE}.sql" + +echo "Cleaning up backups older than ${BACKUP_RETENTION_DAYS:-7} days..." +find "$BACKUP_DIR" -name "*.tar.gz" -type f -mtime +${BACKUP_RETENTION_DAYS:-7} -delete + +echo "[$(date)] Backup complete: ${BACKUP_DIR}/${BACKUP_NAME}" diff --git a/backup.Dockerfile b/backup.Dockerfile new file mode 100644 index 0000000..47da6f4 --- /dev/null +++ b/backup.Dockerfile @@ -0,0 +1,2 @@ +FROM alpine:latest +RUN apk add --no-cache mariadb-client tar gzip diff --git a/compose.yml b/compose.yml index bdc35e2..3575d4d 100644 --- a/compose.yml +++ b/compose.yml @@ -25,7 +25,7 @@ services: restart: unless-stopped command: redis-server --maxmemory 128mb --maxmemory-policy allkeys-lru volumes: - - redis_data:/data + - ./redis:/data networks: - backend healthcheck: @@ -76,10 +76,34 @@ services: networks: - frontend + backup: + build: + context: . + dockerfile: backup.Dockerfile + restart: unless-stopped + depends_on: + db: + condition: service_healthy + entrypoint: /bin/sh + command: + - -c + - | + echo "${BACKUP_SCHEDULE:-0 2 * * *} /usr/local/bin/backup.sh" | crontab - + echo "Backup container ready. Schedule: ${BACKUP_SCHEDULE:-0 2 * * *}" + crond -f -l 2 + volumes: + - ./backup-container.sh:/usr/local/bin/backup.sh:ro + - ./wp:/source/wp:ro + - ./backups:/backups + environment: + - MYSQL_USER=${MYSQL_USER} + - MYSQL_PASSWORD=${MYSQL_PASSWORD} + - MYSQL_DATABASE=${MYSQL_DATABASE} + - BACKUP_RETENTION_DAYS=${BACKUP_RETENTION_DAYS:-7} + networks: + - backend + networks: frontend: backend: - internal: true - -volumes: - redis_data: \ No newline at end of file + internal: true \ No newline at end of file diff --git a/example.env b/example.env index 926ba54..8b22535 100644 --- a/example.env +++ b/example.env @@ -3,4 +3,6 @@ MYSQL_DATABASE=wordpress MYSQL_USER=wordpress MYSQL_PASSWORD=$( openssl rand -hex 16 ) WP_PORT=8080 -STATIC_PORT=8081 \ No newline at end of file +STATIC_PORT=8081 +BACKUP_SCHEDULE=0 2 * * * +BACKUP_RETENTION_DAYS=7 \ No newline at end of file diff --git a/redis/.gitignore b/redis/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/redis/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file