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
|
#!/bin/bash
set -e
PATH=$(dirname "$0"):$PATH
source rpc.bash
datadir=/etc/hosting-tools
default_replica_host_file=$datadir/default-replica-host
replication_password_file=$datadir/replication-password
if [ -r "$default_replica_host_file" ]
then
read default_replica_host < "$default_replica_host_file"
fi
gen_password()
{
generated_password_length=32
generated_password=$(tr -cd a-zA-Z0-9 < /dev/urandom |
head -c "$generated_password_length")
[ "${#generated_password}" -eq "$generated_password_length" ]
printf '%s\n' "$generated_password"
}
if ! [ -e "$replication_password_file" ]
then
gen_password > "$replication_password_file"
fi
if [ -r "$replication_password_file" ]
then
read replication_password < "$replication_password_file"
[ "${#replication_password}" -ge 30 ]
fi
primary_host=$(hostname --fqdn)
replica_host=${1:-$default_replica_host}
replication_user=replication
run_primary()
{
(set -x
: primary : $1 $2 ${3:+ ...})
BASH_RPC_REMOTE_DEST=$primary_host remote_run_function "$@"
}
run_replica()
{
(set -x
: replica : $1 $2 ${3:+ ...})
BASH_RPC_REMOTE_DEST=$replica_host remote_run_function "$@"
}
show_hostnames()
{
printf \
"==> %s %s <==\n %s\n" \
"$(hostname -A)" \
"$(hostname -I)" \
"$(uptime)"
}
check_db()
{
ls -lFC --color /var/lib/mysql
mariadb -t "$@" <<.
select
user()
, @@hostname
, @@server_id
, @@gtid_slave_pos
, @@sql_log_bin
;
select
@@hostname
, schema_name as 'database'
from
information_schema.schemata
;
.
}
create_backup()
{
mostly_silent_unless_error create_backup_verbose "$@"
}
create_backup_verbose()
{
set -e
mariabackup_target_dir=/var/mariadb/backup
binlog_info_file=$mariabackup_target_dir/xtrabackup_binlog_info
[ -e "$binlog_info_file" ] && return
mkdir -p "$mariabackup_target_dir"
set -- \
-u root \
--target-dir="$mariabackup_target_dir" \
"$@"
set -x
mariabackup --backup --rsync "$@"
mariabackup --prepare "$@"
[ -e "$binlog_info_file" ]
}
delete_backup()
{
set -x
rm -r /var/mariadb/backup
}
send_backup()
{
mariabackup_target_dir=/var/mariadb/backup
set -x
rsync -zaR -- /./"${mariabackup_target_dir#/}" "$1":/
}
stop_database_server_and_remove_database_files()
{
livedb=/var/lib/mysql
set -e
if [ -e "$livedb" ]
then
if [ "$(systemctl is-active mariadb)" = active ]
then
systemctl stop mariadb
fi
livedb_backup=$livedb~$(date -Ins)
mv -v -T -- "$livedb" "$livedb_backup"
mkdir "$livedb"
chown --reference="$livedb_backup" "$livedb"
chmod --reference="$livedb_backup" "$livedb"
fi
}
restore_from_backup()
{
mariabackup_target_dir=/var/mariadb/backup
set -e
set -- \
--force-non-empty-directories \
-u root \
--target-dir="$mariabackup_target_dir"
set -- "$@"
if mostly_silent_unless_error \
mariabackup --move-back "$@"
then
# Can't believe mariabackup
# is so primitive as to
# recommend this chown in its
# documentation
chown -R mysql:mysql /var/lib/mysql
systemctl start mariadb
set_server_id
else
exit $?
fi
}
silent_unless_error()
{
set -- "$(mktemp)" "$@"
if
"${@:2}" >"$1" 2>&1
then
local r=0
else
local r=$?
cat "$1" >&2
fi
rm "$1"
return $r
}
mostly_silent_unless_error()
{
echo "+ $*" >&2
set -- "$(mktemp)" "$@"
if
"${@:2}" >"$1" 2>&1
then
tail -n3 "$1" >&2
local r=0
else
local r=$?
cat "$1" >&2
fi
rm "$1"
return $r
}
enable_replication()
{
primary_host=$1
replication_user=$2
replication_password=$3
mariabackup_target_dir=/var/mariadb/backup
binlog_info_file=$mariabackup_target_dir/xtrabackup_binlog_info
set -e
read master_log_file master_log_pos gtid_slave_pos < $binlog_info_file
tee /dev/stderr <<END | mariadb --skip-reconnect -t
STOP SLAVE;
SET GLOBAL gtid_slave_pos = "$gtid_slave_pos";
CHANGE MASTER TO
MASTER_HOST='$primary_host',
MASTER_USER='$replication_user',
MASTER_PASSWORD='$replication_password',
MASTER_USE_GTID=slave_pos;
START SLAVE;
SHOW SLAVE STATUS\G
END
set -x
rm -r /var/mariadb/backup
}
create_replication_user()
{
mariadb --skip-reconnect -t <<END
CREATE OR REPLACE USER '$2'@'$1' IDENTIFIED BY '$3';
GRANT REPLICATION SLAVE ON *.* TO '$2'@'$1';
END
}
showvars()
{
for h in "$@"
do
run_$h mariadb --skip-reconnect -t <<END
select VARIABLE_NAME,SESSION_VALUE,GLOBAL_VALUE
from INFORMATION_SCHEMA.SYSTEM_VARIABLES
where VARIABLE_NAME LIKE '%SLAVE%' \G
END
done
}
check_input()
{
[ "$primary_host" ]
[ "$replica_host" ]
[ "$replication_user" ]
[ "$replication_password" ]
dns_check_servers
}
dns_check_servers()
{
primary_ipv4=$(dig +short -ta "$primary_host")
replica_ipv4=$(dig +short -ta "$replica_host")
echo "primary: $primary_host $primary_ipv4"
echo "replica: $replica_host $replica_ipv4"
}
truncated_machineid_decimal_string_int32()
{
systemd-id128 machine-id |
sha256sum |
( read -n8 &&
printf '%u\n' 0x"$REPLY" )
}
run_both()
{
set -e
r=0
run_primary "$@" || r=$?
run_replica "$@"
return $r
}
set_server_id()
{
set -e
chosen_id=$(truncated_machineid_decimal_string_int32)
[ "$chosen_id" -gt 1 ]
mariadb --skip-reconnect -t <<END
set global server_id = $chosen_id;
END
}
mariadb_list_databases()
{
mariadb -Bsss "$@" <<.
select
schema_name
from
information_schema.schemata;
.
}
list_databases_needed_on_replica()
{
set -- "$(mktemp)" "$(mktemp)"
run_primary mariadb_list_databases | sort -u >"$1"
run_replica mariadb_list_databases | sort -u >"$2"
comm -23 -- "$1" "$2"
rm -- "$1" "$2"
}
mariabackup_create_replica_databases()
{
run_primary create_backup
run_primary send_backup "$replica_host"
run_replica stop_database_server_and_remove_database_files
run_replica restore_from_backup
}
mariadbdump_create_replica_databases()
{
list_databases_needed_on_replica
comm -13 -- \
<(list_databases_needed_on_replica | tee /dev/stderr) \
<(printf '%s\n' "$@")
}
main()
{
set -e
check_input
run_primary set_server_id
run_both list_databases
# run_primary check_db
# run_replica check_db
# showvars replica
run_primary create_replication_user \
"$replica_host" \
"$replication_user" \
"$replication_password"
# mariadbdump_create_replica
run_both list_databases
mariabackup_create_replica_databases
run_both list_databases
run_replica check_db
run_replica enable_replication \
"$primary_host" \
"$replication_user" \
"$replication_password"
run_both list_databases
}
cleanup_after_test()
{
run_primary delete_backup || true
}
list_databases()
{
mariadb -t "$@" <<.
select
@@hostname,
@@server_id,
schema_name as 'database'
from
information_schema.schemata
;
.
}
if true
then
cleanup_after_test
fi
main "$@"
exit $?
|