diff --git a/.github/workflows/image.yml b/.github/workflows/image.yml index 471d2d5..5827146 100644 --- a/.github/workflows/image.yml +++ b/.github/workflows/image.yml @@ -24,7 +24,7 @@ jobs: run: | docker compose up -d mariadb docker compose run backup /backup.sh - docker compose run backup /restore.sh /backup/latest.foo.sql.bz2 + docker compose run backup /restore.sh /backup/latest.foo.sql.gz docker compose stop build: runs-on: ubuntu-22.04 diff --git a/Dockerfile b/Dockerfile index fb17ac7..e6887c2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,7 @@ RUN apk add --update \ tzdata \ bash \ mysql-client \ - bzip2 \ + gzip \ openssl \ mariadb-connector-c \ fdupes && \ diff --git a/README.md b/README.md index 4a7d1f6..16a2d65 100644 --- a/README.md +++ b/README.md @@ -39,8 +39,8 @@ Container is **Healthy** after the database init phase, that is after `INIT_BACK - `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. -- `BZIP2_LEVEL`: Specify the level of bzip2 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 bzip2. +- `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. - `TZ`: Specify TIMEZONE in Container. E.g. "Europe/Berlin". Default is UTC. - `REMOVE_DUPLICATES`: Use [fdupes](https://github.com/adrianlopezroche/fdupes) to remove duplicate database dumps @@ -59,7 +59,7 @@ services: volumes: - data:/var/lib/mysql # If there is not scheme, restore the last created backup (if exists) - - ${VOLUME_PATH}/backup/latest.${DATABASE_NAME}.sql.bz2:/docker-entrypoint-initdb.d/database.sql.bz2 + - ${VOLUME_PATH}/backup/latest.${DATABASE_NAME}.sql.gz:/docker-entrypoint-initdb.d/database.sql.gz environment: - MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD} - MYSQL_DATABASE=${DATABASE_NAME} @@ -80,7 +80,7 @@ services: # Every day at 03:00 - CRON_TIME=0 3 * * * # Make it small - - BZIP2_LEVEL=9 + - GZIP_LEVEL=9 # As of MySQL 8.0.21 this is needed - MYSQLDUMP_OPTS=--no-tablespaces restart: unless-stopped @@ -177,7 +177,7 @@ To restore a database from a certain backup you may have to specify the database ```YAML mysql-cron-backup: image: fradelg/mysql-cron-backup - command: "/restore.sh /backup/201708060500.${DATABASE_NAME}.sql.bz2" + command: "/restore.sh /backup/201708060500.${DATABASE_NAME}.sql.gz" depends_on: - mariadb volumes: @@ -218,7 +218,7 @@ Set `EXIT_BACKUP` to automatic create a last backup on shutdown. # Every day at 03:00 - CRON_TIME=0 3 * * * # Make it small - - BZIP2_LEVEL=9 + - GZIP_LEVEL=9 restart: unless-stopped volumes: @@ -235,7 +235,7 @@ Docker database image could expose a directory you could add files as init sql s 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.bz2 + - ./init-script.sql:/docker-entrypoint-initdb.d/database.sql.gz environment: - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} - MYSQL_DATABASE=${DATABASE_NAME} @@ -250,7 +250,7 @@ Docker database image could expose a directory you could add files as init sql s 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.bz2 + - ./init-script.sql:/docker-entrypoint-initdb.d/database.sql.gz environment: - MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD} - MYSQL_DATABASE=${DATABASE_NAME} diff --git a/backup.sh b/backup.sh index 236f879..d7db8a7 100755 --- a/backup.sh +++ b/backup.sh @@ -13,7 +13,7 @@ # Note: when from file, there can be one database name per line in that file [ -z "${MYSQL_DATABASE_FILE}" ] || { MYSQL_DATABASE=$(cat "${MYSQL_DATABASE_FILE}"); } # Get level from env, else use 6 -[ -z "${BZIP2_LEVEL}" ] && { BZIP2_LEVEL=6; } +[ -z "${GZIP_LEVEL}" ] && { GZIP_LEVEL=6; } DATE=$(date +%Y%m%d%H%M) echo "=> Backup started at $(date "+%Y-%m-%d %H:%M:%S")" @@ -34,9 +34,9 @@ do EXT= if [ -z "${USE_PLAIN_SQL}" ] then - echo "==> Compressing $db with LEVEL $BZIP2_LEVEL" - bzip2 "-$BZIP2_LEVEL" -f "$FILENAME" - EXT=.bz2 + echo "==> Compressing $db with LEVEL $GZIP_LEVEL" + gzip "-$GZIP_LEVEL" -n -f "$FILENAME" + EXT=.gz FILENAME=$FILENAME$EXT LATEST=$LATEST$EXT fi diff --git a/run.sh b/run.sh index 291cd8f..17a420c 100755 --- a/run.sh +++ b/run.sh @@ -11,9 +11,9 @@ elif [ -n "${INIT_RESTORE_LATEST}" ]; then echo "waiting database container..." sleep 1 done - # Needed to exclude the 'latest.<database>.sql.bz2' file, consider only filenames starting with number - # Only data-tagged backups, eg. '202212250457.database.sql.bz2', must be trapped by the regex - find /backup -maxdepth 1 -name '[0-9]*.*.sql.bz2' | sort | tail -1 | xargs /restore.sh + # Needed to exclude the 'latest.<database>.sql.gz' file, consider only filenames starting with number + # Only data-tagged backups, eg. '202212250457.database.sql.gz', must be trapped by the regex + find /backup -maxdepth 1 -name '[0-9]*.*.sql.gz' | sort | tail -1 | xargs /restore.sh fi function final_backup {