mirror of
https://github.com/fradelg/docker-mysql-cron-backup.git
synced 2025-01-18 16:22:33 +01:00
support saving non-gzipped sql files
This commit is contained in:
parent
f0e5cb6ac8
commit
8992f162d7
3 changed files with 27 additions and 10 deletions
|
@ -28,6 +28,7 @@ docker container run -d \
|
||||||
- `INIT_RESTORE_LATEST`: If set, restores latest backup.
|
- `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.
|
- `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.
|
- `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.
|
- `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:
|
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:
|
||||||
|
|
25
backup.sh
25
backup.sh
|
@ -20,19 +20,28 @@ do
|
||||||
then
|
then
|
||||||
echo "==> Dumping database: $db"
|
echo "==> Dumping database: $db"
|
||||||
FILENAME=/backup/$DATE.$db.sql
|
FILENAME=/backup/$DATE.$db.sql
|
||||||
LATEST=/backup/latest.$db.sql.gz
|
LATEST=/backup/latest.$db.sql
|
||||||
if mysqldump --single-transaction -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$db" $MYSQLDUMP_OPTS > "$FILENAME"
|
if mysqldump --single-transaction -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$db" "$MYSQLDUMP_OPTS" > "$FILENAME"
|
||||||
then
|
then
|
||||||
gzip "-$GZIP_LEVEL" -f "$FILENAME"
|
EXT=
|
||||||
echo "==> Creating symlink to latest backup: $(basename "$FILENAME".gz)"
|
if [ -z "${USE_PLAIN_SQL}" ]
|
||||||
|
then
|
||||||
|
echo "==> Compressing $db with LEVEL $GZIP_LEVEL"
|
||||||
|
gzip "-$GZIP_LEVEL" -f "$FILENAME"
|
||||||
|
EXT=.gz
|
||||||
|
FILENAME=$FILENAME$EXT
|
||||||
|
LATEST=$LATEST$EXT
|
||||||
|
fi
|
||||||
|
BASENAME=$(basename "$FILENAME")
|
||||||
|
echo "==> Creating symlink to latest backup: $BASENAME"
|
||||||
rm "$LATEST" 2> /dev/null
|
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" ]
|
if [ -n "$MAX_BACKUPS" ]
|
||||||
then
|
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
|
do
|
||||||
TARGET=$(find /backup -maxdepth 1 -name "*.$db.sql.gz" -type f | sort | head -n 1)
|
TARGET=$(find /backup -maxdepth 1 -name "*.$db.sql$EXT" -type f | sort | head -n 1)
|
||||||
echo "==> Max number of backups ($MAX_BACKUPS) reached. Deleting ${TARGET} ..."
|
echo "==> Max number of ($MAX_BACKUPS) backups reached. Deleting ${TARGET} ..."
|
||||||
rm -rf "${TARGET}"
|
rm -rf "${TARGET}"
|
||||||
echo "==> Backup ${TARGET} deleted"
|
echo "==> Backup ${TARGET} deleted"
|
||||||
done
|
done
|
||||||
|
|
11
restore.sh
11
restore.sh
|
@ -13,13 +13,20 @@ fi
|
||||||
|
|
||||||
set -o pipefail
|
set -o pipefail
|
||||||
|
|
||||||
SQL=$(gunzip -c "$1")
|
if [ -z "${USE_PLAIN_SQL}" ]
|
||||||
|
then
|
||||||
|
SQL=$(gunzip -c "$1")
|
||||||
|
else
|
||||||
|
SQL=$(cat "$1")
|
||||||
|
fi
|
||||||
|
|
||||||
DB_NAME=${MYSQL_DATABASE:-${MYSQL_DB}}
|
DB_NAME=${MYSQL_DATABASE:-${MYSQL_DB}}
|
||||||
if [ -z "${DB_NAME}" ]
|
if [ -z "${DB_NAME}" ]
|
||||||
then
|
then
|
||||||
|
echo "=> Searching database name in $1"
|
||||||
DB_NAME=$(echo "$SQL" | grep -oE '(Database: (.+))' | cut -d ' ' -f 2)
|
DB_NAME=$(echo "$SQL" | grep -oE '(Database: (.+))' | cut -d ' ' -f 2)
|
||||||
fi
|
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"
|
echo "=> Restore database $DB_NAME from $1"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue