2
0

Wordpress_mariadb recipe improvements.

* Unified dumpdb_mysql.sh script.
* Added the restoredb_mysql wrapper script.
* Minimal changes in storage_backup script.
This commit is contained in:
Kovács Zoltán 2025-03-05 20:07:28 +01:00
parent 7e89d1da9e
commit aaf493f13c
5 changed files with 220 additions and 20 deletions

BIN
.metadata

Binary file not shown.

View File

@ -1,6 +1,5 @@
# Wordpress with MariaDB
#
#version: '3'
services:
# https://hub.docker.com/_/wordpress
# https://github.com/docker-library/docs/tree/master/wordpress

View File

@ -1,8 +1,8 @@
#!/bin/bash
#
# A service script to backup the docker-composed WordPress 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 the docker-compose.yml file
# and calls the mysql_dumpdb worker script which should be installed in
@ -10,8 +10,11 @@
#
# Call as a Docker manager user (member of the docker Linux group) via cron.
#
# 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-25 v0.2
@ -22,6 +25,7 @@
#
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).
#
@ -111,7 +115,7 @@ 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")" ]] \
&& exit 1
@ -135,14 +139,14 @@ function parse { [[ -z "$1" ]] && return
echo -e "$value"; return
}
# All parameters are mandatories.
MYCONTAINER="$(parse "WORDPRESS_DB_HOST")"
if [ -z "$MYCONTAINER" ]; then echo "$MSG_NOPARAM WORDPRESS_DB_HOST" >&2; exit 1; fi
MYDATABASE="$(parse "WORDPRESS_DB_NAME")"
if [ -z "$MYDATABASE" ]; then echo "$MSG_NOPARAM WORDPRESS_DB_NAME" >&2; exit 1; fi
MYUSER="$(parse "WORDPRESS_DB_USER")"
if [ -z "$MYUSER" ]; then echo "$MSG_NOPARAM WORDPRESS_DB_USER" >&2; exit 1; fi
MYPASSWORD="$(parse "WORDPRESS_DB_PASSWORD")"
if [ -z "$MYPASSWORD" ]; then echo "$MSG_NOPARAM WORDPRESS_DB_PASSWORD" >&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.

View File

@ -9,6 +9,8 @@
#
# Author: Kovács Zoltán <kovacs.zoltan@smartfront.hu>
# License: GNU/GPL 3+ https://www.gnu.org/licenses/gpl-3.0.en.html
# 2025-03-05 v0.2.1
# mod: reworded some comments and renamed a variable.
# 2024-08-25 v0.2
# new: docker-compose v2 compatibility - tested with Ubuntu 24.04 LTS.
# 2021-10-19 v0.1 Initial version.
@ -25,7 +27,7 @@ MSG_DOESNOTRUN="This service doesn't run."
MSG_MISSINGDEP="Fatal: missing dependency"
MSG_MISSINGYML="Fatal: didn't find the docker-compose.yml file"
MSG_NONWRITE="The target directory isn't writable"
MSG_NOLOCATE="Cannot locate the Mediawiki container."
MSG_NOLOCATE="Cannot locate the service container."
# Other initialisations.
#
@ -108,16 +110,16 @@ BACKUPDIR="${PAR_BACKUPDIR:-$BASE_DIR/$BACKUPDIR}"
[[ -z "$(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring ps --services --filter "status=running")" ]] \
&& exit 1
# Converts the WordPress service name to an actual running container's name.
# Converts the service name to an actual running container's name.
#
WPCONTAINER="$("$DOCKER" inspect -f '{{.Name}}' $(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandline ps -q "$SERVICENAME") | "$CUT" -c2-)"
MYCONTAINER="$("$DOCKER" inspect -f '{{.Name}}' $(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandline ps -q "$SERVICENAME") | "$CUT" -c2-)"
# Gives up here if failed.
if [ -z "$WPCONTAINER" ]; then echo "$MSG_NOLOCATE" >&2; exit 1; fi
if [ -z "$MYCONTAINER" ]; then echo "$MSG_NOLOCATE" >&2; exit 1; fi
# Tries the FS backup.
if [ -w "$BACKUPDIR" ]; then
BACKUP_NAME=$WPCONTAINER.$("$DATE" '+%Y%m%d_%H%M%S').$("$HOSTNAME")
"$DOCKER" exec $WPCONTAINER sh \
BACKUP_NAME=$MYCONTAINER.$("$DATE" '+%Y%m%d_%H%M%S').$("$HOSTNAME")
"$DOCKER" exec $MYCONTAINER sh \
-c "cd /var/www/html; tar cz ." \
> "$BACKUPDIR/$BACKUP_NAME.tgz" 2>>"$BACKUPDIR/$BACKUP_NAME.log"
fi

View File

@ -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 <kovacsz@marcusconsulting.hu>
# 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! :)