175 lines
		
	
	
		
			6.6 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
			
		
		
	
	
			175 lines
		
	
	
		
			6.6 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
#!/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.
 | 
						|
#
 | 
						|
# This script gets the database credentials from MW's LocalSettings.php
 | 
						|
# 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>
 | 
						|
# License: GNU/GPL 3+ https://www.gnu.org/licenses/gpl-3.0.en.html
 | 
						|
# 2024-08-24 v0.2
 | 
						|
# new: docker-compose v2 compatibility - tested with Ubuntu 24.04 LTS.
 | 
						|
# 2021-08-27 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
 | 
						|
 | 
						|
# Messages (maybe overridden by configuration).
 | 
						|
#
 | 
						|
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 docker-compose.yml file"
 | 
						|
MSG_NONWRITE="The target directory isn't writable"
 | 
						|
MSG_NOLOCATE="Cannot locate the database container."
 | 
						|
MSG_NOPARAM="Missing PHP parameter"
 | 
						|
 | 
						|
# Other initialisations.
 | 
						|
#
 | 
						|
CONFFILE="configs/LocalSettings.php"		# MW's configuration file
 | 
						|
DUMPDIR="storage/backups/dumps"			# Folder to dump within
 | 
						|
USER=${USER:-LOGNAME}                           # Fix for cron enviroment only
 | 
						|
YMLFILE="docker-compose.yml"
 | 
						|
 | 
						|
# 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 \
 | 
						|
            find grep hostname id pwd 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 2>&1 >/dev/null; 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 #"
 | 
						|
 | 
						|
# Searches the base folder, containing a docker-compose.yml file.
 | 
						|
#
 | 
						|
# Called from the base folder (./)?
 | 
						|
BASE_DIR="$PAR_BASEDIR"
 | 
						|
TEST_DIR="$SCRPATH"
 | 
						|
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
 | 
						|
# Called from ./tools?
 | 
						|
TEST_DIR="$("$DIRNAME" "$TEST_DIR")"
 | 
						|
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
 | 
						|
# Called from ./tools/*.d?
 | 
						|
TEST_DIR="$("$DIRNAME" "$TEST_DIR")"
 | 
						|
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
 | 
						|
# On failure gives it up here.
 | 
						|
if [ -z "$BASE_DIR" -o ! -r "$BASE_DIR/$YMLFILE" ]; then
 | 
						|
    echo "$MSG_MISSINGYML" >&2; exit 1
 | 
						|
fi
 | 
						|
# Sets the absolute paths.
 | 
						|
CONFFILE="$BASE_DIR/$CONFFILE"
 | 
						|
DUMPDIR="${PAR_DUMPDIR:-$BASE_DIR/$DUMPDIR}"
 | 
						|
 | 
						|
# The dump target folder must be writable.
 | 
						|
#
 | 
						|
[[ ! -w "$DUMPDIR" ]] \
 | 
						|
&& echo "$MSG_NONWRITE: $DUMPDIR" >&2 && exit 1
 | 
						|
 | 
						|
# The service must be running - silently gives up here if not.
 | 
						|
#
 | 
						|
[[ -z "$(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring  ps --services --filter "status=running")" ]] \
 | 
						|
&& exit 1
 | 
						|
 | 
						|
# Searches and parses the MW's LocalSettings.php file.
 | 
						|
#
 | 
						|
if [ ! -r "$CONFFILE" ]; then
 | 
						|
    echo "$MSG_MISSINGCONF $CONFFILE" >&2; exit 1
 | 
						|
fi
 | 
						|
#
 | 
						|
function parse { [[ -z "$1" ]] && return
 | 
						|
    # Gets the live lines containing the parameter.
 | 
						|
    value=$("$CAT" "$CONFFILE" | "$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 equal sign 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
 | 
						|
# 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-)"
 | 
						|
# Gives up here if failed.
 | 
						|
if [ -z "$MYCONTAINER" ]; then echo "$MSG_NOLOCATE" >&2; exit 1; fi
 | 
						|
 | 
						|
# Locates the worker script (in PATH or in this script's folder).
 | 
						|
#
 | 
						|
MYSQL_DUMPDB=$(which mysql_dumpdb)
 | 
						|
if [ -z "$MYSQL_DUMPDB" -a -x "$("$DIRNAME" "$0")/mysql_dumpdb" ]; then
 | 
						|
    MYSQL_DUMPDB="$SCRPATH/mysql_dumpdb"
 | 
						|
fi
 | 
						|
if [ -z "$MYSQL_DUMPDB" ]; then echo "$MSG_MISSINGDEP mysql_dumpdb."; exit 1 ; fi
 | 
						|
 | 
						|
# Tries the DB backup.
 | 
						|
#
 | 
						|
if [ -n "$MYSQL_DUMPDB" -a -w "$DUMPDIR" ]; then
 | 
						|
    BACKUP_NAME=$MYDATABASE.$("$DATE" '+%Y%m%d_%H%M%S').$("$HOSTNAME")
 | 
						|
    ( cd "$DUMPDIR"
 | 
						|
      export MYCONTAINER MYUSER MYPASSWORD
 | 
						|
      "$MYSQL_DUMPDB" --compress "$MYDATABASE" "$DUMPDIR/$BACKUP_NAME.sql" \
 | 
						|
                      2>>"$DUMPDIR/$BACKUP_NAME.log"
 | 
						|
    )
 | 
						|
fi
 | 
						|
 | 
						|
# That's all, Folks! :)
 |