#!/bin/bash
#
echo "MySQL and MariaDB upgrade helper for CentOS Linux 7.x"
echo ""

ARGV="$@"

if [ "x$ARGV" = "x" ] || [ "x$ARGV" = "xusage" ] || [ "x$ARGV" = "xhelp" ] || [ "x$ARGV" = "x--help" ]; then
  cat << EOF
  Usage: $0 mysql-57 || mariadb-10.0 || mariadb-10.1 || mariadb-10.2 || mariadb-10.3 || mariadb-10.4 || mariadb-10.5
  Examples: 
    Upgrade MariaDB 5.5 to MySQL 5.7
    $0 mysql-57

    Upgrade MariaDB 5.5 to MariaDB 10.3
    $0 mariadb-10.3

EOF
 exit 1
fi

showserverparams () {
echo -n "OS and MySQL Version is: "
echo ""
cat /etc/redhat-release
mysql -V
echo ""

echo "Disk usage:"
df -h -P -l -x tmpfs -x devtmpfs
echo ""

echo "MySQL datasize:"
du -hs /var/lib/mysql
echo ""
}

createmysqlbackup () {
read -p "Do you want to create /var/lib/mysql backup to /var/lib/mysql-5.5-old-bak? [Y/n] " -n 1 -r
echo ""
 if [[ $REPLY =~ ^[Nn]$ ]]
 then
   echo "MySQL data is NOT backuped !!"
 else
   echo "Stoping mariadb service and prevent to start them ..."
   systemctl stop mariadb.service
   chmod -x /usr/libexec/mysqld 
   echo "rsync to /var/lib/mysql-5.5-old-bak"
   rsync -a /var/lib/mysql/ /var/lib/mysql-5.5-old-bak
   chmod +x /usr/libexec/mysqld
   echo "Start mariadb service ..."
   systemctl start mariadb.service
 fi
}

install_depended_packages () {
  echo "Update ca-certificates package ..."
  yum -y install ca-certificates
}

install_mysql_repo () {
echo "Add mysql repostory ..."
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
rpm -Uvh https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
}

install_mariadb_repo () {
echo "Add MariaDB repostory ..."
cat > /etc/yum.repos.d/MariaDB.repo << EOF
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/$1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF
}

install_mysql_server () {
echo "Install MySQL server... Please check all and press Y to continue..."
systemctl stop mariadb.service

yum install mysql-community-server

#mkdir /var/run/mysqld ; chown mysql:mysql /var/run/mysqld
#echo "d /var/run/mysqld 0755 mysql mysql -" > /etc/tmpfiles.d/mysql.conf

systemctl enable mysqld.service
systemctl start mysqld.service

echo "Doing mysql_upgrade"
mysql_upgrade

#symlink for ISPmanager
ln -s /usr/sbin/mysqld /usr/libexec/mysqld
}

install_mariadb_server () {
echo "Install MariaDB server... Please check all and press Y to continue..."
systemctl stop mariadb.service

yum install MariaDB-server MariaDB-client

mkdir /var/run/mysqld ; chown mysql:mysql /var/run/mysqld
echo "d /var/run/mysqld 0755 mysql mysql -" > /etc/tmpfiles.d/mysql.conf

if [ "$1" = "10.0" ]; then
  systemctl enable mysql.service
  systemctl start mysql.service
 else
  systemctl enable mariadb.service
  systemctl start mariadb.service
fi

echo "Doing mysql_upgrade ..."
mysql_upgrade

#symlink for ISPmanager
ln -s /usr/sbin/mysqld /usr/libexec/mysqld
}

fix_ispmanager_services () {
case $1 in
     10.5)
      echo "Fixing ISPmanager services (monitoring) for MariaDB 10.5 ..."
      cp /usr/local/mgr5/var/services.db /usr/local/mgr5/var/services.db.bak
      sqlite3 -line /usr/local/mgr5/var/services.db 'update services set bin="mariadbd" where name="mariadb";'
      sqlite3 -line /usr/local/mgr5/var/services.db 'update services set pid="/var/run/mysqld/mysqld.pid" where name="mariadb";'
      rm -f /usr/lib/systemd/system/mysqld.service 
      rm -f /usr/lib/systemd/system/mysql.service 
       ;;
     *)
      echo "WARNING : nothing to do in the fix_ispmanager_services , running without params"
esac
}

installer () {
## main block
#start show server data
showserverparams

# wait user input
read -p "Do you want to continue and upgrade database server? [Y/n] " -n 1 -r
echo ""
 if [[ $REPLY =~ ^[Nn]$ ]]
 then
   exit 1
 fi

# select version and do run
case $1 in
     mysql-57)
      createmysqlbackup
      install_depended_packages
      install_mysql_repo
      install_mysql_server
       ;;
     mariadb-10.0)
      createmysqlbackup
      install_depended_packages
      install_mariadb_repo 10.0
      install_mariadb_server 10.0
       ;;
     mariadb-10.1)
      createmysqlbackup
      install_depended_packages
      install_mariadb_repo 10.1
      install_mariadb_server
       ;;
     mariadb-10.2)
      createmysqlbackup
      install_depended_packages
      install_mariadb_repo 10.2
      install_mariadb_server
       ;;
     mariadb-10.3)
      createmysqlbackup
      install_depended_packages
      install_mariadb_repo 10.3
      install_mariadb_server
       ;;
     mariadb-10.4)
      createmysqlbackup
      install_depended_packages
      install_mariadb_repo 10.4
      install_mariadb_server
       ;;
     mariadb-10.5)
      createmysqlbackup
      install_depended_packages
      install_mariadb_repo 10.5
      install_mariadb_server
      fix_ispmanager_services 10.5
       ;;
     *)
      echo "ERROR : Unknown version in argument, run script without params for help" ; exit 1;
esac
}

# start main module installer
installer $1
