summaryrefslogtreecommitdiff
path: root/src/mariadb-push-replica.sh
blob: e133fadadb7f45a563803466fd967e68eaf905b4 (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
#!/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)
if [ $# = 0 ]
then
	replica_host=$default_replica_host
else
	replica_host=${1:-$default_replica_host}
	shift
fi
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_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"
}

restore_from_backup()
{
	mariabackup_target_dir=/var/mariadb/backup
	set -e
	stop_mariadb_server_and_remove_database_files
	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_via_mariabackup_xtra_info()
{
	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;
.
}

mariabackup_create_replica_databases()
{
	run_primary create_backup
	run_primary send_backup "$replica_host"
	run_replica restore_from_backup
	run_primary create_replication_user \
		"$replica_host" \
		"$replication_user" \
		"$replication_password"
	run_replica enable_replication_via_mariabackup_xtra_info \
		"$primary_host" \
		"$replication_user" \
		"$replication_password"
}

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}"
	) |
	replica_host="$1" run_replica receive_mariadb_dump
}

receive_mariadb_dump()
{
	pv -f | 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()
{
	declare -g -a primary_dbs
	save_array primary_dbs from lines <(
		run_primary mariadb_list_databases </dev/null |
		sort -u
	)

	declare -g -a replica_dbs
	save_array replica_dbs from lines <(
		run_replica mariadb_list_databases </dev/null |
		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
	)
}

mariadbdump_transfer_missing_databases()
{
	mariadb_scan_databases
	declare -a to_replicate
	if [ $# = 0 ]
	then
		to_replicate=("${primary_dbs_not_on_replica[@]}")
		printf "Missing on ${replica_host}: %s\n" \
			"${to_replicate[@]}"
		exit
	else
		save_array to_replicate from lines \
			<(intersection_lines \
				<(printarray primary_dbs_not_on_replica) \
				<(printlines "$@" | sort -u))
	fi
	mariadbdump_transfer_databases \
		"$replica_host" \
		"${to_replicate[@]}"
}

mariadbdump_transfer_databases()
{
	[ $# -ge 2 ] || return
	run_primary \
		send_mariadb_dump "$1" \
		--master-data \
		--gtid \
		--single-transaction \
		--databases "${@:2}"
}

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_primary set_server_id

	# run_primary check_db
	# run_replica check_db
	# showvars replica

	mariadbdump_transfer_missing_databases "$@"
	run_replica list_databases
	run_both list_databases
}

cleanup_after_test()
{
	run_primary delete_backup || true
}

list_databases()
{
mariadb -t "$@" <<.
select
	@@hostname
,	@@server_id
,	count(schema_name) as 'databases'
,	@@gtid_slave_pos
from
	information_schema.schemata
;
select
	schema_name as 'database'
from
	information_schema.schemata
;
.
}

if false
then
	cleanup_after_test
fi
main "$@"
exit $?