From 2231e88e589451f5f23c40eba3730e1842b335ce Mon Sep 17 00:00:00 2001 From: Alan Colon Date: Thu, 26 Apr 2018 10:56:25 -0700 Subject: [PATCH] Implement MYSQL_DB and MYSQLDUMP_OPTS options. Accept MYSQL_DATABASE and MYSQL_PASSWORD to maintain parity with _/mysql image options. --- README.md | 1 + backup.sh | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 29086da..1a649b5 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ This docker image runs mysqldump to backup your databases periodically using cro MYSQL_USER the username of your mysql database MYSQL_PASS the password of your mysql database MYSQL_DB the database name to dump. Default: `--all-databases` + MYSQLDUMP_OPTS command line arguments to pass to mysqldump. Example: `--single-transaction` CRON_TIME the interval of cron job to run mysqldump. `0 0 * * *` by default, which is every day at 00:00 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 diff --git a/backup.sh b/backup.sh index 8de41a4..4278fcd 100755 --- a/backup.sh +++ b/backup.sh @@ -1,17 +1,17 @@ #!/bin/bash [ -z "${MYSQL_USER}" ] && { echo "=> MYSQL_USER cannot be empty" && exit 1; } -[ -z "${MYSQL_PASS}" ] && { echo "=> MYSQL_PASS cannot be empty" && exit 1; } +[ -z "${MYSQL_PASS:=$MYSQL_PASSWORD}" ] && { echo "=> MYSQL_PASS cannot be empty" && exit 1; } DATE=$(date +%Y%m%d%H%M) echo "=> Backup started at $DATE" -databases=$(mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" -e "SHOW DATABASES;" | tr -d "| " | grep -v Database) +databases=${MYSQL_DATABASE:-${MYSQL_DB:-$(mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)}} for db in $databases do if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] then echo "Dumping database: $db" FILENAME=/backup/$DATE.$db.sql - if mysqldump -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" --databases "$db" > "$FILENAME" + if mysqldump -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" --databases "$db" $MYSQLDUMP_OPTS > "$FILENAME" then gzip -f "$FILENAME" else