Merge pull request #93 from g-nardiello/feature-exit-backup

[Feature] Make backup on container shutdown
This commit is contained in:
Fco. Javier Delgado del Hoyo 2023-01-19 19:32:42 +01:00 committed by GitHub
commit 90ad75a24e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 2 deletions

View file

@ -31,6 +31,7 @@ docker container run -d \
- `MAX_BACKUPS`: The number of backups to keep. When reaching the limit, the old backup will be discarded. No limit by default.
- `INIT_BACKUP`: If set, create a backup when the container starts.
- `INIT_RESTORE_LATEST`: If set, restores latest backup.
- `EXIT_BACKUP`: If set, create a backup when the container stops.
- `TIMEOUT`: Wait a given number of seconds for the database to be ready and make the first backup, `10s` by default. After that time, the initial attempt for backup gives up and only the Cron job will try to make a backup.
- `GZIP_LEVEL`: Specify the level of gzip compression from 1 (quickest, least compressed) to 9 (slowest, most compressed), default is 6.
- `USE_PLAIN_SQL`: If set, back up and restore plain SQL files without gzip.
@ -184,4 +185,65 @@ mysql-cron-backup:
docker container exec <your_mysql_backup_container_name> /restore.sh /backup/<your_sql_backup_gz_file>
```
if no database name is specified, `restore.sh` will try to find the database name from the backup file.
if no database name is specified, `restore.sh` will try to find the database name from the backup file.
### Automatic backup and restore on container starts and stops
Set `INIT_RESTORE_LATEST` to automatic restore the last backup on startup.
Set `EXIT_BACKUP` to automatic create a last backup on shutdown.
```yaml
mysql-cron-backup:
image: fradelg/mysql-cron-backup
depends_on:
- mariadb
volumes:
- ${VOLUME_PATH}/backup:/backup
environment:
- MYSQL_HOST=my_mariadb
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASS=${MYSQL_PASSWORD}
- MAX_BACKUPS=15
- INIT_RESTORE_LATEST=1
- EXIT_BACKUP=1
# Every day at 03:00
- CRON_TIME=0 3 * * *
# Make it small
- GZIP_LEVEL=9
restart: unless-stopped
volumes:
data:
```
Docker database image could expose a directory you could add files as init sql script.
```yaml
mysql:
image: mysql
expose:
- 3306
volumes:
- data:/var/lib/mysql
# If there is not scheme, restore using the init script (if exists)
- ./init-script.sql:/docker-entrypoint-initdb.d/database.sql.gz
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_DATABASE=${DATABASE_NAME}
restart: unless-stopped
```
```yaml
mariadb:
image: mariadb
expose:
- 3306
volumes:
- data:/var/lib/mysql
# If there is not scheme, restore using the init script (if exists)
- ./init-script.sql:/docker-entrypoint-initdb.d/database.sql.gz
environment:
- MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD}
- MYSQL_DATABASE=${DATABASE_NAME}
restart: unless-stopped
```

21
run.sh
View file

@ -16,7 +16,26 @@ elif [ -n "${INIT_RESTORE_LATEST}" ]; then
find /backup -maxdepth 1 -name '[0-9]*.*.sql.gz' | sort | tail -1 | xargs /restore.sh
fi
function final_backup {
echo "=> Captured trap for final backup"
DATE=$(date +%Y%m%d%H%M)
echo "=> Requested last backup at $(date "+%Y-%m-%d %H:%M:%S")"
exec /backup.sh
exit 0
}
if [ -n "${EXIT_BACKUP}" ]; then
echo "=> Listening on container shutdown gracefully to make last backup before close"
trap final_backup SIGHUP SIGINT SIGTERM
fi
echo "${CRON_TIME} /backup.sh >> /mysql_backup.log 2>&1" > /tmp/crontab.conf
crontab /tmp/crontab.conf
echo "=> Running cron task manager in foreground"
exec crond -f -l 8 -L /mysql_backup.log
crond -f -l 8 -L /mysql_backup.log &
echo "Listening on crond, and wait..."
tail -f /dev/null & wait $!
echo "Script is shutted down."