#!/bin/bash -e # # summary of how this script can be called: # * install # * install # * upgrade # * abort-upgrade # # shellcheck source=/dev/null . /usr/share/debconf/confmodule # Just kill the invalid insserv.conf.d directory without fallback if [ -d "/etc/insserv.conf.d/mariadb/" ] then rm -rf "/etc/insserv.conf.d/mariadb/" fi if [ -n "$DEBIAN_SCRIPT_DEBUG" ] then set -v -x DEBIAN_SCRIPT_TRACE=1 fi ${DEBIAN_SCRIPT_TRACE:+ echo "#42#DEBUG# RUNNING $0 $*" 1>&2 } export PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin mariadb_datadir=/var/lib/mariadb legacy_mariadb_datadir=/var/lib/mysql mariadb_upgradedir=/var/lib/mysql-upgrade MARIADBD_USERS="root" # Check if user 'mysql' exists before referring to it in pgrep # to avoid pgrep erroring on 'invalid user name' if id mysql >/dev/null 2>&1 then MARIADBD_USERS="$MARIADBD_USERS,mysql" fi # Try to stop the server in a sane way. If it does not success let the admin # do it himself. No database directories should be removed while the server # is running! Another mariadbd in e.g. a different chroot is fine for us. stop_server() { # Return immediately if there are no mysqld processes running on a host # (leave containerized processes with the same name in other namespaces) # as there is no point in trying to shutdown in that case. if ! pgrep -x -u "$MARIADBD_USERS" --nslist pid --ns $$ "mysqld|mariadbd" > /dev/null then return fi set +e invoke-rc.d mariadb stop invoke-rc.d mysql stop # Backwards compatibility errno=$? set -e # systemctl could emit exit code 100=no init script (fresh install) if [ "$errno" != 0 ] && [ "$errno" != 100 ] then echo "Attempt to stop MariaDB/MySQL server returned exitcode $errno" 1>&2 echo "There is a MariaDB/MySQL server running, but we failed in our attempts to stop it." 1>&2 echo "Check if there is any server running with 'pgrep -af \"mysqld|mariadbd\"' and" 1>&2 echo "try to stop it yourself by issuing 'invoke-rc.d mariadb stop'." 1>&2 db_stop exit 1 fi } # Check if the version file mariadb_upgrade_info or mysql_upgrade_info is # included with the provided data directory. If it is a MariaDB version, then # return the major version number only (XX.XX). Return empty if this directory # does not belong to MariaDB. get_mariadb_upgrade_info_major_version() { datadir="$1" # Check both mariadb_upgrade_info (higher priority) and mysql_upgrade_info. if [ -f "$datadir/mysql_upgrade_info" ] then read -r full_version < "$datadir/mysql_upgrade_info" fi if [ -f "$datadir/mariadb_upgrade_info" ] then read -r full_version < "$datadir/mariadb_upgrade_info" fi # If version is not empty and contains 'MariaDB', get the version number. if [ -n "$full_version" ] && echo "$full_version" | grep -q "MariaDB" then # The major version number should the first two components of the version # string (e.g. 11.8 from 11.8.6). echo "$full_version" | awk -F'.' '{print $1"."$2}' fi } ################################ main() ########################## this_version=11.8 max_upgradeable_version=5.7 # Check for previous mariadb version in the new datadir. found_version=$(get_mariadb_upgrade_info_major_version "$mariadb_datadir") # If nothing was found in the new datadir, check the legacy datadir to see if # it has a MariaDB instance. if [ -z "$found_version" ] && [ -d "$legacy_mariadb_datadir" ] then found_version=$(get_mariadb_upgrade_info_major_version "$legacy_mariadb_datadir") # If a mariadb version was found in the legacy datadir, continue using it. if [ -n "$found_version" ] then mariadb_datadir="$legacy_mariadb_datadir" fi fi # If an upgrade is detected, proceed with it automatically without # requiring any user interaction. # # However, if the user attempts to downgrade, warn about the incompatibility. # Downgrade is detected if the flag version is bigger than $this_version # (e.g. 10.1 > 10.0) or the flag version is smaller than 10.0 but bigger # than $max_upgradeable_version. if [ -n "$found_version" ] then echo "$mariadb_datadir: found previous version $found_version" if dpkg --compare-versions "$found_version" '>>' "$this_version" then downgrade_detected=true fi if dpkg --compare-versions "$found_version" '>>' "$max_upgradeable_version" \ && dpkg --compare-versions "$found_version" '<<' "10.0" then downgrade_detected=true fi fi # Don't abort dpkg if downgrade is detected (as was done previously). # Instead simply move the old datadir and create a new for this_version. if [ -n "$downgrade_detected" ] then db_input critical "mariadb-server/old_data_directory_saved" || true db_go echo "The contents of $mariadb_datadir/ indicates a" 1>&2 echo "version that cannot automatically be upgraded. Therefore the" 1>&2 echo "previous data directory will be renamed to $mariadb_datadir-$found_version and" 1>&2 echo "a new data directory will be initialized at $mariadb_datadir." 1>&2 echo "Please manually export/import your data (e.g. with mysqldump) if needed." 1>&2 mv -f "$mariadb_datadir" "$mariadb_datadir-$found_version" # Also move away the old debian.cnf file that included credentials that are # no longer valid. If none existed, ignore error and let dpkg continue. mv -f /etc/mysql/debian.cnf "/etc/mysql/debian.cnf-$found_version" || true fi # to be sure stop_server # if there's a symlink, let's store where it's pointing, because otherwise # it's going to be lost in some situations for dir in DATADIR LOGDIR do checkdir=$(eval echo "$"$dir) if [ -L "$checkdir" ] then # Use mkdir option 'Z' to create with correct SELinux context. mkdir -pZ "$mariadb_upgradedir" cp -dT "$checkdir" "$mariadb_upgradedir/$dir.link" fi done # creating mysql home directory if [ ! -d $mariadb_datadir ] && [ ! -L $mariadb_datadir ] then # Use mkdir option 'Z' to create with correct SELinux context. mkdir -Z $mariadb_datadir fi # As preset blocksize of GNU df is 1024 then available bytes is $df_available_blocks * 1024 # 4096 blocks is then lower than 4 MB df_available_blocks="$(LC_ALL=C BLOCKSIZE='' df --output=avail "$mariadb_datadir" | tail -n 1)" if [ "$df_available_blocks" -lt "4096" ] then echo "ERROR: There's not enough space in $mariadb_datadir/" 1>&2 db_stop exit 1 fi db_stop # Automatically added by dh_installinit/13.31 if [ "$1" = "install" ] && [ -n "$2" ] && [ -e "/etc/init.d/mariadb" ] ; then chmod +x "/etc/init.d/mariadb" >/dev/null || true fi # End automatically added section # dh_installinit/13.11.3 adds this check but only with 'install', so we need to # have and extra one to check 'upgrade'. This ensures that upgrades from # mariadb-server-x.y to mariadb-server (without version suffix) ends up with # the executable bit set on /etc/init.d/mariadb, which otherwise would end up # disabled due to the mariadb-server-x.y.postrm being triggered. # $1 = upgrade # $2 = 1:10.6.11-2 if [ "$1" = "upgrade" ] && [ -n "$2" ] && [ -e "/etc/init.d/mariadb" ] then chmod +x "/etc/init.d/mariadb" >/dev/null || true fi # dh_installinit/13.11.3 adds this check but with extra condition that there # must be a version passed as '$2', but that will always be empty when install # runs after the unpack that is retriggered for package 'mariadb-server' when # the old 'mariadb-server-10.6' is purged, so we need to repeat the same check # here without any expectation for '$2'. This ensures that upgrades from # mariadb-server-x.y to mariadb-server (without version suffix) ends up with the # executable bit set on /etc/init.d/mariadb. if [ "$1" = "install" ] && [ -e "/etc/init.d/mariadb" ] then chmod +x "/etc/init.d/mariadb" >/dev/null || true fi