Skip to content
Snippets Groups Projects

Update localdump to classlike functions

Merged Hahn Axel (hahn) requested to merge update-localdump-to-classlike-functions into master
3 files
+ 169
66
Compare changes
  • Side-by-side
  • Inline

Files

+ 58
33
#!/bin/bash
# ================================================================================
# ================================================================================
#
#
# LOCALDUMP :: MYSQL / MARIADB
# LOCALDUMP :: MYSQL / MARIADB
@@ -12,10 +11,11 @@
@@ -12,10 +11,11 @@
# 2017-03-28 ..... v1.0 added restore
# 2017-03-28 ..... v1.0 added restore
# 2022-01-20 ah v1.1 fixes with shellcheck
# 2022-01-20 ah v1.1 fixes with shellcheck
# 2022-02-14 ah v2.0 rewrite with class like functions
# 2022-02-14 ah v2.0 rewrite with class like functions
 
# 2022-02-18 ah v2.1 WIP: added counters
# ================================================================================
# ================================================================================
if [ -z "$BACKUP_TARGETDIR" ]; then
if [ -z "$LOCALDUMP_LOADED" ]; then
echo "ERROR: you cannot start $(basename $0) directly"
echo "ERROR: you cannot start $(basename $0) directly. Start localdump.sh instead."
rc+=1
rc+=1
exit 1
exit 1
fi
fi
@@ -24,7 +24,15 @@ fi
@@ -24,7 +24,15 @@ fi
# CONFIG
# CONFIG
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
 
# flag: service was foound locally? One of 0|1
mysql_FOUND=0
mysql_FOUND=0
 
 
# counters
 
mysql_COUNT_CREATE=0
 
mysql_COUNT_DUMPS=0
 
mysql_COUNT_DB=0
 
mysql_COUNT_ERRORS=0
 
SOURCE_DIR=/var/lib/mysql
SOURCE_DIR=/var/lib/mysql
@@ -47,6 +55,7 @@ function mysql._check(){
@@ -47,6 +55,7 @@ function mysql._check(){
# set flag and reset return code
# set flag and reset return code
test $rc -eq 0 && mysql_FOUND=1
test $rc -eq 0 && mysql_FOUND=1
 
test $rc -eq 0 || mysql_COUNT_ERRORS+=1
rc=0
rc=0
}
}
@@ -59,6 +68,9 @@ function mysql._check(){
@@ -59,6 +68,9 @@ function mysql._check(){
function mysql.db.create(){
function mysql.db.create(){
local _dbname=$1
local _dbname=$1
echo "CREATE DATABASE IF NOT EXISTS ${_dbname};" | mysql
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
}
}
# dump [database] --> [file]
# dump [database] --> [file]
@@ -67,17 +79,19 @@ function mysql.db.create(){
@@ -67,17 +79,19 @@ function mysql.db.create(){
# param string name of output file
# param string name of output file
function mysql.db.dump(){
function mysql.db.dump(){
local _dbname=$1
local _dbname=$1
local _dumpfile=$2
local _dumpfile=$2
mysqldump --opt \
mysqldump --opt \
--default-character-set=utf8 \
--default-character-set=utf8 \
--flush-logs \
--flush-logs \
--single-transaction \
--single-transaction \
--no-autocommit \
--no-autocommit \
--result-file="$_dumpfile" \
--result-file="$_dumpfile" \
"$_dbname"
"$_dbname"
fetchrc >/dev/null
 
test $myrc -eq 0 && mysql_COUNT_DUMPS+=1
 
test $rc -eq 0 || mysql_COUNT_ERRORS+=1
}
}
# import [file] --> [database]
# import [file] --> [database]
@@ -88,11 +102,19 @@ function mysql.db.import(){
@@ -88,11 +102,19 @@ function mysql.db.import(){
local _dumpfile=$1
local _dumpfile=$1
local _dbname=$2
local _dbname=$2
zcat "$_dumpfile" | mysql "${_dbname}"
zcat "$_dumpfile" | mysql "${_dbname}"
 
fetchrc >/dev/null
 
test $myrc -eq 0 && mysql_COUNT_IMPORT+=1
 
test $rc -eq 0 || mysql_COUNT_ERRORS+=1
}
}
# show a list of existing databases
# show a list of existing databases
function mysql.db.list(){
function mysql.db.list(){
mysql -Ee "show databases ;" | grep "^Database:" | awk '{ print $2 }'
# mysql -Ee "show databases ;" | grep "^Database:" | awk '{ print $2 }'
 
local _result=$( mysql -Ee "show databases ;" )
 
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
}
}
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
@@ -139,19 +161,8 @@ function mysql.backup(){
@@ -139,19 +161,8 @@ function mysql.backup(){
_outfile="${BACKUP_TARGETDIR}/$(get_outfile ${_dbname}).sql"
_outfile="${BACKUP_TARGETDIR}/$(get_outfile ${_dbname}).sql"
mysql.db.dump "$_dbname" "$_outfile"
mysql.db.dump "$_dbname" "$_outfile"
fetchrc
db._compressDumpfile "$_outfile"
# $myrc is last returncode - set in fetchrc
if [ $myrc -eq 0 ]; then
echo -n "gzip ... "
compress_file "$_outfile"
else
color error
echo "ERROR occured - no gzip"
color reset
fi
ls -l "$_outfile"*
echo
done
done
}
}
@@ -169,13 +180,14 @@ function mysql.help(){
@@ -169,13 +180,14 @@ function mysql.help(){
# fi
# fi
# done
# done
cat <<EOHELP
cat <<EOHELP
help for MYSQL-DOT functions
Help for MYSQL-DOT functions
(1) high level functions
(1) high level functions
mysql.check [0|1] check if mysql is available
mysql.available silent; exitcode is 0 if mysql is available
 
mysql.check [0|1] check if mysql is available; shows missing checks
mysql.backup backup all databases
mysql.backup backup all databases
mysql.restore FILE DBNAME restore database
mysql.restore [FILE [DBNAME]] restore database
(2) functions on database level
(2) functions on database level
@@ -226,8 +238,21 @@ function mysql.restore(){
@@ -226,8 +238,21 @@ function mysql.restore(){
}
}
function mysql.shell(){
# WIP: show status
echo "hi"
function mysql.status(){
 
h2 "WIP: Status"
 
h3 "Databases (max. 15)"
 
mysql.db.list | head -15
 
h3 "Counters"
 
cat <<EOSTATUS
 
 
found Dbs: $mysql_COUNT_DB
 
created : $mysql_COUNT_CREATE
 
dumped : $mysql_COUNT_DUMPS
 
 
ERRORS : $mysql_COUNT_ERRORS
 
 
EOSTATUS
}
}
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
# MAIN
# MAIN
Loading