upgrade_db.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/oneinstack
  10. Upgrade_DB() {
  11. pushd ${oneinstack_dir}/src > /dev/null
  12. [ ! -e "${db_install_dir}/bin/mysql" ] && echo "${CWARNING}MySQL/MariaDB/Percona is not installed on your system! ${CEND}" && exit 1
  13. [ "${armplatform}" == 'y' ] && echo "${CWARNING}The arm architecture operating system does not support upgrading MySQL/MariaDB/Percona! ${CEND}" && exit 1
  14. # check db passwd
  15. while :; do
  16. ${db_install_dir}/bin/mysql -uroot -p${dbrootpwd} -e "quit" > /dev/null 2>&1
  17. if [ $? -eq 0 ]; then
  18. break
  19. else
  20. echo
  21. read -e -p "Please input the root password of database: " NEW_dbrootpwd
  22. ${db_install_dir}/bin/mysql -uroot -p${NEW_dbrootpwd} -e "quit" >/dev/null 2>&1
  23. if [ $? -eq 0 ]; then
  24. dbrootpwd=${NEW_dbrootpwd}
  25. sed -i "s+^dbrootpwd.*+dbrootpwd='$dbrootpwd'+" ../options.conf
  26. break
  27. else
  28. echo "${CFAILURE}${DB} root password incorrect,Please enter again! ${CEND}"
  29. fi
  30. fi
  31. done
  32. OLD_db_ver_tmp=`${db_install_dir}/bin/mysql -uroot -p${dbrootpwd} -e 'select version()\G;' | grep version | awk '{print $2}'`
  33. if [ -n "`${db_install_dir}/bin/mysql -V | grep -o MariaDB`" ]; then
  34. [ "${OUTIP_STATE}"x == "China"x ] && DOWN_ADDR=https://mirrors.tuna.tsinghua.edu.cn/mariadb || DOWN_ADDR=https://archive.mariadb.org
  35. DB=MariaDB
  36. OLD_db_ver=`echo ${OLD_db_ver_tmp} | awk -F'-' '{print $1}'`
  37. elif [ -n "`${db_install_dir}/bin/mysql -V | grep -o Percona`" ]; then
  38. DB=Percona
  39. OLD_db_ver=${OLD_db_ver_tmp}
  40. else
  41. #[ "${OUTIP_STATE}"x == "China"x ] && DOWN_ADDR=http://mirrors.ustc.edu.cn/mysql-ftp/Downloads || DOWN_ADDR=http://cdn.mysql.com/Downloads
  42. DOWN_ADDR=http://cdn.mysql.com/Downloads
  43. DB=MySQL
  44. OLD_db_ver=${OLD_db_ver_tmp%%-log}
  45. fi
  46. #backup
  47. echo
  48. echo "${CSUCCESS}Starting ${DB} backup${CEND}......"
  49. ${db_install_dir}/bin/mysqldump -uroot -p${dbrootpwd} --opt --all-databases > DB_all_backup_$(date +"%Y%m%d").sql
  50. [ -f "DB_all_backup_$(date +"%Y%m%d").sql" ] && echo "${DB} backup success, Backup file: ${MSG}`pwd`/DB_all_backup_$(date +"%Y%m%d").sql${CEND}"
  51. #upgrade
  52. echo
  53. echo "Current ${DB} Version: ${CMSG}${OLD_db_ver}${CEND}"
  54. while :; do echo
  55. [ "${db_flag}" != 'y' ] && read -e -p "Please input upgrade ${DB} Version(example: ${OLD_db_ver}): " NEW_db_ver
  56. if [ `echo ${NEW_db_ver} | awk -F. '{print $1"."$2}'` == `echo ${OLD_db_ver} | awk -F. '{print $1"."$2}'` ]; then
  57. if [ "${DB}" == 'MariaDB' ]; then
  58. DB_filename=mariadb-${NEW_db_ver}-linux-systemd-x86_64
  59. DB_URL=${DOWN_ADDR}/mariadb-${NEW_db_ver}/bintar-linux-systemd-x86_64/${DB_filename}.tar.gz
  60. elif [ "${DB}" == 'Percona' ]; then
  61. if [[ "`echo ${NEW_db_ver} | awk -F. '{print $1"."$2}'`" =~ ^5.[5-6]$ ]]; then
  62. perconaVerStr1=$(echo ${NEW_db_ver} | sed "s@-@-rel@")
  63. else
  64. perconaVerStr1=${NEW_db_ver}
  65. fi
  66. if [[ "`echo ${NEW_db_ver} | awk -F. '{print $1"."$2}'`" =~ ^8.0$ ]]; then
  67. DB_filename=Percona-Server-${perconaVerStr1}-Linux.x86_64.glibc2.28
  68. elif [[ "`echo ${NEW_db_ver} | awk -F. '{print $1"."$2}'`" =~ ^5.7$ ]]; then
  69. DB_filename=Percona-Server-${perconaVerStr1}-Linux.x86_64.glibc2.17
  70. else
  71. DB_filename=Percona-Server-${perconaVerStr1}-Linux.x86_64.${sslLibVer}
  72. fi
  73. DB_URL=https://www.percona.com/downloads/Percona-Server-`echo ${NEW_db_ver} | awk -F. '{print $1"."$2}'`/Percona-Server-${NEW_db_ver}/binary/tarball/${DB_filename}.tar.gz
  74. elif [ "${DB}" == 'MySQL' ]; then
  75. DB_filename=mysql-${NEW_db_ver}-linux-glibc2.12-x86_64
  76. if [ `echo ${OLD_db_ver} | awk -F. '{print $1"."$2}'` == '8.0' ]; then
  77. DB_URL=${DOWN_ADDR}/MySQL-`echo ${NEW_db_ver} | awk -F. '{print $1"."$2}'`/${DB_filename}.tar.xz
  78. else
  79. DB_URL=${DOWN_ADDR}/MySQL-`echo ${NEW_db_ver} | awk -F. '{print $1"."$2}'`/${DB_filename}.tar.gz
  80. fi
  81. fi
  82. [ ! -e "`ls ${DB_filename}.tar.?z 2>/dev/null`" ] && wget --no-check-certificate -c ${DB_URL} > /dev/null 2>&1
  83. if [ -e "`ls ${DB_filename}.tar.?z 2>/dev/null`" ]; then
  84. echo "Download [${CMSG}`ls ${DB_filename}.tar.?z 2>/dev/null`${CEND}] successfully! "
  85. else
  86. echo "${CWARNING}${DB} version does not exist! ${CEND}"
  87. fi
  88. break
  89. else
  90. echo "${CWARNING}input error! ${CEND}Please only input '${CMSG}${OLD_db_ver%.*}.xx${CEND}'"
  91. [ "${db_flag}" == 'y' ] && exit
  92. fi
  93. done
  94. if [ -e "`ls ${DB_filename}.tar.?z 2>/dev/null`" ]; then
  95. echo "[${CMSG}`ls ${DB_filename}.tar.?z 2>/dev/null`${CEND}] found"
  96. if [ "${db_flag}" != 'y' ]; then
  97. echo "Press Ctrl+c to cancel or Press any key to continue..."
  98. char=`get_char`
  99. fi
  100. if [ "${DB}" == 'MariaDB' ]; then
  101. tar xzf ${DB_filename}.tar.gz
  102. service mysqld stop
  103. mv ${mariadb_install_dir}{,_old_`date +"%Y%m%d_%H%M%S"`}
  104. mv ${mariadb_data_dir}{,_old_`date +"%Y%m%d_%H%M%S"`}
  105. [ ! -d "${mariadb_install_dir}" ] && mkdir -p ${mariadb_install_dir}
  106. mkdir -p ${mariadb_data_dir};chown mysql:mysql -R ${mariadb_data_dir}
  107. mv ${DB_filename}/* ${mariadb_install_dir}/
  108. sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/local/lib/libjemalloc.so@' ${mariadb_install_dir}/bin/mysqld_safe
  109. ${mariadb_install_dir}/scripts/mysql_install_db --user=mysql --basedir=${mariadb_install_dir} --datadir=${mariadb_data_dir}
  110. chown mysql:mysql -R ${mariadb_data_dir}
  111. service mysqld start
  112. ${mariadb_install_dir}/bin/mysql < DB_all_backup_$(date +"%Y%m%d").sql
  113. service mysqld restart
  114. ${mariadb_install_dir}/bin/mysql -uroot -p${dbrootpwd} -e "drop database test;" >/dev/null 2>&1
  115. ${mariadb_install_dir}/bin/mysql -uroot -p${dbrootpwd} -e "reset master;" >/dev/null 2>&1
  116. ${mariadb_install_dir}/bin/mysql_upgrade -uroot -p${dbrootpwd} >/dev/null 2>&1
  117. [ $? -eq 0 ] && echo "You have ${CMSG}successfully${CEND} upgrade from ${CMSG}${OLD_db_ver}${CEND} to ${CMSG}${NEW_db_ver}${CEND}"
  118. elif [ "${DB}" == 'Percona' ]; then
  119. tar xzf ${DB_filename}.tar.gz
  120. service mysqld stop
  121. mv ${percona_install_dir}{,_old_`date +"%Y%m%d_%H%M%S"`}
  122. mv ${percona_data_dir}{,_old_`date +"%Y%m%d_%H%M%S"`}
  123. [ ! -d "${percona_install_dir}" ] && mkdir -p ${percona_install_dir}
  124. mkdir -p ${percona_data_dir};chown mysql:mysql -R ${percona_data_dir}
  125. mv ${DB_filename}/* ${percona_install_dir}/
  126. sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/local/lib/libjemalloc.so@' ${percona_install_dir}/bin/mysqld_safe
  127. sed -i "s@/usr/local/${DB_filename}@${percona_install_dir}@g" ${percona_install_dir}/bin/mysqld_safe
  128. if [[ "`echo ${NEW_db_ver} | awk -F. '{print $1"."$2}'`" =~ ^5.[5-6]$ ]]; then
  129. ${percona_install_dir}/scripts/mysql_install_db --user=mysql --basedir=${percona_install_dir} --datadir=${percona_data_dir}
  130. else
  131. ${percona_install_dir}/bin/mysqld --initialize-insecure --user=mysql --basedir=${percona_install_dir} --datadir=${percona_data_dir}
  132. fi
  133. chown mysql:mysql -R ${percona_data_dir}
  134. service mysqld start
  135. ${percona_install_dir}/bin/mysql < DB_all_backup_$(date +"%Y%m%d").sql
  136. service mysqld restart
  137. ${percona_install_dir}/bin/mysql -uroot -p${dbrootpwd} -e "drop database test;" >/dev/null 2>&1
  138. ${percona_install_dir}/bin/mysql -uroot -p${dbrootpwd} -e "reset master;" >/dev/null 2>&1
  139. ${percona_install_dir}/bin/mysql_upgrade -uroot -p${dbrootpwd} >/dev/null 2>&1
  140. [ $? -eq 0 ] && echo "You have ${CMSG}successfully${CEND} upgrade from ${CMSG}${OLD_db_ver}${CEND} to ${CMSG}${NEW_db_ver}${CEND}"
  141. elif [ "${DB}" == 'MySQL' ]; then
  142. if [ `echo ${OLD_db_ver} | awk -F. '{print $1"."$2}'` == '8.0' ]; then
  143. tar xJf ${DB_filename}.tar.xz
  144. else
  145. tar xzf ${DB_filename}.tar.gz
  146. fi
  147. service mysqld stop
  148. mv ${mysql_install_dir}{,_old_`date +"%Y%m%d_%H%M%S"`}
  149. mv ${mysql_data_dir}{,_old_`date +"%Y%m%d_%H%M%S"`}
  150. [ ! -d "${mysql_install_dir}" ] && mkdir -p ${mysql_install_dir}
  151. mkdir -p ${mysql_data_dir};chown mysql:mysql -R ${mysql_data_dir}
  152. mv ${DB_filename}/* ${mysql_install_dir}/
  153. sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/local/lib/libjemalloc.so@' ${mysql_install_dir}/bin/mysqld_safe
  154. sed -i "s@/usr/local/mysql@${mysql_install_dir}@g" ${mysql_install_dir}/bin/mysqld_safe
  155. if [[ "`echo ${NEW_db_ver} | awk -F. '{print $1"."$2}'`" =~ ^5.[5-6]$ ]]; then
  156. ${mysql_install_dir}/scripts/mysql_install_db --user=mysql --basedir=${mysql_install_dir} --datadir=${mysql_data_dir}
  157. else
  158. ${mysql_install_dir}/bin/mysqld --initialize-insecure --user=mysql --basedir=${mysql_install_dir} --datadir=${mysql_data_dir}
  159. fi
  160. chown mysql:mysql -R ${mysql_data_dir}
  161. [ -e "${mysql_install_dir}/my.cnf" ] && rm -rf ${mysql_install_dir}/my.cnf
  162. sed -i '/myisam_repair_threads/d' /etc/my.cnf
  163. service mysqld start
  164. ${mysql_install_dir}/bin/mysql < DB_all_backup_$(date +"%Y%m%d").sql
  165. service mysqld restart
  166. ${mysql_install_dir}/bin/mysql -uroot -p${dbrootpwd} -e "drop database test;" >/dev/null 2>&1
  167. ${mysql_install_dir}/bin/mysql -uroot -p${dbrootpwd} -e "reset master;" >/dev/null 2>&1
  168. ${mysql_install_dir}/bin/mysql_upgrade -uroot -p${dbrootpwd} >/dev/null 2>&1
  169. [ $? -eq 0 ] && echo "You have ${CMSG}successfully${CEND} upgrade from ${CMSG}${OLD_db_ver}${CEND} to ${CMSG}${NEW_db_ver}${CEND}"
  170. fi
  171. fi
  172. }