#!/bin/bash



#Preupgrade Assistant performs system upgradability assessment
#and gathers information required for successful operating system upgrade.
#Copyright (C) 2013 Red Hat Inc.
#Honza Horak <hhorak@redhat.com>
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
. /usr/share/preupgrade/common.sh
check_applies_to ""
check_rpm_to "" ""
#END GENERATED SECTION

# TODO: unpublished KB article describing RHEL-5 to RHEL-7 migration
# might be mentioned after publishing
# [link:https://access.redhat.com/articles/2123151]

# check if it is installed at least one of the followed packages
# 1 means False
export my_serv=1
export my55_serv=1

is_pkg_installed "mysql-server" && is_dist_native "mysql-server" && my_serv=0
is_pkg_installed "mysql55-mysql-server" && is_dist_native "mysql55-mysql-server" \
  && my55_serv=0

[ $my_serv -eq 1 ] && [ $my55_serv -eq 1 ] \
  && is_pkg_installed "mysql51-mysql-server" \
  && is_dist_native "mysql51-mysql-server" \
  && {
    cat >>$SOLUTION_FILE <<EOF
You have installed MySQL 5.1 and maybe you are using it now. This is unsupported,
because MySQL 5.1 is provided only for the purposes of migrating to MySQL 5.5.
You have to move to MySQL 5.5 before migration, and then run 'preupg' again.
EOF
    log_high_risk "Maybe you are using MySQL 5.1, which is unsupported, and it will not be migrated."
    exit_fail
  }


[ $my_serv -eq 1 ] && [ $my55_serv -eq 1 ] && exit_not_applicable

# This check can be used if you need root privilegues
check_root

source ../mysql-common.sh
export datadir
export datadir55="/opt/rh/mysql55/root/var/lib"
export errlogfile

# provide general information about migration from MySQL to MariaDB
#
# How to test:
# 1) see the text if it is formatted well in case the data stack is initialized
#    (/var/lib/mysql/mysql exists - or /opt/rh/mysql51/root/var/lib/mysql)
#    if both exists
# 2) see short message in case the data stack is not initialized
#
#

if [ $my_serv -eq 0 ] && [ ! -d "${datadir}/mysql/mysql" ] ; then
    cat >>$SOLUTION_FILE <<EOF
No MySQL data stack initialized at $datadir/mysql. In case you have the MySQL 5.0
initialized in another place and you still use it, see:
[link:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Deployment_Guide/ch-Migrating_from_MySQL_5.0_to_MySQL_5.5.html]

EOF
    [ $my55_serv -eq 1 ] && {
        log_high_risk "MySQL data stack not found initialized at ${datadir}. Follow the instructions from the report."
        exit_fail
    }

  [ $my55_serv -eq 0 ] && cat >>$SOLUTION_FILE <<EOF
If you have upgraded your database to MySQL 5.5 already, and you do not use MySQL 5.0
anymore, you can ignore it.

EOF
fi

if [ $my55_serv -eq 0 ] &&  [ ! -d "${datadir55}/mysql/mysql" ] ; then
    cat >>$SOLUTION_FILE <<EOF
No MySQL data stack initialized at ${datadir55}/mysql, but MySQL 5.5 is installed.
This means that either the data from MySQL 5.0 was not yet migrated to MySQL 5.5,
or that the data is stored in some other path.
EOF
fi


[ $my_serv -eq 0 ] && {
  log_high_risk "Before migration, follow the instructions from the report and dump all data."
  cat >>$SOLUTION_FILE <<EOF
Before migrating from MySQL 5.0 to MariaDB 5.5, back up all your data,
including any MySQL databases. You can upgrade the data in two possible ways:

The first way is to dump all data into an SQL file, so you can restore the data after
migration to MariaDB 5.5. To back up the data in this way, enter:
  root> service mysqld start
  user> mysqldump --all-databases --routines --triggers --events>/your/backup.sql

The second way is to upgrade from MySQL 5.0 to MySQL 5.5 first. The upgraded data can be used in MariaDB 5.5, which is available in Red Hat Enterprise Linux 7 as the mariadb-server package. This way requires backing up all important files (see below).

However, the only supported way to upgrade from MySQL 5.0 to MySQL 5.5
or MariaDB 5.5 is to use MySQL 5.1 as an intermediate step. This is why the mysql51*
Software Collection packages are provided in Red Hat Enterprise Linux 5. Note that the MySQL 5.1 packages
are not supported in Red Hat Enterprise Linux 5 and are provided only for the purposes of migrating to
MySQL 5.5 or MariaDB 5.5. You should not use the mysql51* packages on any of your
production systems.

When choosing the variant of ugrading MySQL in-place and backing up the binary files, upgrade the data from MySQL 5.0 to MySQL 5.1 first:
  root> service mysqld stop
  root> yum -y install mysql51
  root> rm -rf /opt/rh/mysql51/root/var/lib/mysql
  root> cp -r /var/lib/mysql /opt/rh/mysql51/root/var/lib/
  root> chown -R mysql:mysql /opt/rh/mysql51/root/var/lib/mysql/
  root> restorecon -R /opt/rh/mysql51/root/var/lib/mysql/
  root> service mysql51-mysqld start
  root> scl enable mysql51 'mysql_upgrade'

Then, analogically upgrade from MySQL 5.1 to MySQL 5.5:
  root> service mysql51-mysqld stop
  root> yum -y install mysql55
  root> rm -rf /opt/rh/mysql55/root/var/lib/mysql/
  root> cp -r /opt/rh/mysql51/root/var/lib/mysql/ /opt/rh/mysql55/root/var/lib/mysql/
  root> chown -R mysql:mysql /opt/rh/mysql55/root/var/lib/mysql/
  root> restorecon -R /opt/rh/mysql55/root/var/lib/mysql/
  root> service mysql55-mysqld start
  root> scl enable mysql55 'mysql_upgrade'

And finally, stop the server and copy the data files to a safe location:

  root> service mysql55-mysqld stop
  root> cp -r ${datadir55}/mysql/ /your/backup/location
  root> cp -a $datadir /your/backup/location2

This method is usually faster but there are certain risks
and known problems. For more information, refer to MySQL 5.1 Release Notes:
[link:http://dev.mysql.com/doc/relnotes/mysql/5.1/en/]
[link:https://dev.mysql.com/doc/refman/5.1/en/upgrading-from-previous-series.html]
and to MySQL 5.5 Release Notes:
[link:http://dev.mysql.com/doc/relnotes/mysql/5.5/en/]
[link:http://dev.mysql.com/doc/refman/5.5/en/upgrading-from-previous-series.html]
EOF
}

[ $my_serv -eq 1 ] && [ $my55_serv -eq 0 ] && {
  log_high_risk "Before migration, follow the instructions from the report and dump all data."
  cat >>$SOLUTION_FILE <<EOF

You probably upgraded your databases to MySQL 5.5 on this machine already.
In this case, you could probably back up your data stack directory and use
it on the migrated system later. For example:
  root> cp -r ${datadir}/mysql/ /your/backup/location
EOF
}

cat >>$SOLUTION_FILE <<EOF

For further information about migration from MySQL 5.x to MariaDB 5.5, see
[link:https://mariadb.com/kb/en/mariadb-versus-mysql-compatibility]
[link:https://mariadb.com/kb/en/upgrading-to-mariadb-from-mysql/]

EOF

exit_fail
