From 99dce29cfc03bca1ceec7a284876e62deefd98af Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 18 Feb 2018 19:07:44 +0100 Subject: [PATCH] +upd metactl.post-start and metactl.pre-stop scripts (verbosity) for database backup/restore #3 --- metactl/post-start.sh | 45 ++++++++++++++++++++++++++++++++++++------- metactl/pre-stop.sh | 11 +++++++++-- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/metactl/post-start.sh b/metactl/post-start.sh index 3716862..ec5117c 100644 --- a/metactl/post-start.sh +++ b/metactl/post-start.sh @@ -8,13 +8,44 @@ source $(dirname `realpath $0`)/environment.sh; ## ## ############################################# -# 1.1. Restore mariadb 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="$?"; -# 1.2. Try until connection OK -while [ "$DB_RESTORED" != "0" ]; do - sleep 1; + +## [1] Restore database + +# 1. Check if dump file exists +echo -ne " * Check dump file..........."; +test -f $ROOT/persistent/mariadb.sql; +DUMP_FILE_EXISTS="$?"; + +# 2. Restore database +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="$?"; -done; \ No newline at end of file + + # 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"; + +else + + echo "missing"; + + # Create 'persistent' directory if missing + test ! -d $ROOT/persistent && mkdir -p $ROOT/persistent; + + echo " * Create dump file..........created"; + touch $ROOT/persistent/mariadb.sql; + + +fi; \ No newline at end of file diff --git a/metactl/pre-stop.sh b/metactl/pre-stop.sh index 772376d..7497d7a 100644 --- a/metactl/pre-stop.sh +++ b/metactl/pre-stop.sh @@ -8,5 +8,12 @@ source $(dirname `realpath $0`)/environment.sh; ## ## ############################################# -# 1. Backup mariadb database -docker exec $MARIADB_SERVICE mysqldump -uroot -p$MARIADB_ROOT_PASSWORD $MARIADB_DATABASE_NAME > $ROOT/persistent/mariadb.sql; \ No newline at end of file +## [1] Backup mariadb database + +# 1. Create parent dir (if does not exists) +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; +test "$?" = "0" && echo "done" || (echo "failed"; exit 1); \ No newline at end of file