diff --git a/.metadata b/.metadata index 24fd331..2987b9a 100644 Binary files a/.metadata and b/.metadata differ diff --git a/.recipes/mediawiki_mariadb/docker-compose.yml b/.recipes/mediawiki_mariadb/docker-compose.yml index 3a70e66..242c62f 100644 --- a/.recipes/mediawiki_mariadb/docker-compose.yml +++ b/.recipes/mediawiki_mariadb/docker-compose.yml @@ -1,6 +1,5 @@ # MediaWiki with MariaDB (optionally with extensions). # -#version: '3' services: # https://hub.docker.com/_/mediawiki mediawiki: @@ -17,10 +16,9 @@ services: # Needs R/W UID:GID 33:33 (www-data:www-data). - ./storage/volumes/mediawiki_images:/var/www/html/images # After initial setup, download LocalSettings.php - # populate the following line and - # use compose to restart the mediawiki service. + # populate the following line and restart the mediawiki service. # Needs read UID or GID 33 (www-data). - # - ./configs/LocalSettings.php:/var/www/html/LocalSettings.php:ro + #- ./configs/LocalSettings.php:/var/www/html/LocalSettings.php:ro extra_hosts: - "host.docker.internal:host-gateway" labels: diff --git a/.recipes/mediawiki_mariadb/tools/backup.d/dumpdb_mysql.sh b/.recipes/mediawiki_mariadb/tools/backup.d/dumpdb_mysql.sh index 8f808e1..6a0d586 100644 --- a/.recipes/mediawiki_mariadb/tools/backup.d/dumpdb_mysql.sh +++ b/.recipes/mediawiki_mariadb/tools/backup.d/dumpdb_mysql.sh @@ -1,28 +1,31 @@ #!/bin/bash # -# A service script to backup the docker-composed Mediawiki instance. -# Dumps the MySQL/MariaDB database to the $BASE_DIR/storage/backups/dumps -# folder (by default). An optional parameter may change the target folder. +# A service script to backup the docker-composed MySQL/MariaDB database. +# Dumps database to the $BASE_DIR/storage/backups/dumps folder (by default). +# An optional parameter may change the target folder. # -# This script gets the database credentials from MW's LocalSettings.php +# This script gets the database credentials from the docker-compose.yml file # and calls the mysql_dumpdb worker script which should be installed in # the same folder or somewhere in the path. # # Call as a Docker manager user (member of the docker Linux group) via cron. -# Uses the mysql_dumpdb utility which must be available on path. # -# Author: Kovács Zoltán +# Author: Kovács Zoltán +# Kovács Zoltán # License: GNU/GPL 3+ https://www.gnu.org/licenses/gpl-3.0.en.html +# 2025-02-26 v0.3 +# mod: doesn't tied to a particular composition (Mediawiki, Wordpress, etc). # 2024-12-01 v0.2.1 # fix: typo in docker-compose version detection. -# 2024-08-24 v0.2 +# 2024-08-25 v0.2 # new: docker-compose v2 compatibility - tested with Ubuntu 24.04 LTS. -# 2021-08-27 v0.1 Initial version. +# 2021-10-19 v0.1 Initial version. # Accepted environment variables and their defaults. # PAR_BASEDIR=${PAR_BASEDIR:-""} # Service's base folder PAR_DUMPDIR=${PAR_DUMPDIR:-""} # Folder to dump within +PAR_SERVICE=${PAR_SERVICE:-"database"} # Service's name in composition # Messages (maybe overridden by configuration). # @@ -33,11 +36,11 @@ MSG_MISSINGCONF="Fatal: missing config file" MSG_MISSINGYML="Fatal: didn't find the docker-compose.yml file" MSG_NONWRITE="The target directory isn't writable" MSG_NOLOCATE="Cannot locate the database container." -MSG_NOPARAM="Missing PHP parameter" +MSG_NOPARAM="Missing environment parameter" # Other initialisations. # -CONFFILE="configs/LocalSettings.php" # MW's configuration file +CONFFILE="docker-compose.yml" # Configuration file DUMPDIR="storage/backups/dumps" # Folder to dump within USER=${USER:-LOGNAME} # Fix for cron enviroment only YMLFILE="docker-compose.yml" @@ -112,12 +115,12 @@ DUMPDIR="${PAR_DUMPDIR:-$BASE_DIR/$DUMPDIR}" [[ ! -w "$DUMPDIR" ]] \ && echo "$MSG_NONWRITE: $DUMPDIR" >&2 && exit 1 -# The service must be running - silently gives up here if not. +# The composition must be running - silently gives up here if not. # -[[ -z "$(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring ps --services --filter "status=running")" ]] \ +[[ -z "$(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring ps --services --filter "status=running")" ]] \ && exit 1 -# Searches and parses the MW's LocalSettings.php file. +# Searches and parses the config file. # if [ ! -r "$CONFFILE" ]; then echo "$MSG_MISSINGCONF $CONFFILE" >&2; exit 1 @@ -126,31 +129,29 @@ fi function parse { [[ -z "$1" ]] && return # Gets the live lines containing the parameter. value=$("$CAT" "$CONFFILE" | "$GREP" -ve '^#' | \ - "$GREP" -e "^$1" | "$TR" -d '\r') + "$GREP" -e "$1" | "$TR" -d '\r') # If multiple the last one to consider. value=$(echo -e "$value" | "$TAIL" -n1) - # Right side of the equal sign W/O leading and trailing spaces and quotes. - value=$(echo -ne "$value" | "$CUT" -d'=' -f2 | "$XARGS") + # Right side of the colon W/O leading and trailing spaces and quotes. + value=$(echo -ne "$value" | "$CUT" -d':' -f2 | "$XARGS") # Removes the trailing semicolon (if any). value=${value%;*} echo -e "$value"; return } -# Gives up here silently if the type of the database isn't MySQL. -[[ "$(parse "\$wgDBtype")" != 'mysql' ]] && exit 1 # All parameters are mandatories. -MYCONTAINER="$(parse "\$wgDBserver")" -if [ -z "$MYCONTAINER" ]; then echo "$MSG_NOPARAM \$wgDBserver" >&2; exit 1; fi -MYDATABASE="$(parse "\$wgDBname")" -if [ -z "$MYDATABASE" ]; then echo "$MSG_NOPARAM \$wgDBname" >&2; exit 1; fi -MYUSER="$(parse "\$wgDBuser")" -if [ -z "$MYUSER" ]; then echo "$MSG_NOPARAM \$wgDBuser" >&2; exit 1; fi -MYPASSWORD="$(parse "\$wgDBpassword")" -if [ -z "$MYPASSWORD" ]; then echo "$MSG_NOPARAM \$wgDBpassword" >&2; exit 1; fi +MYCONTAINER="$PAR_SERVICE" # TODO: guess from the yml +if [ -z "$MYCONTAINER" ]; then echo "$MSG_NOPARAM PAR_SERVICE" >&2; exit 1; fi1; fi +MYDATABASE="$(parse "MYSQL_DATABASE")" +if [ -z "$MYDATABASE" ]; then echo "$MSG_NOPARAM MYSQL_DATABASE" >&2; exit 1; fi +MYUSER="$(parse "MYSQL_USER")" +if [ -z "$MYUSER" ]; then echo "$MSG_NOPARAM MYSQL_USER" >&2; exit 1; fi +MYPASSWORD="$(parse "MYSQL_PASSWORD")" +if [ -z "$MYPASSWORD" ]; then echo "$MSG_NOPARAM MYSQL_PASSWORD" >&2; exit 1; fi # We've the configuration parsed. # Converts the database service name to an actual running container's name. # -MYCONTAINER="$("$DOCKER" inspect -f '{{.Name}}' $(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandtring ps -q "$MYCONTAINER") | "$CUT" -c2-)" +MYCONTAINER="$("$DOCKER" inspect -f '{{.Name}}' $(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring ps -q "$MYCONTAINER") | "$CUT" -c2-)" # Gives up here if failed. if [ -z "$MYCONTAINER" ]; then echo "$MSG_NOLOCATE" >&2; exit 1; fi diff --git a/.recipes/mediawiki_mariadb/tools/restoredb_mysql.sh b/.recipes/mediawiki_mariadb/tools/restoredb_mysql.sh new file mode 100644 index 0000000..7232865 --- /dev/null +++ b/.recipes/mediawiki_mariadb/tools/restoredb_mysql.sh @@ -0,0 +1,195 @@ +#!/bin/bash +# +# Restores a composed MySQL/MariaDB database from a dump file. +# Gets all necessary data from the docker-compose.yml file. +# +# This is a wrapper script to the system-wide mysql_restoredb tool. +# Database recovey with the necessary user management and grants +# requires superuser privileges in MySQL, but simple data recovery +# is possible if the user and privileges are already set. +# +# You have to call this script as a Docker manager user (member of the +# 'docker' Linux group). The worker tool must be available somewhere +# in PATH. At least 5.7.6 MySQL or at least 10.1.3 MariaDB is required. +# +# Usage: +# $0 path_to_the_dumpfile [ path_to_the_service's_base ] +# +# Author: Kovács Zoltán +# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html) +# +# 2025-02-26 v0.1 Forked from the Smartfront repository and rewritten. + +# Accepted environment variables and their defaults. +# +PAR_SERVICE=${SERVICE:-"database"} # Database container's name + +# Other initialisations. +# +BACKUPFOLDER="storage/backups/dumps" # Skeleton's default dump folder +PROP_DBAPASS="MYSQL_ROOT_PASSWORD" # DB admin password property +PROP_DBNAME="MYSQL_DATABASE" # DB name property +PROP_DBPASS="MYSQL_PASSWORD" # DB password property +PROP_DBUSER="MYSQL_USER" # DB username property +USER=${USER:-LOGNAME} # Fix for cron enviroment only +YMLFILE="docker-compose.yml" + +# Basic environment settings. +# +LANG=C +LC_ALL=C + +# Messages. +# +MSG_BADDUMP="Fatal: doesn't exist or doesn't a dumpfile:" +MSG_DOCKERGRPNEED="You must be a member of the docker group." +MSG_DOESNOTRUN="This service doesn't run." +MSG_MISSINGDEP="Fatal: missing dependency" +MSG_MISSINGCONF="Fatal: missing config file" +MSG_MISSINGYML="Fatal: didn't find the $YMLFILE file" +MSG_NOLOCATE="Cannot locate the database container." +MSG_NOPARAM="Missing environment parameter" + +MSG_USAGE="Usage: $0 dump_pathname [ composition_base_pathname ]\n" +MSG_USAGE+="ENVVAR:\n" +MSG_USAGE+="SERVICE \tDatabase service's name in composition\n" + +# Checks the dependencies. +# +TR=$(which tr 2>/dev/null) +if [ -z "$TR" ]; then echo "$MSG_MISSINGDEP tr."; exit 1 ; fi +for item in basename cat cut date dirname docker \ + grep id mysql_restoredb readlink tail xargs +do + if [ -n "$(which $item)" ] + then export $(echo $item | "$TR" '[:lower:]' '[:upper:]' | "$TR" '-' '_')=$(which $item) + else echo "$MSG_MISSINGDEP $item." >&2; exit 1; fi +done +# All dependencies are available via "$THECOMMAND" (upper case) call. +# +# Let's find which version of docker-compose is installed. +if [ $($DOCKER compose version >/dev/null 2>&1; echo $?) -eq 0 ]; then + # We'll use v2 if it is available. + DOCKER_COMPOSE="$DOCKER" + commandstring="compose" +else + # Otherwise falling back to v1. + DOCKER_COMPOSE="$(which docker-compose)" + commandstring="" +fi +# One of the two is mandatory. +if [ -z "$DOCKER_COMPOSE" ];then echo "$MSG_MISSINGDEP docker-compose" >&2; exit 1; fi +# Below docker-compose should be called as "$DOCKER_COMPOSE" $commandstring sequence. + +# Where I'm? +# https://gist.github.com/TheMengzor/968e5ea87e99d9c41782 +SOURCE="$0" +while [ -h "$SOURCE" ]; do + # resolve $SOURCE until the file is no longer a symlink + SCRPATH="$( cd -P "$("$DIRNAME" "$SOURCE" )" && echo "$PWD" )" #" + SOURCE="$("$READLINK" "$SOURCE")" + # if $SOURCE was a relative symlink, we need to resolve it + # relative to the path where the symlink file was located + [[ $SOURCE != /* ]] && SOURCE="$SCRPATH/$SOURCE" +done; SCRPATH="$( cd -P "$("$DIRNAME" "$SOURCE" )" && echo "$PWD" )" #" + +# Need to be root or a Docker manager user. +# +[[ "$USER" != 'root' ]] \ +&& [[ -z "$(echo "$("$ID" -Gn "$USER") " | "$GREP" ' docker ')" ]] \ +&& echo "$MSG_DOCKERGRPNEED" >&2 && exit 1 #" + +# Gets the command line parameters. +# +# DUMPFILE is mandatory +if [ -n "$1" ]; then DUMPFILE="$1"; shift +else echo -e "$MSG_USAGE" >&2; exit 1; fi +# SERVICE_BASE is optional +if [ -n "$1" ]; then SERVICE_BASE="$1"; shift; fi +# We've read the unchecked command line parameters. + +# Searches the base folder, containing the YMLFILE. +# +if [ -z "$SERVICE_BASE" ]; then + # Called from the base folder (./)? + TEST_DIR="$SCRPATH" + [[ -z "$SERVICE_BASE" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && SERVICE_BASE="$TEST_DIR" + # Called from ./tools? + TEST_DIR="$("$DIRNAME" "$TEST_DIR")" + [[ -z "$SERVICE_BASE" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && SERVICE_BASE="$TEST_DIR" + # Called from ./tools/*.d? + TEST_DIR="$("$DIRNAME" "$TEST_DIR")" + [[ -z "$SERVICE_BASE" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && SERVICE_BASE="$TEST_DIR" +fi +# On failure gives it up here. +if [ -z "$SERVICE_BASE" -o ! -r "$SERVICE_BASE/$YMLFILE" ]; then + echo "$MSG_MISSINGYML" >&2; exit 1 +fi +# Sets the absolute path. +YMLFILE="$SERVICE_BASE/$YMLFILE" +# We've the YMLFILE. + +# Finds the DUMPFILE to use. +# +# The DUMPFILE must point to a readable file. +# If doesn't it tries the skeleton's standard backup folder as well. +if [ ! -r "$DUMPFILE" ] +then DUMPFILE="$("$DIRNAME" "$SERVICE_BASE")/$BACKUPFOLDER/$DUMPFILE"; fi +# If it is an existing symlink dereferences it to ensure, it points to a file. +if [ -h "$DUMPFILE" ]; then + if [[ "$("$READLINK" "$DUMPFILE")" != /* ]] + # relative path in symlink + then DUMPFILE="$("$DIRNAME" "$DUMPFILE")/$("$READLINK" "$DUMPFILE")" + # absolute path in symlink + else DUMPFILE="$("$READLINK" "$DUMPFILE")"; fi +fi +# Let's check it! +if [ ! -r "$DUMPFILE" -o ! -f "$DUMPFILE" ] +then echo -e "$MSG_BADDUMP $DUMPFILE"; exit 1; fi +# We've an existing dumpfile. + +# The composition must be running - silently gives up here if not. +# +[[ -z "$(cd "$SERVICE_BASE"; "$DOCKER_COMPOSE" $commandstring ps --services --filter "status=running")" ]] \ +&& exit 1 + +# Parses the YMLFILE for parameters to use. +# +function parse { [[ -z "$1" ]] && return + # Gets the live lines containing the parameter. + value=$("$CAT" "$YMLFILE" | "$GREP" -ve '^#' | \ + "$GREP" -e "^ *$1" | "$TR" -d '\r') + # If multiple the last one to consider. + value=$(echo -e "$value" | "$TAIL" -n1) + # Right side of the colon W/O leading and trailing spaces and quotes. + value=$(echo -ne "$value" | "$CUT" -d':' -f2 | "$XARGS") + # Removes the trailing semicolon (if any). + value=${value%;*} + echo -e "$value"; return +} +# These parameters are mandatory. +MYCONTAINER="$PAR_SERVICE" # TODO: guess from the yml +if [ -z "$MYCONTAINER" ]; then echo "$MSG_NOPARAM PAR_SERVICE" >&2; exit 1; fi1; fi +MYDATABASE="$(parse "$PROP_DBNAME")" +if [ -z "$MYDATABASE" ]; then echo "$MSG_NOPARAM $PROP_DBNAME" >&2; exit 1; fi +MYUSER="$(parse "$PROP_DBUSER")" +if [ -z "$MYUSER" ]; then echo "$MSG_NOPARAM $PROP_DBUSER" >&2; exit 1; fi +MYPASSWORD="$(parse "$PROP_DBPASS")" +if [ -z "$MYPASSWORD" ]; then echo "$MSG_NOPARAM $PROP_DBPASS" >&2; exit 1; fi +# These are optional. +MYDBAUSER="root" +MYDBAPASSWORD="$(parse "$PROP_DBAPASS")" +# We've the configuration parsed. + +# Converts the database service name to an actual running container's name. +# +MYCONTAINER="$("$DOCKER" inspect -f '{{.Name}}' $(cd "$SERVICE_BASE"; "$DOCKER_COMPOSE" $commandstring ps -q "$MYCONTAINER") | "$CUT" -c2-)" +# Gives up here if failed. +if [ -z "$MYCONTAINER" ]; then echo "$MSG_NOLOCATE" >&2; exit 1; fi + +# Calls the worker script to make the job. +# +export MYDBAUSER MYDBAPASSWORD MYPASSWORD +"$MYSQL_RESTOREDB" -C "$MYCONTAINER" -U "$MYUSER" "$MYDATABASE" "$DUMPFILE" + +# That's all, Folks! :)