summaryrefslogtreecommitdiff
path: root/mariadb/mariadb-push-databases-to-empty-replica
blob: dc5bfa06b5ee4dfa77b0322b19b94928565064d2 (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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#!/bin/bash
set -e
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)
if [ $# = 0 ]
then
	replica_host=$default_replica_host
else
	replica_host=${1:-$default_replica_host}
	shift
fi
replication_user=replication

without_tty_input()
{
	if [ -t 0 ]
	then
		"$@" < /dev/null
	else
		"$@"
	fi
}

run_()
{
	case "$1" in
		primary_host | replica_host ) ;;
		* ) return 58 ;;
	esac
	(set -x
	: ${1%_host} : $2 $3 ${4:+ ...})
	BASH_RPC_REMOTE_DEST=${!1} \
		without_tty_input \
		remote_run_function "${@:2}"
}

run_primary()
{
	run_ primary_host "$@"
}

run_replica()
{
	run_ replica_host "$@"
}

show_hostnames()
{
	printf \
		"==> %s %s <==\n    %s\n" \
		"$(hostname -A)" \
		"$(hostname -I)" \
		"$(uptime)"
}

stop_mariadb_server_and_remove_database_files()
{
	livedb=/var/lib/mysql
	set -e
	set -o pipefail
	[ -e "$livedb" ] || return 0
	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"
}

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
}

mariadb_install_replication_credentials()
{
	set -e
	mariadb -v --skip-reconnect -t <<.
stop slave
;
change master
to
	master_host     = '$1'
,	master_user     = '$2'
,	master_password = '$3'
,	master_ssl = 1
,	master_ssl_verify_server_cert = 1
;
.
}

create_and_authorize_replication_user()
{
	mariadb --skip-reconnect -t <<.
create or replace
user
	'$2'@'$1'
identified
by
	'$3'
;
grant
	replication replica
on
	*.*
to
	'$2'@'$1'
;
select
	@@hostname
	as
		'primary host'
,	concat (user, '@', host)
	as
		login
,	repl_slave_priv
	as
		'replica privilege'
from
	mysql.user
where
	repl_slave_priv = 'Y'
;
.
}

showvars()
{
	mariadb --skip-reconnect -t <<.
select
	variable_name
,	session_value
,	global_value
from
	information_schema.system_variables
where
	variable_name like '%slave%state%'
\G
.
}

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 -v --skip-reconnect -t <<.
set global
	server_id = $chosen_id
;
.
}

mariadb_enable_semi_sync()
{
	set -e
	[ "$1" = off ] || set -- on
	mariadb -v --skip-reconnect -t <<.
stop
	slave io_thread
;
set global
	rpl_semi_sync_master_enabled = $1
;
set global
	rpl_semi_sync_slave_enabled = $1
;
.
	mariadb_show_vars_like 'rpl_%'
}

mariadb_show_vars_like()
{
	set -e
	mariadb -v --skip-reconnect -t <<.
select
	@@hostname
,	@@server_id
;
use
	information_schema
;
select
	variable_name
,	variable_value
from
	global_variables
where
	variable_name like '$1'
;
.
}

mariadb_list_databases()
{
	show_all_databases >&2
	mariadb --skip-reconnect -B -s <<.
select
	schema_name
from
	information_schema.schemata
;
.
}

printlines()
{
	printf '%s\n' "$@"
}

printarray()
{
	declare -n _PRINTARRAY_VARNAME="$1"
	printf '%s\n' "${_PRINTARRAY_VARNAME[@]}"
}

# Call run_replica from here to avoid
# piping the database back to caller
# unnecessarily.  Better would be
# a direct connect from mariadb client
# to remote mariadb server; but that
# requires transmitting credentials.
# Credential-transporter.  Transporter-transporter.
# Well, a transporter protein is more like a provenance tag,
# and the transporter code is the receptor to the transporter
# which is the provenance checker.  But anyway we have that
# with ssh, except we don't: we are assuming the primary
# has the ssh root of the replica!  Insanity!  Now this only works
# because the code runs on the primary; unless we forward the
# ssh auth with the ssh agent; but that only makes the security
# flaw temporary not solved; in fact, the replica should receive
# the transmission on some limited authorization channel; which
# _could_ be ssh; in fact, the dump could transparently be either
# live or else cached on the server side; it could be the
# rsync.net backup even; but ... it needs to include
# the btrfs snap similarly ... .
send_mariadb_dump()
{
	(
		set -x
		mariadb-dump "${@:2}"
	) |
	BASH_RPC_REMOTE_DEST=$1 \
		remote_run_function \
		receive_mariadb_dump
}

receive_mariadb_dump()
{
	pv -f |
	tee /var/cache/mariadb-dump.sql |
	mariadb --skip-reconnect
}

save_array()
{
	declare -n _to_save="$1"
	case "$2 $3" in
		'from zfile' )
			mapfile -d '' -t _to_save < "$4"
			;;
		'from lines' )
			mapfile -t _to_save < "$4"
			;;
		* )
			false
			;;
	esac
}

mariadb_scan_databases()
{
	set -- "$(mktemp)"
	declare -g -a primary_dbs
	save_array primary_dbs from lines <(
		run_primary mariadb_list_databases 2>"$1" |
		sort -u
	)

	declare -g -a replica_dbs
	save_array replica_dbs from lines <(
		run_replica mariadb_list_databases 2>>"$1" |
		sort -u
	)

	declare -g -a primary_dbs_not_on_replica
	save_array primary_dbs_not_on_replica from zfile <(
		subtract_arrays primary_dbs replica_dbs
	)
	if [ ${#primary_dbs_not_on_replica[@]} -gt 0 ]
	then
		cat "$1" >&2
	fi
	rm -- "$1"
}

showmissing()
{
	if [ ${#primary_dbs_not_on_replica[@]} -gt 0 ]
	then
		printf "Missing on ${replica_host}: %s\n" \
			"${primary_dbs_not_on_replica[@]}" >&2
	fi
}

choose_mariadbdump_target_databases()
{
	declare -n _target_db_array="$1"
	shift
	if [ $# = 0 ]
	then
		showmissing

		if [ "$SEND_ALL_MARIADB_DATABASES" ]
		then
			_target_db_array=("${primary_dbs_not_on_replica[@]}")
		else
			_target_db_array=()
		fi
	else
		save_array _target_db_array from lines \
			<(intersection_lines \
				<(printarray primary_dbs_not_on_replica) \
				<(printlines "$@" | sort -u))
	fi
}

intersection_lines()
{
	comm -12 -- "$1" "$2"
}

subtract_arrays()
{
	declare -n _a="$1" _b="$2"
	comm -z -23 -- \
		<(printf '%s\0' "${_a[@]}") \
		<(printf '%s\0' "${_b[@]}")
}

main()
{
	set -e
	check_input

	: run_both set_server_id
	: run_both mariadb_enable_semi_sync off
	run_primary create_and_authorize_replication_user \
		"$replica_host" \
		"$replication_user" \
		"$replication_password"

	mariadb_scan_databases
	choose_mariadbdump_target_databases databases "$@"
	if [ ${#databases[@]} -gt 0 ]
	then
		run_replica \
			mariadb_install_replication_credentials \
			"$primary_host" \
			"$replication_user" \
			"$replication_password"
		run_primary \
			send_mariadb_dump \
			"$replica_host" \
			--master-data \
			--apply-slave-statements \
			--gtid \
			--single-transaction \
			--databases "${databases[@]}"
		run_replica \
			show_all_databases
	fi
	if gtid=$(run_primary mariadb_get_primary_gtid) &&
	[ "$gtid" ]
	then
		run_replica \
			mariadb_wait_on_gtid "$gtid"
	fi
}

mariadb_get_primary_gtid()
{
	mariadb --skip-reconnect -B -s <<.
select
	@@gtid_binlog_pos;
.
}

mariadb_wait_on_gtid()
{
	local gtid="$1"
	mariadb --skip-reconnect -t <<.
select
	@@hostname
,	@@gtid_slave_pos as 'replica gtid'
,	'$gtid' as 'primary gtid'
;
stop
	slave io_thread
;
start
	slave io_thread
;
.
	echo "trying primary_gtid_wait()... " >&2
	mariadb -t --skip-reconnect <<.
select
	master_gtid_wait('$gtid')
	as \`primary_gtid_wait('$gtid')\`
;
.
}

cleanup_after_test()
{
	run_primary delete_backup || true
}

show_all_databases()
{
mariadb --skip-reconnect -t "$@" <<.
select
	@@hostname
,	@@server_id
,	count(schema_name) as 'databases'
,	user() as 'login'
,	@@gtid_slave_pos as 'replica gtid'
,	@@gtid_binlog_pos as 'primary gtid'
from
	information_schema.schemata
\G
select
	schema_name as 'database name'
from
	information_schema.schemata
;
.
}

if false
then
	cleanup_after_test
fi
SEND_ALL_MARIADB_DATABASES=y
main "$@"
exit $?