~mdcallag/+junk/5.1-map

« back to all changes in this revision

Viewing changes to debian/mysql-common.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
start_server() {
 
41
    if [ ! -x /etc/init.d/mysql ]; then return; fi
 
42
 
 
43
    set +e
 
44
    if [ -x /usr/sbin/invoke-rc.d ]; then
 
45
      cmd="invoke-rc.d mysql start"
 
46
    else
 
47
      cmd="/etc/init.d/mysql start"
 
48
    fi
 
49
    $cmd
 
50
    set -e
 
51
}
 
52
 
 
53
##### here's a bunch of helper functions for converting database formats ######
 
54
 
 
55
cvt_get_param(){
 
56
        /usr/sbin/mysqld --print-defaults \
 
57
                | tr " " "\n" \
 
58
                | grep -- "--$1" \
 
59
                | tail -n 1 \
 
60
                | cut -d= -f2
 
61
}
 
62
 
 
63
cvt_setup_stuff(){
 
64
        mytmp=`mktemp -d -t mysql-ISAM-convert.XXXXXX`
 
65
        cvt_log="$mytmp/conversion.log"
 
66
        if [ ! -d "$mytmp" ]; then
 
67
                echo "can't create temporary directory, oh well." >&2
 
68
                exit 1
 
69
        fi
 
70
 
 
71
        chgrp mysql $mytmp
 
72
        chmod g+rwx $mytmp
 
73
        cvt_socket=${mytmp}/mysql.sock
 
74
 
 
75
        cvt_mysqld="mysqld --skip-grant-tables --skip-networking --socket $cvt_socket"
 
76
        cvt_mysql="mysql --socket $cvt_socket"
 
77
        cvt_mysqladmin="mysqladmin --socket $cvt_socket"
 
78
}
 
79
 
 
80
cvt_get_databases(){
 
81
        echo fetching database list ... >&2
 
82
        $cvt_mysql -e 'show databases' | sed -n -e '2,$p'
 
83
}
 
84
 
 
85
cvt_get_tables(){
 
86
        echo querying tables in $1 ... >&2
 
87
        $cvt_mysql $1 -e 'show table status' | sed -n -e '2,$p' | \
 
88
                cut -f 1,2 | grep -w 'ISAM$' | cut -f 1
 
89
}
 
90
 
 
91
cvt_convert_table(){
 
92
        echo converting $1.$2 ... >&2
 
93
        $cvt_mysql $1 -e "alter table $2 type=MyISAM"
 
94
}
 
95
 
 
96
cvt_wait_for_server(){
 
97
        local count
 
98
        echo -n waiting for server startup.. >&2
 
99
        while ! $cvt_mysql </dev/null >/dev/null 2>&1; do
 
100
                echo -n . >&2
 
101
                sleep 1
 
102
                count=".$count"
 
103
                if [ -f $mytmp/mysql.done ]; then
 
104
                        echo "sorry... looks like the server crashed :(" >&2
 
105
                        return 1
 
106
                elif [ "$count" = "...................." ]; then
 
107
                        echo "sorry... looks like the server didn't start :(" >&2
 
108
                        return 1
 
109
                fi
 
110
        done
 
111
        echo ok. >&2
 
112
}
 
113
 
 
114
cvt_wait_for_exit(){
 
115
        local count
 
116
        echo -n waiting for server shutdown.. >&2
 
117
        while [ ! -f $mytmp/mysql.done ]; do
 
118
                echo -n . >&2
 
119
                sleep 1
 
120
                count=".$count"
 
121
                if [ "$count" = "...................." ]; then
 
122
                        echo "hrm... guess it never started?" >&2
 
123
                        return 0
 
124
                fi
 
125
        done
 
126
        echo ok. >&2
 
127
}
 
128
 
 
129
cvt_cleanup(){
 
130
        local mysql_kids
 
131
        rm -rf $mytmp
 
132
        # kill any mysqld child processes left over.  there *shouldn't* be any,
 
133
        # but let's not take chances with that
 
134
        mysql_kids=`ps o 'pid command' --ppid $$ | grep -E '^[[:digit:]]+ mysqld ' | cut -d' ' -f1`
 
135
        if [ "$mysql_kids" ]; then
 
136
                echo "strange, some mysql processes left around. killing them now." >&2
 
137
                kill $mysql_kids
 
138
                sleep 10
 
139
                mysql_kids=`ps o 'pid command' --ppid $$ | grep -E '^[[:digit:]]+ mysqld ' | cut -d' ' -f1`
 
140
                if [ "$mysql_kids" ]; then
 
141
                        echo "okay, they're really not getting the hint..." >&2
 
142
                        kill -9 $mysql_kids
 
143
                fi
 
144
        fi
 
145
}
 
146
 
 
147
################################ main() ##########################
 
148
 
 
149
# test if upgrading from non conffile state
 
150
if [ "$1" = "upgrade" ] && [ -x /usr/sbin/mysqld ]; then
 
151
        cvt_datadir=`cvt_get_param datadir`
 
152
        # test for ISAM tables, which we must convert NOW
 
153
        if [ -n "`find $cvt_datadir -name '*.ISM' 2>/dev/null`" ]; then
 
154
                pidfile=`cvt_get_param pid-file`
 
155
                if [ "$pidfile" ] && [ -f "$pidfile" ]; then
 
156
                        server_pid=`cat $pidfile`
 
157
                        if [ "$server_pid" ] && ps $server_pid >/dev/null 2>&1; then
 
158
                                server_running="yes"
 
159
                        fi
 
160
                fi
 
161
                # to be sure
 
162
                stop_server
 
163
 
 
164
                set +e
 
165
                cat << EOF >&2
 
166
----------------------------------------
 
167
WARNING WARNING WARNING
 
168
----------------------------------------
 
169
 
 
170
It has been detected that are are using ISAM format on some of your
 
171
mysql database tables.  This format has been deprecated and no longer
 
172
supported.  to prevent these databases from essentially disappearing,
 
173
an attempt at format conversion will now be made.  please check after
 
174
your upgrade that all tables are present and accounted for.
 
175
 
 
176
apologies for the noise, but we thought you'd appreciate it :)
 
177
 
 
178
----------------------------------------
 
179
WARNING WARNING WARNING
 
180
----------------------------------------
 
181
EOF
 
182
                cvt_setup_stuff
 
183
                ($cvt_mysqld >$cvt_log 2>&1; touch $mytmp/mysql.done ) &
 
184
 
 
185
                if cvt_wait_for_server; then
 
186
                        dbs=`cvt_get_databases`
 
187
                        for db in $dbs; do
 
188
                                tables=`cvt_get_tables $db`
 
189
                                for tbl in $tables; do
 
190
                                        cvt_convert_table $db $tbl
 
191
                                done
 
192
                        done
 
193
                else
 
194
                        cvt_error="yes"
 
195
                fi
 
196
 
 
197
                echo shutting down server... >&2
 
198
                $cvt_mysqladmin shutdown
 
199
                cvt_wait_for_exit
 
200
                echo "all done!" >&2
 
201
                if [ ! "$cvt_error" = "yes" ]; then
 
202
                        cvt_cleanup
 
203
                else
 
204
                        echo "you might want to look in $mytmp..." >&2
 
205
                fi
 
206
 
 
207
                if [ "$server_running" ]; then
 
208
                        start_server
 
209
                fi
 
210
 
 
211
                set -e
 
212
        fi
 
213
fi
 
214
 
 
215
exit 0