Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
iml-backup
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IML Open Source
iml-backup
Merge requests
!42
mysql as prototype/ POC
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
mysql as prototype/ POC
update-localdump-to-classlike-functions
into
master
Overview
0
Commits
1
Pipelines
0
Changes
2
Merged
Hahn Axel (hahn)
requested to merge
update-localdump-to-classlike-functions
into
master
3 years ago
Overview
0
Commits
1
Pipelines
0
Changes
2
Expand
0
0
Merge request reports
Compare
master
master (base)
and
latest version
latest version
6fde8187
1 commit,
3 years ago
2 files
+
187
−
72
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
plugins/localdump/mysql.sh
+
180
−
69
Options
@@ -10,12 +10,13 @@
@@ -10,12 +10,13 @@
#
#
# 2016-11-10 ah,ds v0.8 needs to be testet
# 2016-11-10 ah,ds v0.8 needs to be testet
# 2017-03-28 ..... v1.0 added restore
# 2017-03-28 ..... v1.0 added restore
# 2022-01-20 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
# ================================================================================
# ================================================================================
if
[
-z
"
$BACKUP_TARGETDIR
"
]
;
then
if
[
-z
"
$BACKUP_TARGETDIR
"
]
;
then
echo
"ERROR: you cannot start
$(
basename
$0
)
directly"
echo
"ERROR: you cannot start
$(
basename
$0
)
directly"
rc
=
$rc
+1
rc+
=
1
exit
1
exit
1
fi
fi
@@ -23,114 +24,224 @@ fi
@@ -23,114 +24,224 @@ fi
# CONFIG
# CONFIG
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
mysql_FOUND
=
0
SOURCE_DIR
=
/var/lib/mysql
SOURCE_DIR
=
/var/lib/mysql
MYSQLDUMP
=
/usr/bin/mysqldump
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
# FUNCTIONS
#
PRIVATE
FUNCTIONS
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
function
doMysqlBackup
(){
# make checks if a service is available on this machine
# it sets mysql_FOUND as flag
function
mysql._check
(){
j_requireBinary
"mysql"
1
j_requireBinary
"mysqldump"
1
j_requireProcess
"mysqld|mariadb"
1
if
[
!
-d
$SOURCE_DIR
]
;
then
echo
"INFO: directory
$SOURCE_DIR
doees not exist."
rc
=
$rc
+1
fi
# set flag and reset return code
test
$rc
-eq
0
&&
mysql_FOUND
=
1
rc
=
0
}
# --------------------------------------------------------------------------------
# PUBLIC FUNCTIONS I :: DB SCHEME - METHODS LOW LEVEL
# --------------------------------------------------------------------------------
# create a database scheme
# param string name of the dabase scheme
function
mysql.db.create
(){
local
_dbname
=
$1
echo
"CREATE DATABASE IF NOT EXISTS
${
_dbname
}
;"
| mysql
}
# dump [database] --> [file]
# dump a single database into given file
# param string name of database to dump
# 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
"
}
# import [file] --> [database]
# import a single db dump into a given db
# param string name of file
# param string name of target db scheme
function
mysql.db.import
(){
local
_dumpfile
=
$1
local
_dbname
=
$2
zcat
"
$_dumpfile
"
| mysql
"
${
_dbname
}
"
}
# show a list of existing databases
function
mysql.db.list
(){
mysql
-Ee
"show databases ;"
|
grep
"^Database:"
|
awk
'{ print $2 }'
}
# --------------------------------------------------------------------------------
# PUBLIC FUNCTIONS II :: HIGH LEVEL
# --------------------------------------------------------------------------------
# return result is the current service available
# USAGE: to abort a function if not available:
# mysql.available || return
function
mysql.available
(){
typeset
-i
local
_rc
=(
1-
$mysql_FOUND
)
return
$_rc
}
# make checks if the current service is available on this machine
# param bool flag: silence; if any parameter is set it shows no output
function
mysql.check
(){
if
[
-n
"
$1
"
]
;
then
mysql._check
>
/dev/null 2>&1
else
echo
echo
Details:
mysql._check
echo
fi
}
# start database backup of all schemes of this service
# no prameters
function
mysql.backup
(){
# abort if service is not available
mysql.available
||
return
local
_dbname
local
_outfile
create_targetdir
create_targetdir
for
DATABASE_DIR
in
$(
f
in
d
$
SOURCE_DIR
/
*
-type
d
-prune
)
;
for
_dbname
in
$
(
mysql.db.list
)
do
do
# DATABASE=`basename $DATABASE_DIR`
echo
"--- database
$_dbname
"
DATABASE
=
$(
basename
"
$DATABASE_DIR
"
|
sed
"s#
\@
002d#-#g"
)
echo
-n
"backup ... "
TABLECOUNT
=
$(
find
"
$DATABASE_DIR
"
/.
-type
f
-name
"*frm"
|
wc
-l
)
_outfile
=
"
${
BACKUP_TARGETDIR
}
/
$(
get_outfile
${
_dbname
}
)
.sql"
echo
"--- database
$DATABASE
-
$TABLECOUNT
tables"
if
[
$TABLECOUNT
-gt
0
]
;
then
mysql.db.dump
"
$_dbname
"
"
$_outfile
"
echo
-n
"backup ... "
fetchrc
OUTFILE
=
"
${
BACKUP_TARGETDIR
}
/
$(
get_outfile
${
DATABASE
}
)
.sql"
# task 1251 - "--master-data=2" was removed
# $myrc is last returncode - set in fetchrc
$MYSQLDUMP
--opt
\
if
[
$myrc
-eq
0
]
;
then
--default-character-set
=
utf8
\
echo
-n
"gzip ... "
--flush-logs
\
compress_file
"
$_outfile
"
--single-transaction
\
else
--no-autocommit
\
color error
--result-file
=
"
$OUTFILE
"
\
echo
"ERROR occured - no gzip"
"
$DATABASE
"
color reset
fetchrc
# $myrc is last returncode - set in fetchrc
if
[
$myrc
-eq
0
]
;
then
echo
-n
"gzip ... "
compress_file
"
$OUTFILE
"
else
echo
"ERROR occured - no gzip"
fi
ls
-l
"
$OUTFILE
"
*
fi
fi
ls
-l
"
$_outfile
"
*
echo
echo
done
done
}
}
# show help
function
mysql.help
(){
# local _bShow=false
# tac "$0" | while read line
# do
# if echo $line | grep "^function mysql\.[a-z]" >/dev/null; then
# _bShow = true
# fi
# if echo $line | grep "^# " >/dev/null; then
# _bShow = true
# fi
# done
cat
<<
EOHELP
help for MYSQL-DOT functions
(1) high level functions
mysql.check [0|1] check if mysql is available
mysql.backup backup all databases
mysql.restore FILE DBNAME restore database
(2) functions on database level
mysql.db.create DBNAME create database
mysql.db.dump DBNAME OUTFILE dump given database to output file
mysql.db.import FILE DBNAME import file into existing database
mysql.db.list list existing databases
EOHELP
}
# restore database dump file into database
# restore database dump file into database
# param string database dump file (gzipped)
# param string database dump file (gzipped)
# param string optional: database to import; default: database is parsed from file
# param string optional: database to import; default: database is parsed from file
function
restoreByFile
(){
function
mysql.restore
(){
sMyfile
=
$1
sMyDb
=
$2
# abort if service is not available
mysql.available
||
return
if
[
-z
"
$sMyDb
"
]
;
then
h2
"analyze dump
$sMyfile
"
local
_infile
=
$1
sMyDb
=
$(
guessDB
$sMyfile
)
local
_dbname
=
$2
echo
"detected db schema from file: [
${
sMyDb
}
]"
if
[
-z
"
$_dbname
"
]
;
then
h2
"analyze dump
$_infile
"
_dbname
=
$(
guessDB
$_infile
)
echo
"detected db schema from file: [
${
_dbname
}
]"
else
else
echo
"db schema from param 2: [
${
sMyDb
}
]"
echo
"db schema from param 2: [
${
_dbname
}
]"
fi
fi
echo
echo
echo
import to
"
$
sMyDb
"
...
echo
import to
"
$
_dbname
"
...
h2 ensure that database exists ...
h2 ensure that database exists ...
color cmd
color cmd
echo
"CREATE DATABASE IF NOT EXISTS
${
sMyDb
}
;"
| mysql
# echo "CREATE DATABASE IF NOT EXISTS ${_dbname};" | mysql
mysql.db.create
"
${
_dbname
}
"
color reset
color reset
h2 import ...
h2 import ...
ls
-l
"
$
sMy
file
"
ls
-l
"
$
_in
file
"
echo
"import to database [
${
sMyDb
}
]"
echo
"import to database [
${
_dbname
}
]"
color cmd
color cmd
zcat
"
$sMyfile
"
| mysql
"
${
sMyDb
}
"
# zcat "$_infile" | mysql "${_dbname}"
mysql.db.import
"
$_infile
"
"
${
_dbname
}
"
fetchrc
fetchrc
color reset
color reset
}
}
function
mysql.shell
(){
echo
"hi"
}
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
# MAIN
# MAIN
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
"
${
SERVICENAME
}
"
.check 1
# ----- check requirements
if
!
"
${
SERVICENAME
}
"
.available
;
then
j_requireBinary
"mysql"
1
echo
"INFO: service [
$SERVICENAME
] is not avilable on this machine."
j_requireBinary
"mysqldump"
1
j_requireProcess
"mysqld|mariadb"
1
if
[
!
-d
$SOURCE_DIR
]
;
then
echo
"INFO: directory
$SOURCE_DIR
doees not exist."
rc
=
$rc
+1
fi
fi
action
=
$1
shift
1
"
${
SERVICENAME
}
.
$action
"
$*
if
[
$rc
-ne
0
]
;
then
echo
"INFO:
$0
$action
$*
[
$SERVICENAME
] final returncode rc=
$rc
"
rc
=
0
echo
"SKIP: mysqld seems not to be here"
else
if
[
"
$1
"
=
"restore"
]
;
then
echo
shift
1
restoreByFile
$*
else
doMysqlBackup
fi
fi
echo
"
$0
$*
[mysql] final returncode rc=
$rc
"
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
Loading