summaryrefslogtreecommitdiff
path: root/scripts/mysql_upgrade_shell.sh
blob: c9f375b6c5bb18fc6c39fb514f049a93ddb43d58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/sh
# Copyright (C) 2002-2003 MySQL AB
# For a more info consult the file COPYRIGHT distributed with this file.

# Runs mysqlcheck --check-upgrade in case it has not been done on this
# major MySQL version

# This script should always be run when upgrading from one major version
# to another (ie: 4.1 -> 5.0 -> 5.1)

#
# Note that in most cases one have to use '--password' as
# arguments as these needs to be passed on to the mysqlcheck command


user=root

case "$1" in
    --no-defaults|--defaults-file=*|--defaults-extra-file=*)
      defaults="$1"; shift
      ;;
esac

parse_arguments() {
  # We only need to pass arguments through to the server if we don't
  # handle them here.  So, we collect unrecognized options (passed on
  # the command line) into the args variable.
  pick_args=
  if test "$1" = PICK-ARGS-FROM-ARGV
  then
    pick_args=1
    shift
  fi

  for arg do
    case "$arg" in
      --basedir=*) MY_BASEDIR_VERSION=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --user=*) user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --ldata=*|--data=*|--datadir=*) DATADIR=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --force) force=1 ;;
      --verbose) verbose=1 ;;
      --help) help_option=1 ;;
      *)
        if test -n "$pick_args"
        then
          # This sed command makes sure that any special chars are quoted,
          # so the arg gets passed exactly to the server.
          args="$args "`echo "$arg" | sed -e 's,\([^a-zA-Z0-9_.=-]\),\\\\\1,g'`
        fi
        ;;
    esac
  done
}

#
# Find where my_print_defaults is
#

find_my_print_defaults () {
  if test -x ./bin/my_print_defaults
  then
    print_defaults="./bin/my_print_defaults"
  elif test -x ./extra/my_print_defaults
  then
    print_defaults="./extra/my_print_defaults"
  elif test -x @bindir@/my_print_defaults
  then
    print_defaults="@bindir@/my_print_defaults"
  elif test -x @bindir@/mysql_print_defaults
  then
    print_defaults="@bindir@/mysql_print_defaults"
  else
    print_defaults="my_print_defaults"
  fi
}

find_my_print_defaults

# Get first arguments from the my.cfg file, groups [mysqld] and
# [mysql_upgrade], and then merge with the command line arguments

args=
DATADIR=
bindir=
MY_BASEDIR_VERSION=
verbose=0
force=0
help_option=0

parse_arguments `$print_defaults $defaults mysqld mysql_upgrade`
parse_arguments PICK-ARGS-FROM-ARGV "$@"

if test $help_option = 1
then
  echo "MySQL utility script to upgrade database to the current server version"
  echo ""
  echo "It takes the following arguments:"
  echo "  --help     Show this help message"
  echo "  --basedir  Specifies the directory where MySQL is installed"
  echo "  --datadir  Specifies the data directory"
  echo "  --force    Mysql_upgrade.info file will be ignored"
  echo "  --user     Username for server login if not current user"
  echo "  --verbose  Display more output about the process"
  echo ""

  exit 0
fi

#
# Try to find where binaries are installed
#

MY_PWD=`pwd`
# Check for the directories we would expect from a binary release install
if test -z "$MY_BASEDIR_VERSION"
then
  if test -f ./share/mysql/english/errmsg.sys -a -x ./bin/mysqld
  then
    MY_BASEDIR_VERSION=$MY_PWD            # Where bin, share and data are
    bindir="$MY_BASEDIR_VERSION/bin"
  # Check for the directories we would expect from a source install
  elif test -f ./share/mysql/english/errmsg.sys -a -x ./libexec/mysqld
  then
    MY_BASEDIR_VERSION=$MY_PWD            # Where libexec, share and var are
    bindir="$MY_BASEDIR_VERSION/bin"
# Since we didn't find anything, used the compiled-in defaults
  else
    MY_BASEDIR_VERSION=@prefix@
    bindir=@bindir@
  fi
else
  bindir="$MY_BASEDIR_VERSION/bin"
fi

#
# Try to find the data directory
#

if test -z "$DATADIR"
then
  # Try where the binary installs put it
  if test -d $MY_BASEDIR_VERSION/data/mysql
  then
    DATADIR=$MY_BASEDIR_VERSION/data
  # Next try where the source installs put it
  elif test -d $MY_BASEDIR_VERSION/var/mysql
  then
    DATADIR=$MY_BASEDIR_VERSION/var
  # Or just give up and use our compiled-in default
  else
    DATADIR=@localstatedir@
  fi
fi

if test ! -x "$bindir/mysqlcheck"
then
  echo "Can't find program '$bindir/mysqlcheck'"
  echo "Please restart with --basedir=mysql-install-directory"
  exit 1
fi

if test ! -f "$DATADIR/mysql/user.frm"
then
  echo "Can't find data directory. Please restart with --datadir=path-to-data-dir"
  exit 1
fi

CHECK_FILE=$DATADIR/mysql_upgrade.info

if test -f $CHECK_FILE -a $force = 0
then
  version=`cat $CHECK_FILE`
  if test "$version" = "@MYSQL_BASE_VERSION@"
  then
    if test $verbose = 1
    then
       echo "mysql_upgrade already done for this version"
    fi
    $bindir/mysql_fix_privilege_tables --silent $args
    exit 0
  fi
fi

#
# Run the upgrade
#

check_args="--check-upgrade --all-databases --auto-repair --user=$user"

if test $verbose = 1
then
  echo "Running $bindir/mysqlcheck $args $check_args"
fi

$bindir/mysqlcheck $check_args $args
if [ $? = 0 ]
then
  # Remember base version so that we don't run this script again on the
  # same base version
  echo "@MYSQL_BASE_VERSION@" > $CHECK_FILE
fi

$bindir/mysql_fix_privilege_tables --silent --user=$user $args