From 1302281333472e54e04af5be6ffc495bd0e044d0 Mon Sep 17 00:00:00 2001 From: Stelage Date: Tue, 4 May 2021 22:59:10 +0200 Subject: [PATCH 1/3] Allow restoring without specifying DATABASE_NAME --- README.md | 15 +++++++++++++-- restore.sh | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index cdecc77..5cc37ca 100644 --- a/README.md +++ b/README.md @@ -74,13 +74,17 @@ volumes: ## Restore from a backup +### List all available backups : + See the list of backups in your running docker container, just write in your favorite terminal: ```bash -docker container exec ls /backup +docker container exec ls /backup ``` -To restore a database from a certain backup you have to specify the database name in the variable MYSQL_DATABASE: +### Restore using a compose file + +To restore a database from a certain backup you may have to specify the database name in the variable MYSQL_DATABASE: ```YAML mysql-cron-backup: @@ -96,3 +100,10 @@ mysql-cron-backup: - MYSQL_PASS=${MARIADB_ROOT_PASSWORD} - MYSQL_DATABASE=${DATABASE_NAME} ``` +### Restore using a docker command + +```bash +docker container exec /restore.sh backup/ +``` + +if no database name is specified, `restore.sh` will try to find the database name from the backup file. \ No newline at end of file diff --git a/restore.sh b/restore.sh index e2d8796..1cd478c 100755 --- a/restore.sh +++ b/restore.sh @@ -7,10 +7,20 @@ then echo "You must pass the path of the backup file to restore" fi -echo "=> Restore database from $1" -set -o pipefail +SQL=$(gunzip -c "$1") DB_NAME=${MYSQL_DATABASE:-${MYSQL_DB}} -if gunzip --stdout "$1" | mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$DB_NAME" +if [ -z "${DB_NAME}"] +then + DB_NAME=$(echo $SQL | grep -oE '(Database: (\w*))' | cut -d ' ' -f 2) +fi +[ -z "${DB_NAME}" ] && { echo "=> database name cannot be found" && exit 1; } + + +echo "=> Restore database $DB_NAME from $1" +set -o pipefail + + +if echo $SQL | mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$DB_NAME" then echo "=> Restore succeeded" else From 7c8f51eac31529c74ee24c853dbd8db9b64d8110 Mon Sep 17 00:00:00 2001 From: Stelage Date: Sun, 9 May 2021 17:57:18 +0200 Subject: [PATCH 2/3] Fix pipefail position --- restore.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/restore.sh b/restore.sh index 1cd478c..ab365f2 100755 --- a/restore.sh +++ b/restore.sh @@ -7,6 +7,8 @@ then echo "You must pass the path of the backup file to restore" fi +set -o pipefail + SQL=$(gunzip -c "$1") DB_NAME=${MYSQL_DATABASE:-${MYSQL_DB}} if [ -z "${DB_NAME}"] @@ -15,10 +17,7 @@ then fi [ -z "${DB_NAME}" ] && { echo "=> database name cannot be found" && exit 1; } - echo "=> Restore database $DB_NAME from $1" -set -o pipefail - if echo $SQL | mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$DB_NAME" then From 41ea03511f5acc62ad0253f463ded9c10b6b86f3 Mon Sep 17 00:00:00 2001 From: Stelage Date: Sun, 9 May 2021 17:58:46 +0200 Subject: [PATCH 3/3] Quotes on SQL var was missing. --- restore.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/restore.sh b/restore.sh index ab365f2..3ca9e92 100755 --- a/restore.sh +++ b/restore.sh @@ -13,13 +13,13 @@ SQL=$(gunzip -c "$1") DB_NAME=${MYSQL_DATABASE:-${MYSQL_DB}} if [ -z "${DB_NAME}"] then - DB_NAME=$(echo $SQL | grep -oE '(Database: (\w*))' | cut -d ' ' -f 2) + DB_NAME=$(echo "$SQL" | grep -oE '(Database: (\w*))' | cut -d ' ' -f 2) fi [ -z "${DB_NAME}" ] && { echo "=> database name cannot be found" && exit 1; } echo "=> Restore database $DB_NAME from $1" -if echo $SQL | mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$DB_NAME" +if echo "$SQL" | mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$DB_NAME" then echo "=> Restore succeeded" else