Skip to content
Snippets Groups Projects
Commit 2fb5acde authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

add db import and export; update docs

parent afc8f55b
Branches
No related tags found
1 merge request!20DB Import/ Export
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
# 2024-07-29 v1.17 <www.axel-hahn.de> hide unnecessary menu items; reorder functions # 2024-07-29 v1.17 <www.axel-hahn.de> hide unnecessary menu items; reorder functions
# 2024-08-14 v1.18 <www.axel-hahn.de> update container view # 2024-08-14 v1.18 <www.axel-hahn.de> update container view
# 2024-09-20 v1.19 <www.axel-hahn.de> detect dockerd-rootless (hides menu item to set permissions) # 2024-09-20 v1.19 <www.axel-hahn.de> detect dockerd-rootless (hides menu item to set permissions)
# 2024-10-16 v1.20 <axel.hahn@unibe.ch> add db import and export
# ====================================================================== # ======================================================================
cd "$( dirname "$0" )" || exit 1 cd "$( dirname "$0" )" || exit 1
...@@ -37,7 +38,7 @@ _self=$( basename "$0" ) ...@@ -37,7 +38,7 @@ _self=$( basename "$0" )
# shellcheck source=/dev/null # shellcheck source=/dev/null
. "${_self}.cfg" || exit 1 . "${_self}.cfg" || exit 1
_version="1.19" _version="1.20"
# git@git-repo.iml.unibe.ch:iml-open-source/docker-php-starterkit.git # git@git-repo.iml.unibe.ch:iml-open-source/docker-php-starterkit.git
selfgitrepo="docker-php-starterkit.git" selfgitrepo="docker-php-starterkit.git"
...@@ -152,7 +153,7 @@ function showMenu(){ ...@@ -152,7 +153,7 @@ function showMenu(){
echo "${_spacer}$( _key T ) - remove generated files" echo "${_spacer}$( _key T ) - remove generated files"
fi fi
echo echo
if [ $DC_WEB_UP -eq 0 ] || [ $_bAll -eq 1 ]; then if [ $DC_WEB_UP -eq 0 ] || [ $DC_DB_UP -eq 0 ] || [ $_bAll -eq 1 ]; then
if [ $DC_CONFIG_CHANGED -eq 0 ] || [ $_bAll -eq 1 ]; then if [ $DC_CONFIG_CHANGED -eq 0 ] || [ $_bAll -eq 1 ]; then
echo "${_spacer}$( _key u ) - startup containers docker-compose ... up -d" echo "${_spacer}$( _key u ) - startup containers docker-compose ... up -d"
echo "${_spacer}$( _key U ) - startup containers docker-compose ... up -d --build" echo "${_spacer}$( _key U ) - startup containers docker-compose ... up -d --build"
...@@ -160,14 +161,20 @@ function showMenu(){ ...@@ -160,14 +161,20 @@ function showMenu(){
echo "${_spacer}$( _key r ) - remove containers docker-compose rm -f" echo "${_spacer}$( _key r ) - remove containers docker-compose rm -f"
fi fi
fi fi
if [ $DC_WEB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then if [ $DC_WEB_UP -eq 1 ] || [ $DC_DB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then
echo "${_spacer}$( _key s ) - shutdown containers docker-compose stop" echo "${_spacer}$( _key s ) - shutdown containers docker-compose stop"
echo echo
echo "${_spacer}$( _key m ) - more infos" echo "${_spacer}$( _key i ) - Import more into infos"
echo "${_spacer}$( _key o ) - open app [${APP_NAME}] $DC_WEB_URL" echo "${_spacer}$( _key o ) - open app [${APP_NAME}] $DC_WEB_URL"
echo "${_spacer}$( _key c ) - console (bash)" echo "${_spacer}$( _key c ) - console (bash)"
fi
if [ $DC_WEB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then
echo "${_spacer}$( _key p ) - console check with php linter" echo "${_spacer}$( _key p ) - console check with php linter"
fi fi
if [ $DC_DB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then
echo "${_spacer}$( _key d ) - Dump container database"
echo "${_spacer}$( _key D ) - Import Dump into container database"
fi
echo echo
echo "${_spacer}$( _key q ) - quit" echo "${_spacer}$( _key q ) - quit"
...@@ -498,6 +505,75 @@ function _wait(){ ...@@ -498,6 +505,75 @@ function _wait(){
echo echo
} }
# DB TOOL - dump db from container
function _dbDump(){
local _iKeepDumps;
typeset -i _iKeepDumps=5
local _iStart;
typeset -i _iStart=$_iKeepDumps+1;
if [ $DC_DB_UP -eq 0 ]; then
echo "Database container is not running. Aborting."
return
fi
outfile=dbdumps/${MYSQL_DB}_$( date +%Y%m%d_%H%M%S ).sql
echo -n "dumping ${MYSQL_DB} ... "
if docker exec -i "${APP_NAME}-db" mysqldump -uroot -p${MYSQL_ROOT_PASS} ${MYSQL_DB} > "$outfile"; then
echo -n "OK ... Gzip ... "
if gzip "${outfile}"; then
echo "OK"
ls -l "$outfile.gz"
# CLEANUP
echo
echo "--- Cleanup: keep $_iKeepDumps files."
ls -1t dbdumps/* | sed -n "$_iStart,\$p" | while read -r delfile
do
echo "CLEANUP: Deleting $delfile ... "
rm -f "$delfile"
done
echo
echo -n "Size of dump directory: "
du -hs dbdumps | awk '{ print $1 }'
else
echo "ERROR"
rm -f "$outfile"
fi
else
echo "ERROR"
rm -f "$outfile"
fi
}
# DB TOOL - import local database dump into container
function _dbImport(){
echo "--- Available dumps:"
ls -ltr dbdumps/*.gz | sed "s#^# #g"
if [ $DC_DB_UP -eq 0 ]; then
echo "Database container is not running. Aborting."
return
fi
echo -n "Dump file to import into ${MYSQL_DB} > "
read -r dumpfile
if [ -z "$dumpfile" ]; then
echo "Abort - no value was given."
return
fi
if [ ! -f "$dumpfile" ]; then
echo "Abort - wrong filename."
return
fi
echo -n "Importing $dumpfile ... "
if zcat "$dumpfile" | docker exec -i "${APP_NAME}-db" mysql -uroot -p${MYSQL_ROOT_PASS} "${MYSQL_DB}"
then
echo "OK"
else
echo "ERROR"
fi
}
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# MAIN # MAIN
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
...@@ -612,6 +688,14 @@ while true; do ...@@ -612,6 +688,14 @@ while true; do
echo "Start your docker container first." echo "Start your docker container first."
fi fi
;; ;;
d)
h2 "DB tools :: dump"
_dbDump
;;
D)
h2 "DB tools :: import"
_dbImport
;;
o) o)
h2 "Open app ..." h2 "Open app ..."
xdg-open "$DC_WEB_URL" xdg-open "$DC_WEB_URL"
......
...@@ -63,7 +63,6 @@ services: ...@@ -63,7 +63,6 @@ services:
volumes: volumes:
- ./containers/db-server/mariadb/my.cnf:/etc/mysql/conf.d/my.cnf - ./containers/db-server/mariadb/my.cnf:/etc/mysql/conf.d/my.cnf
- ./containers/db-server/mariadb/dot_my.cnf:/root/.my.cnf - ./containers/db-server/mariadb/dot_my.cnf:/root/.my.cnf
- ./containers/db-server/var_tmp_db-data/:/var/tmp/db-data
healthcheck: healthcheck:
test: mysqladmin ping -h 127.0.0.1 -u root --password=$$MYSQL_ROOT_PASSWORD test: mysqladmin ping -h 127.0.0.1 -u root --password=$$MYSQL_ROOT_PASSWORD
interval: 5s interval: 5s
......
...@@ -7,6 +7,10 @@ ...@@ -7,6 +7,10 @@
error_reporting=E_ALL error_reporting=E_ALL
display_errors=1 display_errors=1
; If you want to catch emails locally in your dev environment
; eg. https://os-docs.iml.unibe.ch/php-emailcatcher/
; sendmail_path = "php {{WEBROOT}}/vendor/emailcatcher/php-sendmail.php"
; ---------------------------------------------------------------------- ; ----------------------------------------------------------------------
; XDEBUG STUFF BELOW ; XDEBUG STUFF BELOW
; ---------------------------------------------------------------------- ; ----------------------------------------------------------------------
......
...@@ -12,19 +12,20 @@ There is a menu offerning a key and the description for its action. If an action ...@@ -12,19 +12,20 @@ There is a menu offerning a key and the description for its action. If an action
The script supports command line parameters to use it in scripts. Use `-h` to get a help: The script supports command line parameters to use it in scripts. Use `-h` to get a help:
```txt ```txt
INITIALIZER FOR DOCKER APP v1.12
INITIALIZER FOR DOCKER APP v1.20
A helper script written in Bash to bring up a PHP+Mysql application in docker. A helper script written in Bash to bring up a PHP+Mysql application in docker.
Source : https://git-repo.iml.unibe.ch/iml-open-source/docker-php-starterkit 📄 Source : https://git-repo.iml.unibe.ch/iml-open-source/docker-php-starterkit
Docs : https://os-docs.iml.unibe.ch/docker-php-starterkit/ 📗 Docs : https://os-docs.iml.unibe.ch/docker-php-starterkit/
License: GNU GPL 3.0 📜 License: GNU GPL 3.0
(c) Institute for Medical Education; University of Bern (c) Institute for Medical Education; University of Bern
SYNTAX: SYNTAX:
init.sh [-h|-v] init.sh [-h|-v]
init.sh [menu key] init.sh [menu key [.. menu key N]]
OPTIONS: OPTIONS:
-h show this help and exit -h show this help and exit
...@@ -37,21 +38,24 @@ MENU KEYS: ...@@ -37,21 +38,24 @@ MENU KEYS:
g - remove git data of starterkit g - remove git data of starterkit
i - init application: set permissions i - init application: set permissions
t - generate files from templates t - generate files from templates
T - remove generated files T - remove generated files
u - startup containers docker-compose ... up -d u - startup containers docker-compose ... up -d
U - startup containers docker-compose ... up -d --build U - startup containers docker-compose ... up -d --build
s - shutdown containers docker-compose stop
r - remove containers docker-compose rm -f r - remove containers docker-compose rm -f
s - shutdown containers docker-compose stop
m - more infos
i - Import more into infos
o - open app [my_new_app] http://localhost:8001/ o - open app [my_new_app] http://localhost:8001/
c - console (bash) c - console (bash)
p - console check with php linter p - console check with php linter
d - Dump container database
D - Import Dump into container database
q - quit q - quit
EXAMPLES: EXAMPLES:
...@@ -132,4 +136,20 @@ Start a docker command on the webserver container and start php lint for all php ...@@ -132,4 +136,20 @@ Start a docker command on the webserver container and start php lint for all php
Starting from PHP 8.3 php -l supports multiple files. A newer PHP version will be detected to use the faster mode automatically: Starting from PHP 8.3 php -l supports multiple files. A newer PHP version will be detected to use the faster mode automatically:
`php -l $( find . -name '*.php' ) | grep -v '^No syntax errors detected'` `php -l $( find . -name '*.php' ) | grep -v '^No syntax errors detected'`
\ No newline at end of file
#### d - Dump container database
This feature works if the database container is running.
This feature dumps the app database of the database container into `./dbdumps/` using mysqldump. The resulting sql file will be gzipped.
The directory will be cleaned up: the latest 5 latest dump files will be kept.
#### D - Import Dump into container database
This feature works if the database container is running.
You get a file selection of the `./dbdumps/` directory.
The selected sql or sql.gz file will be imported.
**Hint**: With this feature you can import a dump from a live system into your dev environment too. Just copy its dump into `./dbdumps/`.
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment