Merge pull request #71 from fradelg/save-plain-sql

support saving non-gzipped sql files
This commit is contained in:
Fco. Javier Delgado del Hoyo 2022-02-05 18:18:33 +01:00 committed by GitHub
commit 42312b94a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 12 deletions

View file

@ -29,7 +29,8 @@ COPY --from=binary /go/bin/dockerize /usr/local/bin
ENV CRON_TIME="0 3 * * sun" \
MYSQL_HOST="mysql" \
MYSQL_PORT="3306" \
TIMEOUT="10s"
TIMEOUT="10s" \
MYSQLDUMP_OPTS="--quick"
COPY ["run.sh", "backup.sh", "restore.sh", "/"]
RUN mkdir /backup && \

View file

@ -21,13 +21,14 @@ docker container run -d \
- `MYSQL_PASS`: The password of your mysql database.
- `MYSQL_PASS_FILE`: The file in container where to find the password of your mysql database (cf. docker secrets). You should use either MYSQL_PASS_FILE or MYSQL_PASS (see examples below).
- `MYSQL_DATABASE`: The database name to dump. Default: `--all-databases`.
- `MYSQLDUMP_OPTS`: Command line arguments to pass to mysqldump. Example: `--single-transaction`.
- `MYSQLDUMP_OPTS`: Command line arguments to pass to mysqldump (see [mysqldump documentation](https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html)).
- `CRON_TIME`: The interval of cron job to run mysqldump. `0 3 * * sun` by default, which is every Sunday at 03:00. It uses UTC timezone.
- `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.
- `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.
- `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:

View file

@ -20,19 +20,28 @@ do
then
echo "==> Dumping database: $db"
FILENAME=/backup/$DATE.$db.sql
LATEST=/backup/latest.$db.sql.gz
if mysqldump --single-transaction -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$db" $MYSQLDUMP_OPTS > "$FILENAME"
LATEST=/backup/latest.$db.sql
if mysqldump --single-transaction "$MYSQLDUMP_OPTS" -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$db" > "$FILENAME"
then
EXT=
if [ -z "${USE_PLAIN_SQL}" ]
then
echo "==> Compressing $db with LEVEL $GZIP_LEVEL"
gzip "-$GZIP_LEVEL" -f "$FILENAME"
echo "==> Creating symlink to latest backup: $(basename "$FILENAME".gz)"
EXT=.gz
FILENAME=$FILENAME$EXT
LATEST=$LATEST$EXT
fi
BASENAME=$(basename "$FILENAME")
echo "==> Creating symlink to latest backup: $BASENAME"
rm "$LATEST" 2> /dev/null
cd /backup || exit && ln -s "$(basename "$FILENAME".gz)" "$(basename "$LATEST")"
cd /backup || exit && ln -s "$BASENAME" "$(basename "$LATEST")"
if [ -n "$MAX_BACKUPS" ]
then
while [ "$(find /backup -maxdepth 1 -name "*.$db.sql.gz" -type f | wc -l)" -gt "$MAX_BACKUPS" ]
while [ "$(find /backup -maxdepth 1 -name "*.$db.sql$EXT" -type f | wc -l)" -gt "$MAX_BACKUPS" ]
do
TARGET=$(find /backup -maxdepth 1 -name "*.$db.sql.gz" -type f | sort | head -n 1)
echo "==> Max number of backups ($MAX_BACKUPS) reached. Deleting ${TARGET} ..."
TARGET=$(find /backup -maxdepth 1 -name "*.$db.sql$EXT" -type f | sort | head -n 1)
echo "==> Max number of ($MAX_BACKUPS) backups reached. Deleting ${TARGET} ..."
rm -rf "${TARGET}"
echo "==> Backup ${TARGET} deleted"
done

View file

@ -13,13 +13,20 @@ fi
set -o pipefail
if [ -z "${USE_PLAIN_SQL}" ]
then
SQL=$(gunzip -c "$1")
else
SQL=$(cat "$1")
fi
DB_NAME=${MYSQL_DATABASE:-${MYSQL_DB}}
if [ -z "${DB_NAME}" ]
then
echo "=> Searching database name in $1"
DB_NAME=$(echo "$SQL" | grep -oE '(Database: (.+))' | cut -d ' ' -f 2)
fi
[ -z "${DB_NAME}" ] && { echo "=> database name cannot be found" && exit 1; }
[ -z "${DB_NAME}" ] && { echo "=> Database name not found" && exit 1; }
echo "=> Restore database $DB_NAME from $1"