~mdcallag/+junk/5.1-map

« back to all changes in this revision

Viewing changes to debian/mysql-server.preinst.in

  • Committer: msvensson at pilot
  • Date: 2007-04-24 09:11:45 UTC
  • mfrom: (2469.1.106)
  • Revision ID: sp1r-msvensson@pilot.blaudden-20070424091145-10463
Merge pilot.blaudden:/home/msvensson/mysql/my51-m-mysql_upgrade
into  pilot.blaudden:/home/msvensson/mysql/mysql-5.1-maint

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash -e
 
2
#
 
3
# summary of how this script can be called:
 
4
#        * <new-preinst> install
 
5
#        * <new-preinst> install <old-version>
 
6
#        * <new-preinst> upgrade <old-version>
 
7
#        * <old-preinst> abort-upgrade <new-version>
 
8
#
 
9
 
 
10
if [ -n "$DEBIAN_SCRIPT_DEBUG" ]; then set -v -x; DEBIAN_SCRIPT_TRACE=1; fi
 
11
${DEBIAN_SCRIPT_TRACE:+ echo "#42#DEBUG# RUNNING $0 $*" 1>&2 }
 
12
 
 
13
export PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin
 
14
 
 
15
# Try to stop the server in a sane way. If it does not success let the admin
 
16
# do it himself. No database directories should be removed while the server
 
17
# is running! Another mysqld in e.g. a different chroot is fine for us.
 
18
stop_server() {
 
19
    if [ ! -x /etc/init.d/mysql ]; then return; fi
 
20
 
 
21
    set +e
 
22
    if [ -x /usr/sbin/invoke-rc.d ]; then
 
23
      cmd="invoke-rc.d mysql stop"
 
24
    else
 
25
      cmd="/etc/init.d/mysql stop"
 
26
    fi
 
27
    $cmd
 
28
    errno=$?
 
29
    set -e
 
30
   
 
31
    # 0=ok, 100=no init script (fresh install)
 
32
    if [ "$errno" != 0 -a "$errno" != 100 ]; then
 
33
      echo "${cmd/ */} returned $errno" 1>&2
 
34
      echo "There is a MySQL server running, but we failed in our attempts to stop it." 1>&2
 
35
      echo "Stop it yourself and try again!" 1>&2
 
36
      exit 1
 
37
    fi
 
38
}
 
39
 
 
40
##### here's a bunch of helper functions for converting database formats ######
 
41
 
 
42
cvt_get_param(){
 
43
        /usr/sbin/mysqld --print-defaults \
 
44
                | tr " " "\n" \
 
45
                | grep -- "--$1" \
 
46
                | tail -n 1 \
 
47
                | cut -d= -f2
 
48
}
 
49
 
 
50
cvt_setup_stuff(){
 
51
        mytmp=`mktemp -d -t mysql-ISAM-convert.XXXXXX`
 
52
        cvt_log="$mytmp/conversion.log"
 
53
        if [ ! -d "$mytmp" ]; then
 
54
                echo "can't create temporary directory, oh well." >&2
 
55
                exit 1
 
56
        fi
 
57
 
 
58
        chgrp mysql $mytmp
 
59
        chmod g+rwx $mytmp
 
60
        cvt_socket=${mytmp}/mysql.sock
 
61
 
 
62
        cvt_mysqld="mysqld --skip-grant-tables --skip-networking --socket $cvt_socket"
 
63
        cvt_mysql="mysql --socket $cvt_socket"
 
64
        cvt_mysqladmin="mysqladmin --socket $cvt_socket"
 
65
}
 
66
 
 
67
cvt_get_databases(){
 
68
        echo fetching database list ... >&2
 
69
        $cvt_mysql -e 'show databases' | sed -n -e '2,$p'
 
70
}
 
71
 
 
72
cvt_get_tables(){
 
73
        echo querying tables in $1 ... >&2
 
74
        $cvt_mysql $1 -e 'show table status' | sed -n -e '2,$p' | \
 
75
                cut -f 1,2 | grep -w 'ISAM$' | cut -f 1
 
76
}
 
77
 
 
78
cvt_convert_table(){
 
79
        echo converting $1.$2 ... >&2
 
80
        $cvt_mysql $1 -e "alter table $2 type=MyISAM"
 
81
}
 
82
 
 
83
cvt_wait_for_server(){
 
84
        local count
 
85
        echo -n waiting for server startup.. >&2
 
86
        while ! $cvt_mysql </dev/null >/dev/null 2>&1; do
 
87
                echo -n . >&2
 
88
                sleep 1
 
89
                count=".$count"
 
90
                if [ -f $mytmp/mysql.done ]; then
 
91
                        echo "sorry... looks like the server crashed :(" >&2
 
92
                        return 1
 
93
                elif [ "$count" = "...................." ]; then
 
94
                        echo "sorry... looks like the server didn't start :(" >&2
 
95
                        return 1
 
96
                fi
 
97
        done
 
98
        echo ok. >&2
 
99
}
 
100
 
 
101
cvt_wait_for_exit(){
 
102
        local count
 
103
        echo -n waiting for server shutdown.. >&2
 
104
        while [ ! -f $mytmp/mysql.done ]; do
 
105
                echo -n . >&2
 
106
                sleep 1
 
107
                count=".$count"
 
108
                if [ "$count" = "...................." ]; then
 
109
                        echo "hrm... guess it never started?" >&2
 
110
                        return 0
 
111
                fi
 
112
        done
 
113
        echo ok. >&2
 
114
}
 
115
 
 
116
cvt_cleanup(){
 
117
        local mysql_kids
 
118
        rm -rf $mytmp
 
119
        # kill any mysqld child processes left over.  there *shouldn't* be any,
 
120
        # but let's not take chances with that
 
121
        mysql_kids=`ps o 'pid command' --ppid $$ | grep -E '^[[:digit:]]+ mysqld ' | cut -d' ' -f1`
 
122
        if [ "$mysql_kids" ]; then
 
123
                echo "strange, some mysql processes left around. killing them now." >&2
 
124
                kill $mysql_kids
 
125
                sleep 10
 
126
                mysql_kids=`ps o 'pid command' --ppid $$ | grep -E '^[[:digit:]]+ mysqld ' | cut -d' ' -f1`
 
127
                if [ "$mysql_kids" ]; then
 
128
                        echo "okay, they're really not getting the hint..." >&2
 
129
                        kill -9 $mysql_kids
 
130
                fi
 
131
        fi
 
132
}
 
133
 
 
134
################################ main() ##########################
 
135
 
 
136
# to be sure
 
137
stop_server
 
138
 
 
139
# test if upgrading from non conffile state
 
140
if [ "$1" = "upgrade" ] && [ -x /usr/sbin/mysqld ]; then
 
141
        cvt_datadir=`cvt_get_param datadir`
 
142
        # test for ISAM tables, which we must convert NOW
 
143
        if [ -n "`find $cvt_datadir -name '*.ISM' 2>/dev/null`" ]; then
 
144
                set +e
 
145
                cat << EOF >&2
 
146
----------------------------------------
 
147
WARNING WARNING WARNING
 
148
----------------------------------------
 
149
 
 
150
It has been detected that are are using ISAM format on some of your
 
151
mysql database tables.  This format has been deprecated and no longer
 
152
supported.  to prevent these databases from essentially disappearing,
 
153
an attempt at format conversion will now be made.  please check after
 
154
your upgrade that all tables are present and accounted for.
 
155
 
 
156
apologies for the noise, but we thought you'd appreciate it :)
 
157
 
 
158
----------------------------------------
 
159
WARNING WARNING WARNING
 
160
----------------------------------------
 
161
EOF
 
162
                cvt_setup_stuff
 
163
                ($cvt_mysqld >$cvt_log 2>&1; touch $mytmp/mysql.done ) &
 
164
 
 
165
                if cvt_wait_for_server; then
 
166
                        dbs=`cvt_get_databases`
 
167
                        for db in $dbs; do
 
168
                                tables=`cvt_get_tables $db`
 
169
                                for tbl in $tables; do
 
170
                                        cvt_convert_table $db $tbl
 
171
                                done
 
172
                        done
 
173
                else
 
174
                        cvt_error="yes"
 
175
                fi
 
176
 
 
177
                echo shutting down server... >&2
 
178
                $cvt_mysqladmin shutdown
 
179
                cvt_wait_for_exit
 
180
                echo "all done!" >&2
 
181
                if [ ! "$cvt_error" = "yes" ]; then
 
182
                        cvt_cleanup
 
183
                else
 
184
                        echo "you might want to look in $mytmp..." >&2
 
185
                fi
 
186
 
 
187
                set -e
 
188
        fi
 
189
fi
 
190
 
 
191
exit 0