From 575cf402f810fba13b8522cb13c803f3a22daf5d Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 18 Feb 2018 19:17:18 +0100 Subject: [PATCH] +add mariadb dump file path to metactl.env +add connection check after mariadb container is lauched (#3) --- metactl/environment.sh | 3 ++- metactl/post-start.sh | 39 ++++++++++++++++++++++++--------------- metactl/pre-stop.sh | 2 +- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/metactl/environment.sh b/metactl/environment.sh index 616005f..576ac5c 100644 --- a/metactl/environment.sh +++ b/metactl/environment.sh @@ -10,4 +10,5 @@ NGINX_SERVICE="nginx"; # 3. Mariadb credentials MARIADB_ROOT_PASSWORD="root"; -MARIADB_DATABASE_NAME="vhost"; \ No newline at end of file +MARIADB_DATABASE_NAME="vhost"; +MARIADB_DUMP="$ROOT/persistent/mariadb.sql"; \ No newline at end of file diff --git a/metactl/post-start.sh b/metactl/post-start.sh index ec5117c..d79c307 100644 --- a/metactl/post-start.sh +++ b/metactl/post-start.sh @@ -10,11 +10,31 @@ source $(dirname `realpath $0`)/environment.sh; +## [1] Wait for database to be READY + +# Restore mariadb database +echo -ne " * mariadb socket.."; +docker exec $MARIADB_SERVICE mysql -uroot -p$MARIADB_ROOT_PASSWORD 2>/dev/null; +DB_RESTORED="$?"; + +# Try until connection OK +while [ "$DB_RESTORED" != "0" ]; do + sleep .5; + echo -ne "."; + docker exec $MARIADB_SERVICE mysql -uroot -p$MARIADB_ROOT_PASSWORD 2>/dev/null; + DB_RESTORED="$?"; +done; + +echo "ready"; + + + + ## [1] Restore database # 1. Check if dump file exists echo -ne " * Check dump file..........."; -test -f $ROOT/persistent/mariadb.sql; +test -f $MARIADB_DUMP; DUMP_FILE_EXISTS="$?"; # 2. Restore database @@ -23,19 +43,8 @@ if [ "$DUMP_FILE_EXISTS" = "0" ]; then echo "found"; # Restore mariadb database - echo -ne " * Restore database"; - cat $ROOT/persistent/mariadb.sql | docker exec -i $MARIADB_SERVICE mysql -uroot -p$MARIADB_ROOT_PASSWORD $MARIADB_DATABASE_NAME 2>/dev/null; - DB_RESTORED="$?"; - - # Try until connection OK - while [ "$DB_RESTORED" != "0" ]; do - sleep .5; - echo -ne "."; - cat $ROOT/persistent/mariadb.sql | docker exec -i $MARIADB_SERVICE mysql -uroot -p$MARIADB_ROOT_PASSWORD $MARIADB_DATABASE_NAME 2>/dev/null; - DB_RESTORED="$?"; - done; - - echo "restored"; + echo -ne " * Restore database.........."; + cat $MARIADB_DUMP | docker exec -i $MARIADB_SERVICE mysql -uroot -p$MARIADB_ROOT_PASSWORD $MARIADB_DATABASE_NAME 2>/dev/null && echo "restored" || echo "failed"; else @@ -45,7 +54,7 @@ else test ! -d $ROOT/persistent && mkdir -p $ROOT/persistent; echo " * Create dump file..........created"; - touch $ROOT/persistent/mariadb.sql; + touch $MARIADB_DUMP; fi; \ No newline at end of file diff --git a/metactl/pre-stop.sh b/metactl/pre-stop.sh index 7497d7a..d361a5a 100644 --- a/metactl/pre-stop.sh +++ b/metactl/pre-stop.sh @@ -15,5 +15,5 @@ test ! -d $ROOT/persistent && mkdir -p $ROOT/persistent; # 2. Backup database echo -ne " * Backup database..........."; -docker exec $MARIADB_SERVICE mysqldump -uroot -p$MARIADB_ROOT_PASSWORD $MARIADB_DATABASE_NAME > $ROOT/persistent/mariadb.sql; +docker exec $MARIADB_SERVICE mysqldump -uroot -p$MARIADB_ROOT_PASSWORD $MARIADB_DATABASE_NAME > $MARIADB_DUMP; test "$?" = "0" && echo "done" || (echo "failed"; exit 1); \ No newline at end of file