diff --git a/.github/workflows/image.yml b/.github/workflows/image.yml index 5827146..471d2d5 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.gz + docker compose run backup /restore.sh /backup/latest.foo.sql.bz2 docker compose stop build: runs-on: ubuntu-22.04 diff --git a/Dockerfile b/Dockerfile index 51fbf16..29cae87 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,7 @@ RUN apk add --update \ tzdata \ bash \ mysql-client \ - gzip \ + bzip2 \ openssl \ mariadb-connector-c && \ rm -rf /var/cache/apk/* diff --git a/README.md b/README.md index dad5be3..6c39e9f 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. -- `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. +- `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. - `TZ`: Specify TIMEZONE in Container. E.g. "Europe/Berlin". Default is UTC. If you want to make this image the perfect companion of your MySQL container, use [docker-compose](https://docs.docker.com/compose/). You can add more services that will be able to connect to the MySQL image using the name `my_mariadb`, note that you only expose the port `3306` internally to the servers and not to the host: @@ -58,7 +58,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.gz:/docker-entrypoint-initdb.d/database.sql.gz + - ${VOLUME_PATH}/backup/latest.${DATABASE_NAME}.sql.bz2:/docker-entrypoint-initdb.d/database.sql.bz2 environment: - MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD} - MYSQL_DATABASE=${DATABASE_NAME} @@ -79,7 +79,7 @@ services: # Every day at 03:00 - CRON_TIME=0 3 * * * # Make it small - - GZIP_LEVEL=9 + - BZIP2_LEVEL=9 # As of MySQL 8.0.21 this is needed - MYSQLDUMP_OPTS=--no-tablespaces restart: unless-stopped @@ -176,7 +176,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.gz" + command: "/restore.sh /backup/201708060500.${DATABASE_NAME}.sql.bz2" depends_on: - mariadb volumes: @@ -217,7 +217,7 @@ Set `EXIT_BACKUP` to automatic create a last backup on shutdown. # Every day at 03:00 - CRON_TIME=0 3 * * * # Make it small - - GZIP_LEVEL=9 + - BZIP2_LEVEL=9 restart: unless-stopped volumes: @@ -234,7 +234,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.gz + - ./init-script.sql:/docker-entrypoint-initdb.d/database.sql.bz2 environment: - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} - MYSQL_DATABASE=${DATABASE_NAME} @@ -249,7 +249,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.gz + - ./init-script.sql:/docker-entrypoint-initdb.d/database.sql.bz2 environment: - MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD} - MYSQL_DATABASE=${DATABASE_NAME} diff --git a/backup.sh b/backup.sh index 747cd82..fca0571 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 "${GZIP_LEVEL}" ] && { GZIP_LEVEL=6; } +[ -z "${BZIP2_LEVEL}" ] && { BZIP2_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 $GZIP_LEVEL" - gzip "-$GZIP_LEVEL" -f "$FILENAME" - EXT=.gz + echo "==> Compressing $db with LEVEL $BZIP2_LEVEL" + bzip2 "-$BZIP2_LEVEL" -f "$FILENAME" + EXT=.bz2 FILENAME=$FILENAME$EXT LATEST=$LATEST$EXT fi diff --git a/run.sh b/run.sh index 17a420c..291cd8f 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.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 + # 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 fi function final_backup {