2
0

Mediawiki_mariadb recipe improvements.

* Unified dumpdb_mysql.sh script.
* Added the restoredb_mysql wrapper script.
This commit is contained in:
2025-03-05 19:46:08 +01:00
parent de8bfe5137
commit 7e89d1da9e
4 changed files with 225 additions and 31 deletions

View File

@ -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 <kovacs.zoltan@smartfront.hu>
# Author: Kovács Zoltán <kovacsz@marcusconsulting.hu>
# Kovács Zoltán <kovacs.zoltan@smartfront.hu>
# 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