#!/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! :)