diff --git a/README.md b/README.md index 3c135b7..25a91fb 100644 --- a/README.md +++ b/README.md @@ -68,10 +68,11 @@ This container automatically detects the database type (MariaDB or PostgreSQL) i ### Backup Methods -The container supports two backup methods: +The container supports three execution modes: 1. **`restic`** - Backup to Restic repositories (cloud storage, remote servers) 2. **`local`** - Create compressed local backup files +3. **`loop`** - Keep container running for external schedulers (e.g., Ofelia, Kubernetes CronJob) ### Restic Backups @@ -97,6 +98,18 @@ docker run --rm \ gitea.ceperka.net/rosti/db-backup:latest local ``` +### Loop Mode (For External Schedulers) + +```bash +docker run -d \ + -v /var/run/docker.sock:/var/run/docker.sock \ + -e CONTAINER=my-mariadb-container \ + -e MARIADB_ROOT_PASSWORD=db-password \ + gitea.ceperka.net/rosti/db-backup:latest loop +``` + +In loop mode, the container stays running indefinitely, allowing external schedulers like Ofelia, Kubernetes CronJobs, or other orchestrators to execute the backup scripts directly inside the running container. + ### With Docker Compose #### Restic Backup Setup @@ -163,6 +176,52 @@ volumes: db_data: ``` +#### Loop Mode with External Scheduler (Ofelia) + +```yaml +version: '3.8' + +services: + database: + image: mariadb:latest + environment: + MARIADB_ROOT_PASSWORD: secretpassword + MARIADB_DATABASE: myapp + volumes: + - db_data:/var/lib/mysql + + backup: + image: gitea.ceperka.net/rosti/db-backup:latest + depends_on: + - database + environment: + CONTAINER: database + TARGET_DIR: /backups + MARIADB_ROOT_PASSWORD: secretpassword + NOTIFY_URL: https://hc-ping.com/your-healthcheck-uuid + volumes: + - /var/run/docker.sock:/var/run/docker.sock + - ./backups:/backups + command: ["loop"] + labels: + ofelia.enabled: "true" + ofelia.job-exec.backup-local.schedule: "0 2 * * *" + ofelia.job-exec.backup-local.command: "/backup_local.sh" + ofelia.job-exec.backup-restic.schedule: "0 3 * * *" + ofelia.job-exec.backup-restic.command: "/backup_restic.sh" + + scheduler: + image: mcuadros/ofelia:latest + depends_on: + - backup + command: daemon --docker + volumes: + - /var/run/docker.sock:/var/run/docker.sock + +volumes: + db_data: +``` + ### Scheduled Backups with Cron To run backups on a schedule, you can use cron or a container orchestrator: @@ -179,6 +238,28 @@ To run backups on a schedule, you can use cron or a container orchestrator: 0 3 * * * docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /host/backups:/backups -e CONTAINER=my-db -e TARGET_DIR=/backups -e MARIADB_ROOT_PASSWORD=dbpass gitea.ceperka.net/rosti/db-backup:latest local ``` +### External Schedulers with Loop Mode + +When using loop mode, you can execute backups from external schedulers by running the backup scripts directly inside the running container: + +#### With Ofelia Scheduler +Ofelia can execute jobs in running containers using labels (see Docker Compose example above). + +#### Manual Execution in Loop Mode +```bash +# Execute local backup in running container +docker exec /backup_local.sh + +# Execute restic backup in running container +docker exec /backup_restic.sh +``` + +#### With Kubernetes CronJob + Running Pod +```bash +# Execute backup in running pod +kubectl exec -- /backup_local.sh +``` + ### Kubernetes CronJob #### Restic Backup CronJob diff --git a/backup.sh b/backup.sh index 5eacf7b..d1847e5 100644 --- a/backup.sh +++ b/backup.sh @@ -4,6 +4,11 @@ if [ "$1" = "local" ]; then source /backup_local.sh elif [ "$1" = "restic" ]; then source /backup_restic.sh +elif [ "$1" = "loop" ]; then + # Inifinite loop to keep the container running and let Ofelia scheduler (or any other) manage the execution + while true; do + sleep 86400 + done else echo "Unknown backup method. Use 'local' or 'restic'." fi