From 7becf8f70473833ef25466e2369fd3b46c3b365b Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Mon, 21 Mar 2022 18:02:18 +0100
Subject: [PATCH] Check if the created dump contains "insert"

---
 plugins/localdump/mysql.sh | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/plugins/localdump/mysql.sh b/plugins/localdump/mysql.sh
index f0dfebb..ae3f65b 100755
--- a/plugins/localdump/mysql.sh
+++ b/plugins/localdump/mysql.sh
@@ -12,7 +12,8 @@
 # 2022-01-20  ah     v1.1  fixes with shellcheck
 # 2022-02-14  ah     v2.0  rewrite with class like functions
 # 2022-02-18  ah     v2.1  WIP: added counters
-# 2022-03-17         v1.2  WIP: add lines with prefix __DB__
+# 2022-03-17         v2.2  WIP: add lines with prefix __DB__
+# 2022-03-21         v2.3  Check if the created dump contains "insert"
 # ================================================================================
 
 if [ -z "$LOCALDUMP_LOADED" ]; then
@@ -71,7 +72,7 @@ function mysql.db.create(){
   echo "CREATE DATABASE IF NOT EXISTS ${_dbname};" | mysql
   fetchrc >/dev/null
   test $myrc -eq 0 && mysql_COUNT_CREATE+=1
-  test $rc -eq 0 || mysql_COUNT_ERRORS+=1
+  test $myrc -eq 0 || mysql_COUNT_ERRORS+=1
 }
 
 # dump [database] --> [file] 
@@ -89,11 +90,17 @@ function mysql.db.dump(){
             --single-transaction \
             --no-autocommit \
             --result-file="$_dumpfile" \
-            "$_dbname"
+            "$_dbname" 2>&1
   fetchrc >/dev/null
-  test $myrc -eq 0 && mysql_COUNT_DUMPS+=1
-  test $rc -eq 0 || mysql_COUNT_ERRORS+=1
-  test $rc -eq 0
+
+  zgrep -i "INSERT" "$_dumpfile" >/dev/null
+  typeset -i local _rc2=$?
+  test $_rc2 -ne 0 && echo "ERROR: the dump does not contain an insert statement."
+
+  typeset -i local _rctotal=$?+$_rc2
+  test $_rctotal -eq 0 && mysql_COUNT_DUMPS+=1
+  test $_rctotal -eq 0 || mysql_COUNT_ERRORS+=1
+  test $_rctotal -eq 0
 }
 
 # import [file] --> [database] 
@@ -106,7 +113,7 @@ function mysql.db.import(){
   zcat "$_dumpfile" | mysql "${_dbname}"
   fetchrc >/dev/null
   test $myrc -eq 0 && mysql_COUNT_IMPORT+=1
-  test $rc -eq 0 || mysql_COUNT_ERRORS+=1
+  test $myrc -eq 0 || mysql_COUNT_ERRORS+=1
 }
 
 # show a list of existing databases
@@ -116,7 +123,7 @@ function mysql.db.list(){
   fetchrc >/dev/null
   test $myrc -eq 0 && mysql_COUNT_DB=$( echo "$_result" | grep -c "^Database:" ) 
   test $myrc -eq 0 && echo "$_result" | grep "^Database:" | awk '{ print $2 }'
-  test $rc -eq 0 || mysql_COUNT_ERRORS+=1
+  test $myrc -eq 0 || mysql_COUNT_ERRORS+=1
 }
 
 # --------------------------------------------------------------------------------
-- 
GitLab