#!/usr/bin/env bash CUR_IFS="$IFS" script_name="$(basename $(realpath $0))" working_dir="$(dirname $(realpath $0))" conf_file="${working_dir}/conf/${script_name%%.*}.conf" log_file="$(mktemp)" backup_date=$(date +%Y-%m-%d-%H%M) # ============= # --- Some Variable # ============= # - Give your default values here # - right_tabstop=75 DATABASE_SERVER="" DATABASE_SERVER_NEEDED=true DATABASE_NAME="" DATABASE_NAME_NEEDED=true DATABASE_CREDENTIALS_ARGS="" _DATABASE_CREDENTIALS_ARGS="" DATABASE_CREDENTIALS_NEEDED=true DATABASE_DUMP_FILE="" DATABASE_DUMP_FILE_NEEDED=true BATCH_MODE=false QUIET_MODE=false NON_INTERACTIVE_MODE=false # ============= # --- Some functions # ============= usage() { [[ -n "$1" ]] && error "$1" [[ $terminal ]] && echo -e " \033[1mUsage:\033[m $(basename $0) [OPTION [OPTION .. \033[1mDescription\033[m Script restores a database from a database dump file. \033[1mNotice\033[m This script was created to restore large databases. The recovery of such databases often takes a lot of time (often several hours). For a more performant but above all faster recovery, global variables are changed for the recovery time. This requires extensive rights on the database server. Please take this into account when specifying the MySQL login information. \033[1mOptions\033[m -b Sets batch mode to true - no interaction will be made. -c A string containing credentials to establish a database connection. This character string can consist of username and password, but also of login information stored in the file system or the database itself. Example: -c "u root -p ''" -c "--login-path=local" -c "--login-path=mysql-5.7" -c "--defaults-file=/usr/local/mysql/sys-maint.cnf" -c "-u root -S /run/mysqld/mysqld.sock" If this Parameter is omitted, the script tries to detect the right credentials. -d The name of the database to restore. -f Full qualified Path Name (FQPN) to the Dump file from which the database is to be restored. -h Prints this help. -s The database server to restore the database to. It is possible to enter an IP address or a host name. \033[1mFiles\033[m Configuration file: $conf_file \033[1mExample:\033[m Restore database 'cloud_fm' from file '/backup/cl-fm.oopen.de/MariaDB/cloud_fm/cloud_fm-2022-07-04-0006.sql'. Run in batch mode. \033[1m$(basename $0) -b -d cloud_01 -c "-u root -S /run/mysqld/mysqld.sock"\033[m " clean_up 1 } clean_up() { if [[ -f "$_backup_crontab_file" ]]; then blank_line #echononl " (Re)Install Crontab from previously saved crontab file '$_backup_crontab_file'.." echononl " (Re)Install Crontab from previously saved crontab file ..." crontab $_backup_crontab_file >> $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed error "$(cat $log_file)" fi fi if [[ -n "$CUR_AUTOCOMMIT" ]] ; then ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='$CUR_AUTOCOMMIT'" > /dev/null 2>&1 fi if [[ -n "$CUR_FOREIGN_KEY_CHECKS" ]] ; then ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='$CUR_FOREIGN_KEY_CHECKS'" > /dev/null 2>&1 fi if [[ -n "$CUR_UNIQUE_CHECKS" ]] ; then ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='$CUR_UNIQUE_CHECKS'" > /dev/null 2>&1 fi if [[ -n "$CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT" ]] ; then ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='$CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT'" > /dev/null 2>&1 fi # Perform program exit housekeeping rm -f $log_file blank_line exit $1 } is_number() { return $(test ! -z "${1##*[!0-9]*}" > /dev/null 2>&1); # - also possible # - #[[ ! -z "${1##*[!0-9]*}" ]] && return 0 || return 1 #return $([[ ! -z "${1##*[!0-9]*}" ]]) } echononl(){ if $terminal && ! $QUIET_MODE ; then echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n "$*\\c" 1>&2 else echo -e -n "$*" 1>&2 fi rm /tmp/shprompt$$ fi } echo_done() { if $terminal && ! $QUIET_MODE ; then echo -e "\033[${right_tabstop}G[ \033[32mdone\033[m ]" fi } echo_ok() { if $terminal && ! $QUIET_MODE ; then echo -e "\033[${right_tabstop}G[ \033[32mok\033[m ]" fi } echo_warning() { if $terminal && ! $QUIET_MODE ; then echo -e "\033[${right_tabstop}G[ \033[33m\033[1mwarn\033[m ]" fi } echo_failed(){ if $terminal && ! $QUIET_MODE ; then echo -e "\033[${right_tabstop}G[ \033[1;31mfailed\033[m ]" fi } echo_skipped() { if $terminal && ! $QUIET_MODE; then echo -e "\033[${right_tabstop}G[ \033[37mskipped\033[m ]" fi } fatal (){ blank_line if $terminal ; then echo -e "\t[ \033[31m\033[1mFatal\033[m ]: \033[37m\033[1m$*\033[m" echo "" echo -e "\t\033[31m\033[1m Script will be interrupted..\033[m\033[m" else echo "fatal: $*" echo "Script will be interrupted.." fi clean_up 1 } error(){ blank_line if $terminal ; then echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*" else echo "Error: $*" fi blank_line } warn (){ blank_line if $terminal && ! $QUIET_MODE ; then echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" else echo "Warning: $*" fi blank_line } info (){ if $terminal && ! $QUIET_MODE ; then echo "" echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*" echo "" fi } time_duration () { local __begin=$1 local __end=$2 local __duration if [[ "$__begin" ]] && [[ "$__end" ]] ; then local __duration local __time=`expr $__end - $__begin` local __t_h=`expr $__time / 60 / 60` local __t_rest_h=`expr $__time - $__t_h \\* 60 \\* 60` local __t_m=`expr $__t_rest_h / 60` local __t_s=`expr $__t_rest_h - $__t_m \\* 60` if [[ $__t_h -gt 0 ]]; then if [[ $__t_h -lt 10 ]] ; then __t_h=" $__t_h" fi if [[ $__t_m -lt 10 ]] ; then __t_m=" $__t_m" fi if [[ $__t_s -lt 10 ]] ; then __t_s=" $__t_s" fi echo "$__t_h h : $__t_m min : $__t_s sec" elif [[ $__t_m -gt 0 ]]; then if [[ $__t_m -lt 10 ]] ; then __t_m=" $__t_m" fi if [[ $__t_s -lt 10 ]] ; then __t_s=" $__t_s" fi echo "$__t_m min : $__t_s sec" else if [[ $__t_s -lt 10 ]] ; then echo " $__t_s sec" else echo "$__t_s sec" fi fi else echo "N/A" fi } echo_duration () { local __time_duration=${1} local __length=${#__time_duration} # print duratuion inside inside square brackets "[ duration ]" with blank delimiter, # so add reduce tabstop by 4 characters # __tabstop=`expr ${right_tabstop} - ${__length} - 1 - 4` echo -en "\033[${__tabstop}G[ \033[33m${__time_duration}\033[m ]" } detect_mysql_version () { _MYSQLD_VERSION="$(mysqld -V 2>/dev/null)" if [[ -z "$_MYSQLD_VERSION" ]]; then fatal "No installed MySQL server or distribution found!" elif [[ -d "/usr/local/mysql" ]] && [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ percona- ]]; then MYSQL_CUR_DISTRIBUTION="Percona" elif [[ "$_MYSQLD_VERSION" =~ MariaDB ]]; then MYSQL_CUR_DISTRIBUTION="MariaDB" elif [[ "$_MYSQLD_VERSION" =~ MySQL ]]; then MYSQL_CUR_DISTRIBUTION="MySQL" elif [[ -d "/usr/local/mysql" ]] && [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ mysql- ]]; then MYSQL_CUR_DISTRIBUTION="MySQL" elif [[ -d "/usr/local/mysql" ]] && [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ mariadb- ]]; then MYSQL_CUR_DISTRIBUTION="MariaDB" else if $BATCH_MODE ; then fatal "No MySQL Instalation on this Server found!" fi error "MySQL Instalation found, but cannot determin the distribution!" MYSQL_CUR_DISTRIBUTION= echo "" echo " Select the MySQL distribution to install." echo "" echo " [1] MySQL (the original community edition)" echo " [2] Percona Server for MySQL" echo " [3] MariaDB" echo "" echononl " Eingabe [1/2/3]: " while [ "$MYSQL_CUR_DISTRIBUTION" != "MySQL" -a "$MYSQL_CUR_DISTRIBUTION" != "MariaDB" -a "$MYSQL_CUR_DISTRIBUTION" != "Percona" ];do read OPTION case $OPTION in 1) MYSQL_CUR_DISTRIBUTION="MySQL" ;; 2) MYSQL_CUR_DISTRIBUTION="Percona" ;; 3) MYSQL_CUR_DISTRIBUTION="MariaDB" ;; *) echo "" echo -e "\tFalsche Eingabe ! [ 1 = MySQL ; 2 = Percona ; 3 = MariaDB ]" echo "" echononl " Eingabe:" ;; esac done fi MYSQL_VERSION="$(echo $_MYSQLD_VERSION | grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+)?" | head -n 1)" MYSQL_MAJOR_VERSION="$(echo $MYSQL_VERSION | cut -d '.' -f1)" MYSQL_MINOR_VERSION="$(echo $MYSQL_VERSION | cut -d '.' -f2)" MYSQL_PATCH_LEVEL="$(echo $MYSQL_VERSION | cut -d '.' -f3)" MYSQL_MAIN_VERSION="$(echo $MYSQL_VERSION | cut -d '.' -f1,2)" } # Check if systemd service exists # service_exists() { local n=$1 if [[ $(systemctl list-units --all -t service --full --no-legend "$n.service" | sed 's/^\s*//g' | cut -f1 -d' ') == $n.service ]]; then return 0 else return 1 fi } # Check if a given array (parameter 2) contains a given string (parameter 1) # containsElement () { local e for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done return 1 } trim() { local var="$*" var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters echo -n "$var" } blank_line() { if $terminal ; then echo "" fi } # ---------- # - Jobhandling # ---------- # - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM # - trap clean_up SIGHUP SIGINT SIGTERM # ============= # --- Some # ============= # - Support systemd ? # - if [[ "X$(which systemd)" = "X" ]]; then SYSTEMD_EXISTS=false else SYSTEMD_EXISTS=true fi # - Running in a terminal? # - if [[ -t 1 ]] ; then terminal=true clear else terminal=false QUIET_MODE=true NON_INTERACTIVE_MODE=true fi echononl " Get MySQL command.." mysql_command="$(which mysql)" if [[ $? -eq 0 ]]; then echo_ok else if [[ -x "/usr/local/mysql/bin/mysql" ]]; then mysql_command="/usr/local/mysql/bin/mysql" echo_ok else echo_failed fatal "$(cat $tmp_log_file)" fi fi # ---------- # Read Configurations from $conf_file # ---------- if ! $QUIET_MODE ; then echo "" fi echononl " Loading configuration settings from $(basename ${conf_file}).." if [[ -f "$conf_file" ]]; then source "$conf_file" > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_done else echo_failed fatal "$(cat $log_file)" fi else echo_skipped warn "No Configuration File found. Loading defaults.." fi # ---------- # - Read commandline parameter # ---------- echononl " Read Commandline Parameter.." while getopts bd:f:hs: opt ; do case $opt in b) BATCH_MODE=true NON_INTERACTIVE_MODE=true ;; c) DATABASE_CREDENTIALS_ARGS="$OPTARG" ;; d) DATABASE_NAME="$OPTARG" ;; f) DATABASE_DUMP_FILE="$OPTARG" ;; h) usage ;; s) DATABASE_SERVER="$OPTARG" ;; \?) usage ;; esac done echo_done [[ -n "$DATABASE_CREDENTIALS_ARGS" ]] && DATABASE_CREDENTIALS_NEEDED=false [[ -n "$DATABASE_SERVER" ]] && DATABASE_SERVER_NEEDED=false [[ -n "$DATABASE_NAME" ]] && DATABASE_NAME_NEEDED=false [[ -n "$DATABASE_DUMP_FILE" ]] && DATABASE_DUMP_FILE_NEEDED=false if $BATCH_MODE ; then if [[ -z "$DATABASE_DUMP_FILE" ]] ; then fatal "No SQL Dump File given \033[m(Option -f)\033[1m!" elif [[ ! -f "$DATABASE_DUMP_FILE" ]] ; then fatal "\033[mSQL Dump File \033[1m${DATABASE_DUMP_FILE}\033[m not found!" fi if [[ -z "$DATABASE_NAME" ]] ; then fatal "No Database given \033[m(Option -d)\033[1m!" fi # if [[ -z "$DATABASE_CREDENTIALS_ARGS" ]] && [[ -z "$DATABASE_SERVER" ]]; then # # # Try to detect local MySQL Installation # # # detect_mysql_version # # MAJOR_VERSION="$MYSQL_MAJOR_VERSION" # MINOR_VERSION="$MYSQL_MINOR_VERSION" # PATCH_LEVEL="$MYSQL_PATCH_LEVEL" # # if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \ # || ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then # if [[ -S "/tmp/mysql.sock" ]]; then # DATABASE_CREDENTIALS_ARGS="-u root -S /tmp/mysql.sock" # elif [[ -S "/run/mysqld/mysqld.sock" ]]; then # DATABASE_CREDENTIALS_ARGS="-u root -S /run/mysqld/mysqld.sock" # elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then # DATABASE_CREDENTIALS_ARGS="-u root -S /var/run/mysqld/mysqld.sock" # else # fatal "Parameter 'DATABASE_CREDENTIALS_ARGS' cannot be determined automated. # # Use configuration file "$conf_file" or commandline Parameter or set # thr mysql credentials." # fi # else # if $(${mysql_command} --login-path=local -e ";" > /dev/null 2>&1) ; then # DATABASE_CREDENTIALS_ARGS="--login-path=local" # elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then # DATABASE_CREDENTIALS_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" # elif [[ -f "/etc/mysql/debian.cnf" ]] ; then # DATABASE_CREDENTIALS_ARGS="--defaults-file=/etc/mysql/debian.cnf" # else # fatal "Parameter 'DATABASE_CREDENTIALS_ARGS' cannot be determined automated. # # Use configuration file "$conf_file" to set # parameter manually." # fi # fi # fi if [[ -z "$DATABASE_CREDENTIALS_ARGS" ]] && [[ -n "$DATABASE_SERVER" ]]; then fatal "Cannot detect database credentials on remote machines. You have to set Parameter '-c'" fi else if $DATABASE_SERVER ; then echo "" echo -e "\033[32m--\033[m" echo "" echo "Insert Database Server for which we should restore a database.." echo "" echo "" echo -e " \033[33mType to accept the default (localhost).\033[m" echo "" echononl "Database Server [localhost]: " read DATABASE_SERVER if [[ "X$DATABASE_SERVER" = "X" ]] \ || [[ "$DATABASE_SERVER" = 'localhost' ]] \ || [[ "$DATABASE_SERVER" =~ ^127\. ]]; then DATABASE_SERVER="" fi fi if $DATABASE_NAME_NEEDED ; then echo "" echo -e "\033[32m--\033[m" echo "" echo "Insert Database name which should be created.." echo "" echo "" echononl "Database name: " read DATABASE_NAME while [ "X$DATABASE_NAME" = "X" ] ; do echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n" echononl "Database name: " read DATABASE_NAME done fi if $DATABASE_CREDENTIALS_NEEDED ; then echo "" echo -e "\033[32m--\033[m" echo "" echo "Insert Database Credentials.." echo "" echo -e "A string containing credentials to establish a database connection. This character string can consist of username and password, but also of login information stored in the file system or the database itself. Example: \033[33m-u root -p''\033[m \033[33m--login-path=local\033[m \033[33m--login-path=mysql-5.7\033[m \033[33m--defaults-file=/usr/local/mysql/sys-maint.cnf\033[m \033[33m-u root -S /run/mysqld/mysqld.sock\033[m" echo "" if [[ -n "$_DATABASE_CREDENTIALS_ARGS" ]] ; then echo -e " Type to accept the default (${_DATABASE_CREDENTIALS_ARGS})." echo "" echononl "Database Credentials [\033[1m$_DATABASE_CREDENTIALS_ARGS\033[m]: " read DATABASE_CREDENTIALS_ARGS if [[ "X${DATABASE_CREDENTIALS_ARGS}" = "X" ]]; then DATABASE_CREDENTIALS_ARGS="${_DATABASE_CREDENTIALS_ARGS}" fi else echononl "Database Credentials: " read DATABASE_CREDENTIALS_ARGS while [ "X$DATABASE_CREDENTIALS_ARGS" = "X" ] ; do echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n" echononl "Database Credentials: " read DATABASE_CREDENTIALS_ARGS done fi # if [[ -n "$_DATABASE_CREDENTIALS_ARGS" ]] fi # if $DATABASE_CREDENTIALS_NEEDED ; then if $DATABASE_DUMP_FILE_NEEDED ; then echo "" echo -e "\033[32m--\033[m" echo "" echo "Give full qualified path name (FQPN) of MySQL Dump file." echo "" echo "" while [ "X$DATABASE_DUMP_FILE" = "X" ] ; do echononl "FQPN to MySQL Dump File: " read DATABASE_DUMP_FILE if [[ "X$DATABASE_DUMP_FILE" = "X" ]] ; then echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n" continue fi if [[ ! -f "$DATABASE_DUMP_FILE" ]] ; then echo -e "\n\t\033[33m\033[1mMySQL Dump File '${DATABASE_DUMP_FILE}' not found!.\033[m\n" DATABASE_DUMP_FILE="" fi done : fi fi # if $BATCH_MODE ; then echo "" echo -e "\033[32m--\033[m" echo "" echononl "Check connection to Database Server.." if [[ -n "$DATABASE_SERVER" ]] ; then if ! $(${mysql_command} -h $DATABASE_SERVER $DATABASE_CREDENTIALS_ARGS -e ";" > /dev/null 2> $log_file) ; then if [[ "$(cat $log_file)" =~ "unknown variable 'login-path" ]] ; then if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then mysql_command="/usr/local/mysql/bin/mysql" else fatal "$(cat $log_file)" fi else fatal "$(cat $log_file)" fi fi else if ! $(${mysql_command} $DATABASE_CREDENTIALS_ARGS -e ";" > /dev/null 2> $log_file) ; then if [[ "$(cat $log_file)" =~ "unknown variable 'login-path" ]] ; then if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then mysql_command="/usr/local/mysql/bin/mysql" else fatal "$(cat $log_file)" fi else fatal "$(cat $log_file)" fi fi fi if [[ -n "$DATABASE_CREDENTIALS_ARGS" ]] ; then # - Get MySQL Version # - echo "" echo -e "\033[32m--\033[m" echo "" echononl "Get MySQL Version" _version="$(${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SELECT VERSION()" 2> $log_file)" if [[ $? -ne 0 ]] ; then echo_failed fatal "$(cat $log_file)" else echo_ok fi echo "" echo "_version: $_version" echo "" echo "" echo "" IFS='.' read -r -a version_arr <<< "$_version" declare -i MAJOR_VERSION="${version_arr[0]}" declare -i MINOR_VERSION="${version_arr[1]}" _path_level="${version_arr[2]}" declare -i PATCH_LEVEL="${_path_level%%-*}" ## - Get current MySQL Distribution ## - echononl " Get current MySQL distribution .." if [[ -z "$_version" ]]; then echo_failed fatal "No installed MySQL server or distribution found!" elif [[ "$_version" =~ MariaDB ]]; then MYSQL_CUR_DISTRIBUTION="MariaDB" else MYSQL_CUR_DISTRIBUTION="MySQL" fi echo_ok echo "" echo -e "\033[32m--\033[m" echo "" else # Try to detect local MySQL Installation # detect_mysql_version MAJOR_VERSION="$MYSQL_MAJOR_VERSION" MINOR_VERSION="$MYSQL_MINOR_VERSION" PATCH_LEVEL="$MYSQL_PATCH_LEVEL" if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \ || ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then if [[ -S "/tmp/mysql.sock" ]]; then DATABASE_CREDENTIALS_ARGS="-u root -S /tmp/mysql.sock" elif [[ -S "/run/mysqld/mysqld.sock" ]]; then DATABASE_CREDENTIALS_ARGS="-u root -S /run/mysqld/mysqld.sock" elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then DATABASE_CREDENTIALS_ARGS="-u root -S /var/run/mysqld/mysqld.sock" else fatal "Parameter 'DATABASE_CREDENTIALS_ARGS' cannot be determined automated. Use configuration file "$conf_file" or commandline Parameter or set thr mysql credentials." fi else if $(${mysql_command} --login-path=local -e ";" > /dev/null 2>&1) ; then DATABASE_CREDENTIALS_ARGS="--login-path=local" elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then DATABASE_CREDENTIALS_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" elif [[ -f "/etc/mysql/debian.cnf" ]] ; then DATABASE_CREDENTIALS_ARGS="--defaults-file=/etc/mysql/debian.cnf" else fatal "Parameter 'DATABASE_CREDENTIALS_ARGS' cannot be determined automated. Use configuration file "$conf_file" to set parameter manually." fi fi fi if ! $QUIET_MODE ; then echo "" echo "" echo -e "\033[32m\033[1m====================\033[m" echo "Create MySQL Database settings" echo -e "\033[32m\033[1m====================\033[m" echo "" echo " MySQL Distribution...........: $MYSQL_CUR_DISTRIBUTION" if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] ; then echo " MariaDB Version..............: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}" else echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}" fi echo "" echo " MySQL Command................: ${mysql_command}" echo "" if [[ -n "$DATABASE_SERVER" ]] ; then echo " Database server..............: $DATABASE_SERVER" else echo " Database server..............: localhost" fi echo " Database name................: $DATABASE_NAME" echo "" echo " MySQL Credentials............: $DATABASE_CREDENTIALS_ARGS" echo "" echo " Database Dump File...........: $DATABASE_DUMP_FILE" echo "" echo "" echo "" fi if ! $NON_INTERACTIVE_MODE ; then echo -e -n "\033[1mContinue restoring database with above settings? [y/n]:\033[m " read OK while [[ "X${OK}X" = "XX" ]] ; do echo "" echo -e -n "\033[1mContinue with above settings? [y/n]:\033[m " read OK done if [[ "${OK,,}" != 'yes' ]] && [[ "${OK,,}" != 'y' ]]; then fatal "Abort by user request." fi fi # ----- # - Doing some checks # ----- if ! $QUIET_MODE ; then echo "" echo "" echo -e "\033[37m\033[1mDoing some checks..\033[m" echo "" fi echononl " Checkif Database Dump File exists .." if [[ ! -f "$DATABASE_DUMP_FILE" ]]; then fatal "Database Dump File '$DATABASE_DUMP_FILE' not found!" fi echononl " Check connection to Database Server.." if [[ -n "$DATABASE_SERVER" ]] ; then if ! $(${mysql_command} -h $DATABASE_SERVER $DATABASE_CREDENTIALS_ARGS -e ";" > /dev/null 2>&1) ; then fatal "Cannot connect to Database Server '$DATABASE_SERVER'!" fi else if ! $(${mysql_command} $DATABASE_CREDENTIALS_ARGS -e ";" > /dev/null 2>&1) ; then fatal "Cannot connect to Database Server!" fi fi echo_done echononl " Check connection to Database '$DATABASE_NAME'.." if [[ -n "$DATABASE_SERVER" ]] ; then if ! $(${mysql_command} -h $DATABASE_SERVER $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME -e ";" > /dev/null 2>&1) ; then fatal "Cannot connect to Database '$DATABASE_NAME'!" fi else if ! $(${mysql_command} $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME -e ";" > /dev/null 2>&1) ; then fatal "Cannot connect to Database '$DATABASE_NAME'!" fi fi echo_done # ----- # - Doing some pre-script tasks # ----- if ! $QUIET_MODE ; then echo "" echo "" echo -e "\033[37m\033[1mDoing some pre-script tasks..\033[m" echo "" fi # - Deaktiviere Cronjobs # - _backup_crontab_file=/tmp/crontab_root.${backup_date} echononl " Backup Crontab to '$_backup_crontab_file'" crontab -l > $_backup_crontab_file 2> $log_file if [[ $? -eq 0 ]]; then echo_ok else echo_failed fatal "$(cat $log_file)" fi echononl " Remove crontab for root.." crontab -r > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed fatal "$(cat $log_file)" fi # - Stop Apache Webserver # - blank_line echononl " Stop Apache Webserver.." if service_exists apache2 ; then systemctl stop apache2 if [[ $? -eq 0 ]]; then echo_ok else echo_failed fatal "$(cat $log_file)" fi else echo_skipped fi blank_line # - GET current (global) Autocommit value # - echononl " GET current (global) value \033[1mautocommit\033[m" CUR_AUTOCOMMIT="$(${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'autocommit'" | awk '{print$2}')" >> $log_file 2>&1 if [[ $? -eq 0 ]];then echo_ok else echo_failed error "$(cat $log_file)" fi # - GET current (global) value for 'foreign_key_checks' # - echononl " GET current (global) value \033[1mforeign_key_checks\033[m" CUR_FOREIGN_KEY_CHECKS="$(${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'foreign_key_checks'" | awk '{print$2}')" >> $log_file 2>&1 if [[ $? -eq 0 ]];then echo_ok else echo_failed error "$(cat $log_file)" fi # - GET current (global) value for 'unique_checks' # - echononl " GET current (global) value \033[1munique_checks\033[m" CUR_UNIQUE_CHECKS="$(${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'unique_checks'" | awk '{print$2}')" >> $log_file 2>&1 if [[ $? -eq 0 ]];then echo_ok else echo_failed error "$(cat $log_file)" fi # - GET current (global) value for 'innodb_flush_log_at_trx_commit' # - echononl " GET current (global) value \033[1minnodb_flush_log_at_trx_commit\033[m" CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT="$(${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'innodb_flush_log_at_trx_commit'" | awk '{print$2}')" >> $log_file 2>&1 if [[ $? -eq 0 ]];then echo_ok else echo_failed error "$(cat $log_file)" fi # - GET current (global) value for 'max_allowed_packet' # - echononl " GET current (global) value \033[1mmax_allowed_packet\033[m" CUR_MAX_ALLOWED_PACKET="$(${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'" | awk '{print$2}')" >> $log_file 2>&1 if [[ $? -eq 0 ]];then echo_ok else echo_failed error "$(cat $log_file)" fi blank_line echononl " Set Autocommit to OFF" ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='OFF'" >> $log_file 2>&1 if [[ $? -eq 0 ]];then echo_ok else echo_failed error "$(cat $log_file)" fi echononl " Set foreign_key_checks to OFF" ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL foreign_key_checks='OFF'" >> $log_file 2>&1 if [[ $? -eq 0 ]];then echo_ok else echo_failed error "$(cat $log_file)" fi echononl " Set unique_checks to OFF" ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL unique_checks='OFF'" >> $log_file 2>&1 if [[ $? -eq 0 ]];then echo_ok else echo_failed error "$(cat $log_file)" fi echononl " Set innodb_flush_log_at_trx_commit to 2" ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL innodb_flush_log_at_trx_commit=2" >> $log_file 2>&1 if [[ $? -eq 0 ]];then echo_ok else echo_failed error "$(cat $log_file)" fi echononl " Set max_allowed_packet to 1G" ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL max_allowed_packet=1073741824" >> $log_file 2>&1 if [[ $? -eq 0 ]];then echo_ok else echo_failed error "$(cat $log_file)" fi if ! $QUIET_MODE ; then echo "" echo "" echo -e "\033[1mProcessing restoring database \033[32m$DATABASE_NAME\033[m" echo "" fi echononl " Restore Database '${DATABASE_NAME}'.." b_timestamp=$(date +"%s") if [[ -n "$DATABASE_SERVER" ]] ; then ${mysql_command} -h $DATABASE_SERVER $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME < $DATABASE_DUMP_FILE > $log_file 2>&1 else ${mysql_command} $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME < $DATABASE_DUMP_FILE > $log_file 2>&1 fi retval=$? e_timestamp=$(date +"%s") echo_duration "$(time_duration $b_timestamp $e_timestamp)" if [[ $retval -eq 0 ]]; then echo_ok else echo_failed fatal "$(cat $log_file)" fi # ----- # - Doing some post-script tasks # ----- if ! $QUIET_MODE ; then echo "" echo "" echo -e "\033[37m\033[1mDoing some post-script tasks..\033[m" echo "" fi echononl " Set Autocommit to $CUR_AUTOCOMMIT" ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='$CUR_AUTOCOMMIT'" >> $log_file 2>&1 if [[ $? -eq 0 ]];then echo_ok CUR_AUTOCOMMIT="" else echo_failed error "$(cat $log_file)" fi echononl " Set foreign_key_checks to $CUR_FOREIGN_KEY_CHECKS" ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL foreign_key_checks='$CUR_FOREIGN_KEY_CHECKS'" >> $log_file 2>&1 if [[ $? -eq 0 ]];then echo_ok CUR_FOREIGN_KEY_CHECKS="" else echo_failed error "$(cat $log_file)" fi echononl " Set unique_checks to $CUR_UNIQUE_CHECKS" ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL unique_checks='$CUR_UNIQUE_CHECKS'" >> $log_file 2>&1 if [[ $? -eq 0 ]];then echo_ok CUR_UNIQUE_CHECKS="" else echo_failed error "$(cat $log_file)" fi echononl " Set innodb_flush_log_at_trx_commit to $CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT" ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL innodb_flush_log_at_trx_commit=$CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT" >> $log_file 2>&1 if [[ $? -eq 0 ]];then echo_ok CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT="" else echo_failed error "$(cat $log_file)" fi echononl " Set max_allowed_packet to $CUR_MAX_ALLOWED_PACKET" ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL max_allowed_packet=$CUR_MAX_ALLOWED_PACKET" >> $log_file 2>&1 if [[ $? -eq 0 ]];then echo_ok CUR_MAX_ALLOWED_PACKET="" else echo_failed error "$(cat $log_file)" fi blank_line # - Start Apache Webserver # - blank_line echononl " Start Apache Webserver.." if service_exists apache2 ; then systemctl start apache2 > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed error "$(cat $log_file)" fi else echo_skipped fi clean_up 0