A minimal Alpine-based container to back up directories and databases on a schedule, with notification support via Nextcloud Talk Bot and e-mail.
  • Shell 93.4%
  • Dockerfile 6.6%
Find a file
Michael Kleger 2ecec4d6b3 Version 1.0.5
- Switch zwischen MariaDB und MySQL für besser Kompatibilität
- OS Updates
2025-09-26 12:02:43 +02:00
scripts Version 1.0.5 2025-09-26 12:02:43 +02:00
docker-compose.yml Version 1.0.2 2025-05-01 14:45:34 +02:00
Dockerfile Version 1.0.5 2025-09-26 12:02:43 +02:00
README.md Version 1.0.5 2025-09-26 12:02:43 +02:00

Docker Backup Container

A minimal Alpine-based container to back up directories and databases on a schedule, with notification support via Nextcloud Talk Bot and e-mail.


🚀 Features

  • File backups (tar.gz)
  • PostgreSQL, MariaDB and MySQL backups (.sql.gz)
  • Cron-based scheduling
  • Initial backup on container start
  • Retention-based cleanup (delete old backups)
  • Talk Bot notifications (HMAC-based)
  • Optional e-mail notifications via SMTP
  • Stale lockfile cleanup (after 1 hour)
  • Unified logging for file + console (timestamped)
  • Custom container/service/stack labeling for notifications

📦 Environment Variables

📁 General

Variable Description Required
FILES_TO_BACKUP Space-separated list of folders to back up
BACKUP_DIR Root folder for all backups
LOG_DIR Log file location
RETENTION_DAYS Days to keep old backups
CRON_SCHEDULE Cron format (e.g. 0 * * * *)
STACK_NAME Logical stack name (for identification)
BACKUP_NAME Friendly container/service name

🛢️ Database (optional)

Set DB_TYPE=postgres, mariadb, mysql, or none

PostgreSQL:

Variable Description
POSTGRES_HOST Database host
POSTGRES_PORT Database port (Standard port 5432 is already set)
POSTGRES_USER Username
POSTGRES_PASSWORD Password
POSTGRES_DATABASE DB name
POSTGRES_EXTRA_OPTS Extra options

MariaDB:

Variable Description
MARIADB_HOST Database host
MARIADB_PORT Database port (Standard port 3306 is already set)
MARIADB_USER Username
MARIADBYSQL_PASSWORD Password
MARIADB_DATABASE DB name
MARIADB_EXTRA_OPTS Extra options

MySQL:

Variable Description
MYSQL_HOST Database host
MYSQL_PORT Database port (Standard port 3306 is already set)
MYSQL_USER Username
MYSQL_PASSWORD Password
MYSQL_DATABASE DB name
MYSQL_EXTRA_OPTS Extra options

💬 Nextcloud Talk Bot (HMAC)

Variable Description
NC_TALK_NOTIFY=on Enable Talk Bot notifications
NC_TALK_URL Nextcloud base URL
NC_TALK_BOT_CHANNEL Bot ID or room token (from Talk or occ)
NC_TALK_BOT_SECRET Shared secret (from php occ talk:bot:install)

📧 Email Notifications (optional)

Variable Description
EMAIL_NOTIFY=on Enable e-mail alerts
EMAIL_TO Recipient
EMAIL_FROM Sender
SMTP_SERVER SMTP hostname/IP
SMTP_PORT Port (default: 587)
SMTP_USER SMTP user (if auth needed)
SMTP_PASS SMTP password
SMTP_TLS=on Enable STARTTLS (default: on)

📦 Uses msmtp (included) for sending mail


🧪 Example docker-compose.yml

version: '3'

services:
  backup:
    build: .
    environment:
      FILES_TO_BACKUP: "/data"
      BACKUP_DIR: "/backup"
      LOG_DIR: "/log"
      RETENTION_DAYS: 30
      CRON_SCHEDULE: "0 * * * *"

      BACKUP_NAME: "web-backup-db"

      DB_TYPE: "postgres"
      POSTGRES_HOST: "postgres"
      POSTGRES_USER: "backupuser"
      POSTGRES_PASSWORD: "secret"
      POSTGRES_DATABASE: "myapp"

      NC_TALK_NOTIFY: "off"
      NC_TALK_URL: "https://cloud.example.com"
      NC_TALK_BOT_CHANNEL: "abc123"
      NC_TALK_BOT_SECRET: "your_shared_hmac_secret"

      EMAIL_NOTIFY: "off"
      EMAIL_TO: "alerts@example.com"
      EMAIL_FROM: "backup@host.local"
      SMTP_SERVER: "smtp.example.com"
      SMTP_PORT: 587
      SMTP_USER: "smtp-user"
      SMTP_PASS: "smtp-pass"
      SMTP_TLS: "on"

    volumes:
      - ./data:/data
      - ./backup:/backup
      - ./log:/log

📂 Folder Structure

/backup/
  2025-04/
    files_data_2025-04-30_1400.tar.gz
    postgres_myapp_2025-04-30_1400.sql.gz
/log/
  backup_2025-04-30.log

🧹 Cleanup

Backups older than RETENTION_DAYS are removed automatically. Log entries show deleted files.


🩺 Healthcheck (optional)

Add this to your Compose file to track success:

healthcheck:
  test: ["CMD", "/scripts/healthcheck.sh"]
  interval: 5m
  timeout: 10s
  retries: 3
  start_period: 30s

The backup script writes this file only on success


🧑‍💻 Author


📝 License

MIT License free for commercial and private use.


Build

Multi Platform Builder

docker buildx create --use --platform=linux/arm64,linux/amd64 --name multi-platform-builder
docker buildx inspect --bootstrap

Build

docker buildx build --platform linux/amd64,linux/arm64 --push --tag onesystems/dockerbackup:v1.0.5 --file Dockerfile .
docker buildx build --platform linux/amd64,linux/arm64 --push --tag onesystems/dockerbackup:v1.0.5 --tag onesystems/dockerbackup:latest --file Dockerfile .