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:
2025-03-05 20:07:28 +01:00
parent 7e89d1da9e
commit aaf493f13c
5 changed files with 220 additions and 20 deletions

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