From 823343381d42e055998106cb492ae18908cc8dc0 Mon Sep 17 00:00:00 2001 From: Tobias Janke Date: Sun, 8 Dec 2024 11:12:39 +0100 Subject: [PATCH] [Feature] Remove duplicate database dumps to save storage space (#127) * Replace gzip with deterministic bzip2 compression * Exclude comments from SQL dump * Add option to remove duplicates using fdupes * Revert change to bzip2 as gzip can be deterministic using no-name parameter --- Dockerfile | 3 ++- README.md | 1 + backup.sh | 9 +++++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 51fbf16..e6887c2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,7 +21,8 @@ RUN apk add --update \ mysql-client \ gzip \ openssl \ - mariadb-connector-c && \ + mariadb-connector-c \ + fdupes && \ rm -rf /var/cache/apk/* COPY --from=binary /go/bin/dockerize /usr/local/bin diff --git a/README.md b/README.md index dad5be3..16a2d65 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ Container is **Healthy** after the database init phase, that is after `INIT_BACK - `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 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: diff --git a/backup.sh b/backup.sh index 747cd82..d7db8a7 100755 --- a/backup.sh +++ b/backup.sh @@ -29,13 +29,13 @@ do echo "==> Dumping database: $db" FILENAME=/backup/$DATE.$db.sql LATEST=/backup/latest.$db.sql - if mysqldump --single-transaction $MYSQLDUMP_OPTS -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" $MYSQL_SSL_OPTS "$db" > "$FILENAME" + if mysqldump --single-transaction --skip-comments $MYSQLDUMP_OPTS -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" $MYSQL_SSL_OPTS "$db" > "$FILENAME" then EXT= if [ -z "${USE_PLAIN_SQL}" ] then echo "==> Compressing $db with LEVEL $GZIP_LEVEL" - gzip "-$GZIP_LEVEL" -f "$FILENAME" + gzip "-$GZIP_LEVEL" -n -f "$FILENAME" EXT=.gz FILENAME=$FILENAME$EXT LATEST=$LATEST$EXT @@ -44,6 +44,11 @@ do echo "==> Creating symlink to latest backup: $BASENAME" rm "$LATEST" 2> /dev/null cd /backup || exit && ln -s "$BASENAME" "$(basename "$LATEST")" + if [ -n "$REMOVE_DUPLICATES" ] + then + echo "=> Removing duplicate database dumps" + fdupes -idN /backup/ + fi if [ -n "$MAX_BACKUPS" ] then # Execute the delete script, delete older backup or other custom delete script