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
3
+ 58
33
#!/bin/bash
# ================================================================================
#
# LOCALDUMP :: MYSQL / MARIADB
@@ -12,10 +11,11 @@
# 2017-03-28 ..... v1.0 added restore
# 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
# ================================================================================
if [ -z "$BACKUP_TARGETDIR" ]; then
echo "ERROR: you cannot start $(basename $0) directly"
if [ -z "$LOCALDUMP_LOADED" ]; then
echo "ERROR: you cannot start $(basename $0) directly. Start localdump.sh instead."
rc+=1
exit 1
fi
@@ -24,7 +24,15 @@ fi
# CONFIG
# --------------------------------------------------------------------------------
# flag: service was foound locally? One of 0|1
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
@@ -47,6 +55,7 @@ function mysql._check(){
# set flag and reset return code
test $rc -eq 0 && mysql_FOUND=1
test $rc -eq 0 || mysql_COUNT_ERRORS+=1
rc=0
}
@@ -59,6 +68,9 @@ function mysql._check(){
function mysql.db.create(){
local _dbname=$1
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]
@@ -67,17 +79,19 @@ function mysql.db.create(){
# param string name of output file
function mysql.db.dump(){
local _dbname=$1
local _dumpfile=$2
mysqldump --opt \
--default-character-set=utf8 \
--flush-logs \
--single-transaction \
--no-autocommit \
--result-file="$_dumpfile" \
"$_dbname"
local _dbname=$1
local _dumpfile=$2
mysqldump --opt \
--default-character-set=utf8 \
--flush-logs \
--single-transaction \
--no-autocommit \
--result-file="$_dumpfile" \
"$_dbname"
fetchrc >/dev/null
test $myrc -eq 0 && mysql_COUNT_DUMPS+=1
test $rc -eq 0 || mysql_COUNT_ERRORS+=1
}
# import [file] --> [database]
@@ -88,11 +102,19 @@ function mysql.db.import(){
local _dumpfile=$1
local _dbname=$2
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
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(){
_outfile="${BACKUP_TARGETDIR}/$(get_outfile ${_dbname}).sql"
mysql.db.dump "$_dbname" "$_outfile"
fetchrc
# $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
db._compressDumpfile "$_outfile"
done
}
@@ -169,13 +180,14 @@ function mysql.help(){
# fi
# done
cat <<EOHELP
help for MYSQL-DOT functions
Help for MYSQL-DOT 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.restore FILE DBNAME restore database
mysql.restore [FILE [DBNAME]] restore database
(2) functions on database level
@@ -226,8 +238,21 @@ function mysql.restore(){
}
function mysql.shell(){
echo "hi"
# WIP: show status
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
Loading