diff --git a/docker/dbdumps/.gitkeep b/docker/dbdumps/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/docker/init.sh b/docker/init.sh index d75223516bd09e854db9378231f85b42b1ba4e70..9464d8757259adb4f7b1d772e24fca010e596797 100755 --- a/docker/init.sh +++ b/docker/init.sh @@ -24,6 +24,7 @@ # 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-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 @@ -37,7 +38,7 @@ _self=$( basename "$0" ) # shellcheck source=/dev/null . "${_self}.cfg" || exit 1 -_version="1.19" +_version="1.20" # git@git-repo.iml.unibe.ch:iml-open-source/docker-php-starterkit.git selfgitrepo="docker-php-starterkit.git" @@ -152,7 +153,7 @@ function showMenu(){ echo "${_spacer}$( _key T ) - remove generated files" fi 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 echo "${_spacer}$( _key u ) - startup containers docker-compose ... up -d" echo "${_spacer}$( _key U ) - startup containers docker-compose ... up -d --build" @@ -160,14 +161,20 @@ function showMenu(){ echo "${_spacer}$( _key r ) - remove containers docker-compose rm -f" 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 - 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 c ) - console (bash)" + fi + if [ $DC_WEB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then echo "${_spacer}$( _key p ) - console check with php linter" 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 "${_spacer}$( _key q ) - quit" @@ -498,6 +505,75 @@ function _wait(){ 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 # ---------------------------------------------------------------------- @@ -612,6 +688,14 @@ while true; do echo "Start your docker container first." fi ;; + d) + h2 "DB tools :: dump" + _dbDump + ;; + D) + h2 "DB tools :: import" + _dbImport + ;; o) h2 "Open app ..." xdg-open "$DC_WEB_URL" diff --git a/docker/templates/db_data_readme.md b/docker/templates/db_data_readme.md deleted file mode 100644 index 1c357dbf700b72678c14be0b2e9b1e9d019ccc5e..0000000000000000000000000000000000000000 --- a/docker/templates/db_data_readme.md +++ /dev/null @@ -1,5 +0,0 @@ -# TARGET: docker/containers/db-server/var_tmp_db-data/readme.txt - -# Info - -content of `docker/containers/db_server/var_tmp_db-data/` will be visible in the database container as `/var/tmp/db-data/`. It is a helper to transfer/ import sqldumps. diff --git a/docker/templates/docker-compose.yml b/docker/templates/docker-compose.yml index 8285d5fdae96d927cce39f4ef970c550dff69594..75b983803c3b37131b798883e1a2a6d08580e73c 100644 --- a/docker/templates/docker-compose.yml +++ b/docker/templates/docker-compose.yml @@ -63,7 +63,6 @@ services: volumes: - ./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/var_tmp_db-data/:/var/tmp/db-data healthcheck: test: mysqladmin ping -h 127.0.0.1 -u root --password=$$MYSQL_ROOT_PASSWORD interval: 5s diff --git a/docker/templates/extra-php-config.ini b/docker/templates/extra-php-config.ini index d3f53279bbc03ee66876efaeec97cfdd207f3c94..0b2fc0e716737468146164a31e30750513ba4b5c 100644 --- a/docker/templates/extra-php-config.ini +++ b/docker/templates/extra-php-config.ini @@ -7,6 +7,10 @@ error_reporting=E_ALL 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 ; ---------------------------------------------------------------------- diff --git "a/docs/50_\342\214\250\357\270\217_Usage.md" "b/docs/50_\342\214\250\357\270\217_Usage.md" index 288934ff73c1662b543f1dd4a239e4745c92ef80..8284eb1cca9e69f8e7da4528ff5f58e283c75f1b 100644 --- "a/docs/50_\342\214\250\357\270\217_Usage.md" +++ "b/docs/50_\342\214\250\357\270\217_Usage.md" @@ -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: ```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. -Source : https://git-repo.iml.unibe.ch/iml-open-source/docker-php-starterkit -Docs : https://os-docs.iml.unibe.ch/docker-php-starterkit/ -License: GNU GPL 3.0 +📄 Source : https://git-repo.iml.unibe.ch/iml-open-source/docker-php-starterkit +📗 Docs : https://os-docs.iml.unibe.ch/docker-php-starterkit/ +📜 License: GNU GPL 3.0 (c) Institute for Medical Education; University of Bern SYNTAX: init.sh [-h|-v] - init.sh [menu key] + init.sh [menu key [.. menu key N]] OPTIONS: -h show this help and exit @@ -37,21 +38,24 @@ MENU KEYS: g - remove git data of starterkit - + i - init application: set permissions t - generate files from templates T - remove generated files - + u - startup containers docker-compose ... up -d U - startup containers docker-compose ... up -d --build - s - shutdown containers docker-compose stop + r - remove containers docker-compose rm -f - - m - more infos + s - shutdown containers docker-compose stop + + i - Import more into infos o - open app [my_new_app] http://localhost:8001/ c - console (bash) p - console check with php linter - + d - Dump container database + D - Import Dump into container database + q - quit EXAMPLES: @@ -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: -`php -l $( find . -name '*.php' ) | grep -v '^No syntax errors detected'` \ No newline at end of file +`php -l $( find . -name '*.php' ) | grep -v '^No syntax errors detected'` + +#### 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