1#!/bin/bash
2
3# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; version 2 of the License.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17
18set -e
19
20# It is possible that Debconf has already been removed, too.
21if [ -f /usr/share/debconf/confmodule ]; then
22  . /usr/share/debconf/confmodule
23fi
24
25if [ -n "$DEBIAN_SCRIPT_DEBUG" ]; then set -v -x; DEBIAN_SCRIPT_TRACE=1; fi
26${DEBIAN_SCRIPT_TRACE:+ echo "#42#DEBUG# RUNNING $0 $*" 1>&2 }
27
28mysql_cfgdir=/etc/mysql
29MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
30
31# To avoid having hardcoded paths in the script, we do a search on the path, as suggested at:
32# https://www.debian.org/doc/manuals/developers-reference/ch06.en.html#bpp-debian-maint-scripts
33pathfind() {
34  OLDIFS="$IFS"
35  IFS=:
36  for p in $PATH; do
37    if [ -x "$p/$*" ]; then
38      IFS="$OLDIFS"
39      return 0
40    fi
41  done
42  IFS="$OLDIFS"
43  return 1
44}
45
46# Choose the proper mechanism. Ubuntu 16.04 uses systemd but still provides invoke-rc.d,
47# so the existence of systemd must be checked first.
48invoke() {
49	if pathfind systemctl; then
50		systemctl $1 "@DEB_SYSTEMD_SERVICE_NAME@"
51	elif pathfind invoke-rc.d; then
52		invoke-rc.d mysql $1
53	else
54		/etc/init.d/mysql $1
55	fi
56}
57
58# Try to stop the server in a sane way. If it does not success let the admin
59# do it himself. No database directories should be removed while the server
60# is running!
61stop_server() {
62  set +e
63  invoke stop
64  errno=$?
65  set -e
66
67  if [ "$?" != 0 ]; then
68    echo "Trying to stop the MySQL server resulted in exitcode $?." 1>&2
69    echo "Stop it yourself and try again!" 1>&2
70    exit 1
71  fi
72}
73
74case "$1" in
75  purge|remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
76    if [ -n "`$MYADMIN ping 2>/dev/null`" ]; then
77      stop_server
78      sleep 2
79    fi
80  ;;
81  *)
82    echo "postrm called with unknown argument '$1'" 1>&2
83    exit 1
84  ;;
85esac
86
87# New packaging paradigm for my.cnf as of Dec-2014 for sharing mysql
88# variants in Ubuntu.
89case "$1" in
90  remove|disappear)
91    [ -x /usr/share/mysql-common/configure-symlinks ] && /usr/share/mysql-common/configure-symlinks remove mysql "$mysql_cfgdir/mysql.cnf"
92  ;;
93esac
94
95#
96# - Do NOT purge logs or data if another mysql-sever* package is installed (#307473)
97# - Remove the mysql user only after all his owned files are purged.
98#
99if [ "$1" = "purge" -a ! \( -x /usr/sbin/mysqld -o -L /usr/sbin/mysqld \) ]; then
100  # we remove the mysql user only after all his owned files are purged
101  rm -f /var/log/mysql.{log,err}{,.0,.[1234567].gz}
102  rm -rf /var/log/mysql
103
104  db_input high mysql-@DEB_PRODUCTNAME@-server/postrm_remove_databases || true
105  db_go || true
106  db_get mysql-@DEB_PRODUCTNAME@-server/postrm_remove_databases || true
107  if [ "$RET" = "true" ]; then
108    # never remove the debian.cnf when the databases are still existing
109    # else we ran into big trouble on the next install!
110    rm -f /etc/mysql/debian.cnf
111    rm -rf /var/lib/mysql
112    rm -rf /var/lib/mysql-files
113    rm -rf /var/lib/mysql-keyring
114    userdel mysql || true
115  fi
116fi
117
118#DEBHELPER#
119
120exit 0
121