-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb
More file actions
executable file
·383 lines (358 loc) · 7.46 KB
/
Copy pathdb
File metadata and controls
executable file
·383 lines (358 loc) · 7.46 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/bin/bash
# Copyright 2008-2012 Attributor
#
# This file is part of DB Tools.
#
# DB Tools is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DB Tools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with DB Tools. If not, see <http://www.gnu.org/licenses/>.
set -e
zero="$(readlink -f "$0")"
zdir="$(dirname "$zero")"
confdir="$zero.conf"
function cf () {
local f
for f in "$@"
do
# these -e's are not -r's for a reason. If the file exists and is not readable by this user, it is an error.
if [ -e "$confdir/this/$f" ]
then
. "$confdir/this/$f"
elif [ -e "$confdir/default/$f" ]
then
. "$confdir/default/$f"
elif [ -e "$confdir/pool/$f" ]
then
. "$confdir/pool/$f"
else
echo "no such file $f ($confdir)" 1>&2
exit 1
fi
done
}
cf start
cf die
# initialization directives, such as for authentication
case "$1" in
(auth-cli|auth_cli)
shift
auth_cli "$1" ; shift
;;
(auth-env|auth_env)
shift
auth_env "$1" ; shift
;;
(auth-set|auth_set)
shift
auth_set "$1" ; shift
;;
(auth-kill|auth_kill|deauth)
shift
auth_kill
;;
(auth)
shift
auth_env "$1" ; shift
;;
esac
if [ -z "$1" ]
then
exit
fi
# meta directives, such as db discovery or specification of a remote host.
# if none of the explicit meta directives are matched, the arg is assumed to be the tag or port of a db.
case "$1" in
(--compgen)
echo auth auth-cli auth-env auth-set auth-kill deauth
echo show-auth remote local ls tag port port-tag tag-port
self tag
exit
;;
(show-auth|auth-show)
echo --user="$DBUSER" --password="$DBPASSWORD"
# example: eval "export $( db auth foo - show-auth )"
exit
;;
(remote) # as in cli will be told --host foo
die "$1 not implemented"
;;
(remotely) # as in ssh to host, then execute again as if local
die "$1 not implemented"
# don't forget the intricacies of -t -tt -T etc
;;
('local')
shift
db_set "$1"
shift
;;
(tag|tags|ls)
db_find_tags
exit
;;
(port|ports)
db_find_ports
exit
;;
(port-tag|ports-tags|tag-port|tags-ports)
db_find_ports_tags
exit
;;
(*)
db_set "$1"
shift
;;
esac
arg="$1" ; shift
case "$arg" in
(--compgen)
echo info cli tag port socket
echo ls db
echo pw psw lsps pshost kill
echo slavestatus ss ss-behind sss ss-behind-to-zero SSS ss-behind-continual SSS-nostop
echo mycnf mysql_version vsmy
echo budump fulldump fulldump-no-routines dump mysqldump import admin mysqladmin
# self "$TAG" ls "$@"
if [ -d "$maatkit_bindir"/ ]
then
ls "$maatkit_bindir"/
fi
if [ -d "$percona_toolkit_bindir"/ ]
then
ls "$percona_toolkit_bindir"/
fi
exit
;;
(info) echo TAG:$TAG MYCNF:$MYCNF SOCKET:$SOCKET PORT:$PORT ;;
(cli) if [ "$1" = --compgen ]
then
shift
ls_dbs "$@"
else
cli_mysql exec "$@"
fi
exit # redundant, but helps readability
;;
(tag) echo "$TAG" ; exit ;;
(port) echo "$PORT" ; exit ;;
(socket) echo "$SOCKET" ; exit ;;
(ls_dbs|ls-dbs)
ls_dbs "$@"
exit
;;
(ls_tables|ls-tables) # note the plurality
ls_tables "$@"
exit
;;
(ls_table|ls-table) # note the singlularity
ls_table "$@"
exit
;;
(ls) # dispatch to proper ls_* function
if [ "$1" = --compgen ]
then
self "$TAG" ls
exit
elif [ "$2" = --compgen ]
then
self "$TAG" ls "$1"
exit
fi
if [ -z "$1" ] # no arg
then
ls_dbs "$@"
elif [ "${1#-}" != "$1" ] # arg starts with a dash
then
ls_dbs "$@"
elif [ -n "$1" -a -z "$2" ] # one arg
then
ls_tables "$@"
elif [ -n "$1" -a "${2#-}" != "$2" ] # one arg, but second starts with a dash
then
ls_tables "$@"
elif [ -n "$1" -a -n "$2" ] # two real args
then
ls_table "$@"
fi
exit
;;
(ps) cli_mysql exec "$@" -e 'show processlist'
;;
(psw) cli_mysql exec "$@" -e 'show full processlist'
;;
#
(lsps) cli_mysql exec "$@" -e 'show full processlist' | grep -v 'Sleep' |grep -v Binlog ;;
#
(pshost)
cli_mysql exec "$@" -e 'show full processlist' \
| awk '{print $3}'|sed 's/:.*// ; s/[.].*$//' \
| grep -v Host | grep -v '|' | sort -f |uniq -c | sort -n
;;
(mycnf)
if [ $# -eq 0 ]
then
echo "$MYCNF"
# elif compgen?
else
for x in "$@"
do
extract_from_mycnf "$MYCNF" "$x"
done
fi
exit
;;
#
(admin|mysqladmin) cli_mysql mysqladmin "$@" ;;
#
(import) cli_mysql mysqlimport "$@" ;;
#
(mysqldump) cli_mysql mysqldump "$@" ;;
#
(dump) cli_mysql mysqldump --skip-lock-tables "$@" ;;
#
(fulldump-no-routines) cli_mysql mysqldump --skip-lock-tables --hex-blob --add-drop-database "$@" ;;
#
(fulldump) cli_mysql mysqldump --routines --skip-lock-tables --hex-blob --add-drop-database "$@" ;;
#
(budump)
# $1 is assumed to be the name of the db to dump
TS=$(date +%F_%H%M%S) # not %T because ":" means something to [sr]cp
buname="$1-$TS.sql"
self "$TAG" fulldump --databases "$@" > "$buname"
bzip2 -9v "$buname"
;;
#
(db)
if [ "$1" = --compgen ]
then
self "$TAG" ls
exit
fi
db="$1" ; shift
ls_db "$db"
exit
;;
#
(kill)
for x in "$@"
do
if [ "$x" = --compgen ]
then
self "$TAG" ps | awk '{print $1}' | grep -v ^Id
exit
fi
done
tid=$( echo "$@" | sed 's/ /,/g' ) # gets awkward if -u or -p is specified
cli_mysql exec mysqladmin "$@" kill "$tid"
;;
#
(mysql_version|vsmy)
cli_mysql "$@" -e 'select version()' |skip_first_line
;;
(slavestatus|ss)
cli_mysql "$@" -e 'show slave status\G'
;;
(ss-behind|sss)
j=$( cli_mysql "$@" -e 'show slave status\G' | grep '^ *Seconds_Behind_Master:' | sed 's/.*: *//' )
if [ "$j" = NULL ]
then
echo NULL
echo NULL
else
echo "$j p 3600 4k/p" |dc
fi
;;
(ss-behind-to-zero|SSS)
# tell how far behind is this slave. like sss, but repeatedly.
X=5
if [ -n "$1" ]
then
X="$1"
shift
fi
j=1
while [ "$j" != NULL -a "$j" != 0 ]
do
j=$( cli_mysql "$@" -e 'show slave status\G' | grep '^ *Seconds_Behind_Master:' | sed 's/.*: *//' )
if [ "$j" = 0 ]
then
echo -ne '\r'
tput el
echo 0
elif [ "$j" = NULL ]
then
echo -ne '\r'
tput el
echo NULL
else
echo -ne '\r'
tput el
d=$(echo "$j 3600 4k/ 24/p" |dc)
h=$(echo "$j 3600 4k/p" |dc)
echo -n "${j}s -- ${h}h -- ${d}d"
echo -n " -- $( date -d @$( echo "$(date +%s) $j +p" |dc ) )"
if [ $j -gt 0 ]
then
sleep $X
fi
fi
done
exit
;;
(ss-behind-continual|SSS-nostop)
# tell how far behind is this slave. like sss, but repeatedly.
X=5
if [ -n "$1" ]
then
X="$1"
shift
fi
j=1
while true
do
j=$( cli_mysql "$@" -e 'show slave status\G' | grep '^ *Seconds_Behind_Master:' | sed 's/.*: *//' )
echo -ne '\r'
tput el
if [ "$j" = NULL ]
then
echo -n NULL
elif [ "$j" -eq 0 ]
then
echo -n "$j"s
else
echo -n "$j"s -- $(echo "$j 3600 4k/p" |dc)'h'
fi
sleep $X
done
exit
;;
#
(mk-*)
if [ "$1" = --compgen ]
then
"$maatkit_bindir/$arg" --help 1>&2
exit
fi
cli_mysql maatkit "$arg" "$@"
;;
#
(pt-*)
if [ "$1" = --compgen ]
then
"$percona_toolkit_bindir/$arg" --help 1>&2
exit
fi
cli_mysql percona_toolkit "$arg" "$@"
;;
(*)
exit 1
;;
esac