forked from skeyby/mysqlfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqlfs_setup
executable file
·207 lines (180 loc) · 5.65 KB
/
mysqlfs_setup
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
204
205
206
207
#!/bin/sh
##
## MySQLfs - kickoff and upgrade scripts
##
## Requirements:
## - rely on the presence of the tree table
## to understand if this is a new setup
## - rely on a "DATABASE_VERSION" table inside the database
## to understand what's the upgrade starting point.
## - requires mysql client to be in the path...
##
## if it can't find the table it asks you if you want to create it
##
##
DBUpdateScripts="/usr/local/share/mysqlfs/sql/update"
clear
echo
echo Welcome to MySQLfs database setup script - relase 1.0
echo
echo
echo ============================================================
echo WARNING:
echo "If you're upgrading, it's recommend to shutdown MySQLfs's"
echo "instance before upgrading the DB."
echo Upgrading the DB with an active instance could corrupt your data.
echo ============================================================
echo
echo
echo In order to setup/upgrade your DB we need some informations.
echo
echo "Please insert your DB host (leave blank for localhost):"
printf "#> "
read DBHost
if [ "$DBHost" = "" ]; then
DBHost="localhost"
fi
echo "Please insert your MySQLfs DB name (leave blank for mysqlfs):"
printf "#> "
read DBName
if [ "$DBName" = "" ]; then
DBName="mysqlfs"
fi
echo "Please insert your MySQLfs username (leave blank for mysqlfs):"
printf "#> "
read DBUser
if [ "$DBUser" = "" ]; then
DBUser="mysqlfs"
fi
echo "Please insert your MySQLfs password (leave blank for mysqlfs):"
printf "#> "
read DBPass
if [ "$DBPass" = "" ]; then
DBPass="mysqlfs"
fi
echo
echo Please confirm the following settings:
echo mysql://$DBUser:$DBPass@$DBHost/$DBName
echo
echo "Correct? (y/n)"
printf "#> "
read Correct
if [ "$Correct" != "y" ]; then
echo
echo Aborting setup. Please restart $0 to retry.
echo -------------------------------------------
echo
exit 1
fi
echo
echo Checking if we already a database...
TableExists=`echo "SHOW TABLES LIKE 'tree';" | mysql -N -h $DBHost -u $DBUser --password=$DBPass $DBName`
if [ "$TableExists" != "tree" ]; then
echo "A 'tree' table doesn't seem to exist in your database."
echo
echo "If this your first installation this is normal."
echo
echo "Continue creating a new database? (y/n)"
printf "#> "
read Correct
if [ "$Correct" != "y" ]; then
echo
echo Aborting setup. Please restart $0 to retry.
echo -------------------------------------------
echo
exit 1
fi
echo Executing $DBUpdateScripts/initial_schema.sql
mysql -N -h $DBHost -u $DBUser --password=$DBPass $DBName < $DBUpdateScripts/initial_schema.sql > /tmp/dbupdate_stdout.log 2> /tmp/dbupdate_stderr.log
ErrorLevel=$?
if [ $ErrorLevel -ne 0 ]; then
echo
echo Error creating the initial schema.
echo Aborting setup. Please restart $0 to retry.
echo -------------------------------------------
echo
exit 1
else
echo
echo Initial database setup succeeded!
echo
fi
echo
fi
echo
echo Checking for the DATABASE_VERSION table...
TableExists=`echo "SHOW TABLES LIKE 'DATABASE_VERSION';" | mysql -N -h $DBHost -u $DBUser --password=$DBPass $DBName`
if [ "$TableExists" != "DATABASE_VERSION" ]; then
echo "A 'DATABASE_VERSION' table doesn't seem to exist in your database."
echo
echo If this is your first database upgrade ever this may be normal.
echo
echo PLEASE BE AWARE THAT THIS SCRIPT REQUIRE YOUR PREVIOUS MYSQLFS
echo RELEASE TO BE AT LEAST 0.4.1!!!!!!
echo
echo "Continue creating DATABASE_VERSION? (y/n)"
printf "#> "
read Correct
if [ "$Correct" != "y" ]; then
echo
echo Aborting setup. Please restart $0 to retry.
echo -------------------------------------------
echo
exit 1
fi
echo Executing $DBUpdateScripts/00000000.sql
mysql -N -h $DBHost -u $DBUser --password=$DBPass $DBName < $DBUpdateScripts/00000000.sql > /tmp/dbupdate_stdout.log 2> /tmp/dbupdate_stderr.log
ErrorLevel=$?
if [ $ErrorLevel -ne 0 ]; then
echo
echo Error creating the DATABASE_VERSION table
echo Aborting setup. Please restart $0 to retry.
echo -------------------------------------------
echo
exit 1
else
echo
echo DATABASE_VERSION succesfully created.
echo
fi
echo
fi
echo
echo Checking current DATABASE_VERSION:
CurrentDB=`echo "SELECT MAX(CURRENT_VERSION) FROM DATABASE_VERSION;" | mysql -N -h $DBHost -u $DBUser --password=$DBPass $DBName`
if [ "$CurrentDB" = "NULL" ]; then
CurrentDB=0
fi
echo Current database version $CurrentDB
echo
NextDB=`expr $CurrentDB + 1`
NextFile=`echo 0000000$NextDB | rev | cut -c 1-8 | rev`
echo "Searching for next DB upgrades (starting from #$NextDB)"
while [ -f $DBUpdateScripts/$NextFile.sql ]; do
echo "Found $DBUpdateScripts/$NextFile.sql - executing it"
mysql -N -h $DBHost -u $DBUser --password=$DBPass $DBName < $DBUpdateScripts/$NextFile.sql > /tmp/dbupdate_stdout.log 2> /tmp/dbupdate_stderr.log
ErrorLevel=$?
if [ $ErrorLevel -ne 0 ]; then
echo "Error applying update $NextDB"
cat /tmp/dbupdate_stdout.log
rm /tmp/dbupdate_stdout.log
cat /tmp/dbupdate_stderr.log
rm /tmp/dbupdate_stderr.log
exit 1
else
echo "INSERT INTO DATABASE_VERSION SET CURRENT_VERSION = $NextDB, LAST_CHANGE=NOW();" | mysql -N -h $DBHost -u $DBUser --password=$DBPass $DBName
echo "Updating DATABASE_VERSION to version $NextDB"
fi
rm /tmp/dbupdate_std*.log
NextDB=`expr $NextDB + 1`
NextFile=`echo 0000000$NextDB | rev | cut -c 1-8 | rev`
done
echo
echo Everything done.
echo
echo Now you can upgrade your MySQLfs binaries and start/restart your filesystem
echo by running command
echo
echo mysql://$DBUser:$DBPass@$DBHost/$DBName
echo "#> mysqlfs -oallow_other -ohost=$DBHost -odatabase=$DBName -ouser=$DBUser -opassword=$DBPass -obig_writes /mountpoint"
echo