blob: cc40981b9ff32ebabf0bc73109759190fd0a244a (
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
#!/bin/sh
#
# Update XXV's database
#
# Usage: update-xxv [OPTIONS]
#
# Options: -b <target> : Make a backup first to <target>.gz
# -h <hostname> : Host name of the MySQL server (default: localhost)
# -P <port> : Port number of MySQL server
# -d <database> : XXV database name (default: xxv)
# -u <user> : Username (default: root)
# -p <password> : Password (default: no password)
# -a : Ask for password (once every run of mysql)
# -A : Ask for password (just once)
# -f : Force upgrade, do not check version
# -v : Display actual Version
# -h : Help
# Increment the version number each time the database changes!
#
actualVersion=32
# Specify tables to export before and import after update
#
tables='AUTOTIMER CHRONICLE MEDIALIB_ACTORS MEDIALIB_VIDEODATA MEDIALIB_VIDEOGENRE MOVETIMER USER XMLTV RECORDER'
showTitle()
{
echo
echo 'update-xxv'
echo '----------'
echo
}
showHelp()
{
echo 'Usage: update-xxv [-b] [-H hostname] [-P port] [-d database]'
echo ' [-u user] [-p password]'
echo
echo '-b <target> : Make a backup first to <target>.gz'
echo '-H <hostname> : Host name of the MySQL server (default: localhost)'
echo '-P <port> : Port number of MySQL server'
echo '-d <database> : XXV database name (default: xxv)'
echo '-u <user> : Username (default: root)'
echo '-p <password> : Password (default: no password)'
echo '-s <sql-file> : Upgrade sql script (default: upgrade-xxv-db.sql)'
echo '-a : Ask for password (once every run of mysql)'
echo '-A : Ask for password (just once)'
echo '-f : Force upgrade, do not check version'
echo '-v : Display actual version'
echo '-h : Help'
}
showCommandLineHint()
{
echo
echo 'Hint: You might need to overwrite the defaults for the database'
echo 'server, the database name or the user name and password.'
echo
echo 'See update-xxv -h for a list of available options.'
echo
}
showError()
{
echo "Error: $*" >&2
}
showInfo()
{
echo "Info: $*"
}
setDefaults()
{
[ -z $host ] && host='localhost'
[ -z $database ] && database='xxv'
[ -z $user ] && user='root'
[ -z $isForceUpdate ] && isForceUpdate='false'
[ -z $askPassword ] && askPassword='false'
[ -z $upgrade ] && upgrade='upgrade-xxv-db.sql'
}
buildMysqlParams()
{
mysqlParams="--host=$host --user=$user"
if [ $askPassword == 'true' ] ; then
mysqlParams="$mysqlParams -p"
else
if [ -n "$password" ] ; then
mysqlParams="$mysqlParams --password='$password'"
fi
fi
if [ -n "$port" ] ; then
mysqlParams="$mysqlParams --port=$port"
fi
}
checkXxv()
{
if pgrep xxvd >/dev/null 2>&1 ; then
showError 'XXV is still running, please stop XXV first!'
exit 1
fi
}
checkFiles()
{
if [ ! -e "$upgrade" ]; then
showError "File '$upgrade' not found!"
exit 1
fi
}
checkServer()
{
showInfo 'Checking Server'
if ! eval mysql $mysqlParams </dev/null >/dev/null 2>&1 ; then
showError "Can not connect to mysql server on '$host' !"
showCommandLineHint
exit 1
fi
}
checkDatabase()
{
showInfo 'Checking database'
if ! eval mysql $mysqlParams -f -B -e '"show databases;"' | \
grep -e "^$database$" > /dev/null 2>&1 ; then
showError "Can not access database '$database'"
showCommandLineHint
exit 1
fi
}
exportDb()
{
showInfo 'Export data'
for table in $tables ; do
savFile="$exportDir/$table.sav"
if ! eval mysqldump $mysqlParams -n -t -c -r "$savFile" "$database" "$table" ; then
showError "Can't save table '$table'"
else
if ! eval mysql $mysqlParams -f -B $database -e \
"\"DROP TABLE IF EXISTS $table;\"" ; then
showError "Can't drop table '$table'"
exit 1
fi
fi
done
}
importDb()
{
showInfo 'Restoring data'
for table in $tables ; do
savFile="$exportDir/$table.sav"
if [ -e "$savFile" ]; then
if ! eval mysql $mysqlParams -f -B $database < $savFile ; then
showError "Can't load table '$table'"
exit 1
fi
fi
done
}
updateDb()
{
showInfo 'Updating database'
if ! eval mysql $mysqlParams -f -B $database < $upgrade ; then
showError 'Update failed!' ;
exit 1
fi
}
getActualVersion()
{
showInfo "Retrieving actual version: '$actualVersion'";
}
getVersion()
{
minver=10000000;
maxver=0;
showInfo 'Retrieving current version'
for table in $tables ; do
comment=`eval mysql $mysqlParams -f -B -s $database -e \
"\"SHOW TABLE STATUS LIKE '$table';\"" 2>/dev/null | cut -f 18`
if echo $comment | egrep -q '^[0-9]+$' ; then
if [ "$comment" -lt "$minver" ] ; then
minver="$comment"
fi
if [ "$comment" -gt "$maxver" ] ; then
maxver="$comment"
fi
else
minver="0"
fi
done
if [ "$minver" -ne "$maxver" ] ; then
showInfo "Mixed versions of tables: '$minver' until '$maxver'";
fi
if [ "$minver" -ne 10000000 ] ; then
version=$minver
fi
}
updateVersion()
{
showInfo 'Updateing version'
for table in $tables ; do
if ! eval mysql $mysqlParams -f -B $database -e \
"\"ALTER TABLE $table COMMENT '$actualVersion';\"" ; then
showError "Can't alter table '$table'"
exit 1
fi
done
}
#
# main()
#
showTitle
# parse parameters
while [ $# -ge 1 ] ; do
case $1 in
-b)
backupTarget=$2 ; shift
;;
-H)
host=$2 ; shift
;;
-P)
port=$2 ; shift
;;
-d)
database=$2 ; shift
;;
-u)
user=$2 ; shift
;;
-p)
password=$2 ; shift
;;
-s)
$upgrade=$2 ; shift
;;
-a)
askPassword='true'
;;
-A)
read -s -p 'Enter Password (hidden): ' password
;;
-f)
isForceUpdate='true'
;;
-v)
getActualVersion
exit 1;
;;
*)
showHelp
exit 1
;;
esac
shift
done
# check if XXV is still running
checkXxv
# set defaults and build mysql command line
setDefaults
buildMysqlParams
# check needed files
checkFiles
# check the database
checkServer
checkDatabase
# retrieve version and check if update should be performed
getVersion
[ -z $version ] && version=0
showInfo "current database version: $version"
showInfo "new database version: $actualVersion"
if [ $actualVersion -eq $version ] ; then
showInfo 'Database is up to date - no need to update'
if [ $isForceUpdate == 'true' ] ;then
showInfo 'As requested, the update will be forced!'
else
exit 0
fi
fi
if [ $actualVersion -lt $version ] ; then
showError 'Database is more actual than the version you want to update to.'
showError 'Database downgrades are not advisable!'
if [ $isForceUpdate == 'true' ] ;then
showInfo 'As requested, a downgrade will be forced anyway!'
else
exit 1
fi
fi
exportDir=/tmp/xxv-update.$$
mkdir $exportDir
chmod a+rwx $exportDir
# backup
if [ -z $backupTarget ] ; then
backupTarget=$exportDir/backup.sql
fi
showInfo "Performing backup to $backupTarget"
if ! eval mysqldump $mysqlParams -r "$backupTarget" "$database" ; then
showError 'Backup failed!!!'
exit 1
fi
# now actually perform the update
showInfo 'Starting update...'
exportDb
updateDb
importDb
updateVersion
showInfo 'Update successful'
rm -rf $exportDir
|