-
Notifications
You must be signed in to change notification settings - Fork 8
/
shrink
executable file
·348 lines (318 loc) · 8.91 KB
/
shrink
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
#!/bin/bash
# Checked by https://www.shellcheck.net/
# Constants
MYSQL_USER=root
# If no password is defined, then you'll be prompted for one if necessary
#MYSQL_PASS=
# Path to the mysql databases:
MYSQL_DATA_DIR=/var/lib/mysql
# Functions
prompt_yn() { # params: message
msg="$*"
if [ -z "$msg" ]; then
msg='Are you sure?'
fi
result=-1
while true; do
read -rp "$msg [yes/no] " result
case $result in
[Yy]* ) result=0; break;;
[Nn]* ) result=1; break;;
*) echo "Please answer yes or no.";;
esac
done
return $result
}
warn() { # params: message
echo "$@" >&2
}
die() { # params: exit code; message
rc="$1"
shift
warn "$@"
exit "$rc"
}
service_mysql_is_running() {
if [ -n "$(command -v service)" ]; then
if service mysql status > /dev/null; then
return 0
fi
return 1
elif [ -f /etc/init.d/mysql ]; then
if /etc/init.d/mysql status > /dev/null; then
return 0
fi
return 1
elif [ -f /usr/local/etc/rc.d/mysql ]; then
if /usr/local/etc/rc.d/mysql status > /dev/null; then
return 0
fi
return 1
else
#die 1 "Fix me: I don't know how to determine if mysql is running";
if [ -z "$(pidof mysqld)" ]; then
return 0
fi
return 1
fi
}
service_mysql_start() {
printf 'Starting mysql service... '
if [ -n "$(command -v service)" ]; then
if ! service mysql start; then
die $? 'ERROR: Failed to start mysql service!'
fi
elif [ -f /etc/init.d/mysql ]; then
if ! /etc/init.d/mysql start > /dev/null; then
die $? 'ERROR: Failed to start mysql service!'
fi
elif [ -f /usr/local/etc/rc.d/mysql ]; then # FreeBSD
if ! /usr/local/etc/rc.d/mysql start > /dev/null; then
die $? 'ERROR: Failed to start mysql service!'
fi
else
die 1 "Fix me: I don't know how to start mysql";
fi
echo 'OK'
}
service_mysql_stop() {
printf 'Stopping mysql service... '
if [ -n "$(command -v service)" ]; then
if ! service mysql stop; then
die $? 'ERROR: Failed to stop mysql service!'
fi
elif [ -f /etc/init.d/mysql ]; then
if ! /etc/init.d/mysql stop > /dev/null; then
die $? 'ERROR: Failed to stop mysql service!'
fi
elif [ -f /usr/local/etc/rc.d/mysql ]; then # FreeBSD
if ! /usr/local/etc/rc.d/mysql stop > /dev/null; then
die $? 'ERROR: Failed to stop mysql service!'
fi
else
die 1 "Fix me: I don't know how to stop mysql";
fi
echo 'OK'
}
usage() {
msg="$*"
this=$(basename "$0")
if [ -n "$msg" ]; then
warn "$this: ERROR: $msg"
fi
warn "Usage: $this [-[dhky]] start"
warn ' Arguments:'
#warn " -c or --continue: Continue after failure. This restores all previously made *.sql.gz files too."
warn " -d or --dry-run: Do not drop databases, remove files from $MYSQL_DATA_DIR, or restore databases."
warn ' -h or --help: Show this.'
warn ' -k or --keep: Keep backup files.'
warn ' -y or --yes: Automatically answer yes to all prompts.'
warn "Note: Backups will be made into the current working directory ($(pwd))."
if [ -n "$msg" ]; then
exit 1
fi
exit 0
}
# Command line arguments
while :
do
case "$1" in
-c) CONTINUE=1;; # undocumented option
--continue) CONTINUE=1;; # undocumented option
-d) DRYRUN=1;;
--dry-run) DRYRUN=1;;
-h) usage;;
--help) usage;;
-k) KEEP_BACKUPS=1;;
--keep) KEEP_BACKUPS=1;;
-t) TEST=1;; # undocumented option
--test) TEST=1;; # undocumented option
-y) YES=1;;
--yes) YES=1;;
#-o) shift; o="$1";;
#--) shift; break;;
-*) usage "bad argument $1";;
*) break;;
esac
shift
done
# This forces the use to read the usage before actually running the process
if [ -z "$*" ] || [ "$*" != 'start' ]; then
usage
fi
if [ -n "$DRYRUN" ]; then
echo '*** DRY RUN ***'
fi
# Make sure this script is running as root as sudo is not installed by default on Debian.
if [ "$(id -u)" -ne 0 ]; then
die 1 'This script must be executed as root!';
fi
# Check that $MYSQL_DATA_DIR and the ibdata1 file exist.
if [ ! -d "$MYSQL_DATA_DIR" ]; then
die 1 "Directory $MYSQL_DATA_DIR not found!"
fi
IBDATA1_FILE="$MYSQL_DATA_DIR/ibdata1"
if [ ! -f "$IBDATA1_FILE" ]; then
die 1 "$IBDATA1_FILE not found!"
fi
# Check that required binaries are available.
if [ -z "$(command -v mysql)" ]; then
die $? 'MySQL client (mysql) is not installed!'
fi
if [ -z "$(command -v mysqldump)" ]; then
die $? 'mysqldump not found!'
fi
GZIP="$(command -v pigz)" # compatible with, but faster than gzip
if [ -z "$GZIP" ]; then
GZIP="$(command -v gzip)"
if [ -z "$GZIP" ]; then
die $? 'gzip not found!'
fi
fi
# This line is the only bash dependency:
if [ "$(basename "$SHELL")" = 'bash' ]; then
set -o pipefail
fi
# Make sure mysqld is running
if service_mysql_is_running; then
service_mysql_start
else
echo 'Found active mysql service.'
fi
# Attempt login (first by trying auth_socket authentication, then plain old password authentication)
MYSQL_PASS=
if mysql -u"$MYSQL_USER" -e ';' 2>/dev/null; then
# password is ignored for auth_socket authentication, but must be at least 1 character if the -p argument is given
MYSQL_PASS='dummy'
else
if [ -z "$MYSQL_PASS" ]; then
read -rsp "Enter the MySQL $MYSQL_USER password: " MYSQL_PASS
fi
if ! mysql -u"$MYSQL_USER" -p"$MYSQL_PASS" -e ';' 2> /dev/null; then
echo 'Invalid password.'
exit 1
fi
fi
SIZE_BEFORE=$(du "$IBDATA1_FILE" | cut -f1)
SIZE_BEFORE_HUMAN=$(du -h "$IBDATA1_FILE" | cut -f1)
echo "Current size of $IBDATA1_FILE: $SIZE_BEFORE_HUMAN"
# List databases
#DATABASES=$( mysql -u"$MYSQL_USER" -p"$MYSQL_PASS" --silent -e 'SHOW DATABASES' | grep -Ev '^(information_schema|mysql|performance_schema)$' )
if ! DATABASES=$(mysql -u"$MYSQL_USER" -p"$MYSQL_PASS" --silent -e 'SHOW DATABASES'); then
die $? 'SHOW DATABASES command failed!'
fi
DATABASES=$( echo "$DATABASES" | grep -Ev '^(information_schema|mysql|performance_schema)$' );
if [ ! $? ]; then
die $? 'Failed to grep database list!'
fi
echo "Databases to backup into the current directory ($(pwd)) and then drop:"
for DATABASE in $DATABASES; do
echo " $DATABASE"
done
if [ -z "$YES" ] && ! prompt_yn 'Do you really want to continue?'; then
exit 0
fi
## Backup and drop databases
umask 077
for DATABASE in $DATABASES; do
FILE="$DATABASE.sql.gz"
printf 'Backup %s to %s... ' "$DATABASE" "$FILE"
if ! mysqldump --routines --opt -u"$MYSQL_USER" -p"$MYSQL_PASS" --databases "$DATABASE" | "$GZIP" > "$FILE"; then
die $? 'ERROR: mysqldump failed'
fi
if [ ! -f "$FILE" ]; then
die 1 "ERROR: mysqldump indicated success, but $FILE is missing. Something went wrong!"
fi
echo "OK $(du -h "$FILE" | cut -f1)"
# Only dump 1 database for testing:
if [ -n "$DRYRUN" ] && [ -n "$TEST" ]; then
echo 'Only dumping 1 database for testing.'
break
fi
done
# Drop databases last as mysqldump may refuse to dump those containing views referencing other already dropped databases.
for DATABASE in $DATABASES; do
printf 'Drop %s... ' "$DATABASE"
if [ -z "$DRYRUN" ]; then
if ! mysql -u"$MYSQL_USER" -p"$MYSQL_PASS" -e "DROP DATABASE \`$DATABASE\`"; then
die 1 "ERROR: failed to drop database $DATABASE"
fi
echo 'OK'
else
echo '(Skipped in DRY RUN)'
fi
done
# Stop mysql; delete ib* files; start mysql
service_mysql_stop
for x in "$MYSQL_DATA_DIR"/ibdata? "$MYSQL_DATA_DIR"/ib_logfile*; do
if [ -f "$x" ]; then
printf 'Remove %s (%s)... ' "$x" "$(du -h "$x" | cut -f1)"
if [ -z "$DRYRUN" ]; then
if ! rm -f "$x"; then
die $? "ERROR: Failed to remove $x"
fi
echo 'OK'
else
echo '(Skipped in DRY RUN)'
fi
fi
done
service_mysql_start
# Restoring
if [ -n "$CONTINUE" ]; then
known_databases=''
unknown_databases=''
for file in *.sql.gz; do
known=''
database=$( echo "$file" | sed 's/\.sql\.gz$//' );
#database=${file//.sql.gz/};
for DATABASE in $DATABASES; do
if [ "$database" = "$DATABASE" ]; then
known_databases="$known_databases $database"
known=1
break
fi
done
if [ -z "$known" ]; then
unknown_databases="$unknown_databases $database"
fi
done;
if [ -n "$unknown_databases" ]; then
DATABASES=$( for x in $known_databases $unknown_databases; do echo "$x"; done | sort );
else
DATABASES=$( for x in $known_databases; do echo "$x"; done | sort );
fi
echo 'Databases to restore:'
for DATABASE in $DATABASES; do
echo " $DATABASE"
done
if [ -z "$YES" ] && ! prompt_yn 'Do you really want to continue?'; then
exit 0
fi
fi
for DATABASE in $DATABASES; do
FILE="$DATABASE.sql.gz"
if [ -f "$FILE" ]; then
printf 'Restore %s to database %s... ' "$FILE" "$DATABASE"
if [ -z "$DRYRUN" ]; then
if ! "$GZIP" -dc "$FILE" | mysql -u"$MYSQL_USER" -p"$MYSQL_PASS"; then
die $? 'ERROR'
fi
echo 'OK'
if [ -z "$KEEP_BACKUPS" ]; then
rm -f "$FILE"
fi
else
echo '(Skipped in DRY RUN)'
fi
fi
done
# Statistics
echo "Size of $IBDATA1_FILE before shrinking: $SIZE_BEFORE_HUMAN"
SIZE_AFTER_HUMAN=$(du -h "$IBDATA1_FILE" | cut -f1)
SIZE_AFTER=$(du "$IBDATA1_FILE" | cut -f1)
printf "Size of %s after shrinking: %s (%.1f%% of original; space recovered=%d bytes)\n" \
"$IBDATA1_FILE" \
"$SIZE_AFTER_HUMAN" \
"$(awk "BEGIN { print "100*"$SIZE_AFTER"/"$SIZE_BEFORE"" }")" \
"$(("$SIZE_BEFORE"-"$SIZE_AFTER"))"