diff options
author | root <root@vps-18a7a2b7.vps.ovh.ca> | 2024-02-13 10:51:03 -0500 |
---|---|---|
committer | root <root@vps-18a7a2b7.vps.ovh.ca> | 2024-02-13 10:51:06 -0500 |
commit | 482596890c808af2f47c8713d0e168e4f0d1ec42 (patch) | |
tree | 8cd12828b0eb01605a8a4b3164a1371229c218ff | |
parent | afd1bdd2065a466888dd6076f9fb9d08a05e9b65 (diff) |
mariadb-dump based database replication
we list the databases on the primary and replica server,
determine which dbs the replica server needs, then send
those dbs in a single tcp stream with mariadb-dump.
supposedly, the CHANGE MASTER sql statements are automatically included.
however, it is unclear how or whether replication is working here.
also, even if replication is working right now, it probably got set up
by the old code that used mariabackup's "xtra" slave info file to get
the binlog position. So the testing needs to be set up to make sure that
this is all OK...
-rwxr-xr-x | src/mariadb-push-replica.sh | 119 |
1 files changed, 84 insertions, 35 deletions
diff --git a/src/mariadb-push-replica.sh b/src/mariadb-push-replica.sh index a7345dc..7134864 100755 --- a/src/mariadb-push-replica.sh +++ b/src/mariadb-push-replica.sh | |||
@@ -33,7 +33,13 @@ then | |||
33 | fi | 33 | fi |
34 | 34 | ||
35 | primary_host=$(hostname --fqdn) | 35 | primary_host=$(hostname --fqdn) |
36 | replica_host=${1:-$default_replica_host} | 36 | if [ $# = 0 ] |
37 | then | ||
38 | replica_host=$default_replica_host | ||
39 | else | ||
40 | replica_host=${1:-$default_replica_host} | ||
41 | shift | ||
42 | fi | ||
37 | replication_user=replication | 43 | replication_user=replication |
38 | 44 | ||
39 | run_primary() | 45 | run_primary() |
@@ -114,7 +120,7 @@ send_backup() | |||
114 | rsync -zaR -- /./"${mariabackup_target_dir#/}" "$1":/ | 120 | rsync -zaR -- /./"${mariabackup_target_dir#/}" "$1":/ |
115 | } | 121 | } |
116 | 122 | ||
117 | stop_database_server_and_remove_database_files() | 123 | stop_mariadb_server_and_remove_database_files() |
118 | { | 124 | { |
119 | livedb=/var/lib/mysql | 125 | livedb=/var/lib/mysql |
120 | set -e | 126 | set -e |
@@ -136,6 +142,7 @@ restore_from_backup() | |||
136 | { | 142 | { |
137 | mariabackup_target_dir=/var/mariadb/backup | 143 | mariabackup_target_dir=/var/mariadb/backup |
138 | set -e | 144 | set -e |
145 | stop_mariadb_server_and_remove_database_files | ||
139 | set -- \ | 146 | set -- \ |
140 | --force-non-empty-directories \ | 147 | --force-non-empty-directories \ |
141 | -u root \ | 148 | -u root \ |
@@ -188,7 +195,7 @@ mostly_silent_unless_error() | |||
188 | return $r | 195 | return $r |
189 | } | 196 | } |
190 | 197 | ||
191 | enable_replication() | 198 | enable_replication_via_mariabackup_xtra_info() |
192 | { | 199 | { |
193 | primary_host=$1 | 200 | primary_host=$1 |
194 | replication_user=$2 | 201 | replication_user=$2 |
@@ -286,57 +293,99 @@ from | |||
286 | . | 293 | . |
287 | } | 294 | } |
288 | 295 | ||
289 | list_databases_needed_on_replica() | ||
290 | { | ||
291 | set -- "$(mktemp)" "$(mktemp)" | ||
292 | run_primary mariadb_list_databases | sort -u >"$1" | ||
293 | run_replica mariadb_list_databases | sort -u >"$2" | ||
294 | comm -23 -- "$1" "$2" | ||
295 | rm -- "$1" "$2" | ||
296 | } | ||
297 | |||
298 | mariabackup_create_replica_databases() | 296 | mariabackup_create_replica_databases() |
299 | { | 297 | { |
300 | run_primary create_backup | 298 | run_primary create_backup |
301 | run_primary send_backup "$replica_host" | 299 | run_primary send_backup "$replica_host" |
302 | run_replica stop_database_server_and_remove_database_files | ||
303 | run_replica restore_from_backup | 300 | run_replica restore_from_backup |
301 | run_primary create_replication_user \ | ||
302 | "$replica_host" \ | ||
303 | "$replication_user" \ | ||
304 | "$replication_password" | ||
305 | run_replica enable_replication_via_mariabackup_xtra_info \ | ||
306 | "$primary_host" \ | ||
307 | "$replication_user" \ | ||
308 | "$replication_password" | ||
309 | } | ||
310 | |||
311 | printlines() | ||
312 | { | ||
313 | printf '%s\n' "$@" | ||
314 | } | ||
315 | |||
316 | printarray() | ||
317 | { | ||
318 | declare -n _PRINTARRAY_VARNAME="$1" | ||
319 | printf '%s\n' "${_PRINTARRAY_VARNAME[@]}" | ||
320 | } | ||
321 | |||
322 | # Call run_replica from here to avoid | ||
323 | # piping the database back to caller | ||
324 | # unnecessarily | ||
325 | send_mariadb_dump() | ||
326 | { | ||
327 | (set -x | ||
328 | mariadb-dump "${@:2}") | | ||
329 | replica_host="$1" run_replica pipe_into_mariadb | ||
304 | } | 330 | } |
305 | 331 | ||
306 | mariadbdump_create_replica_databases() | 332 | pipe_into_mariadb() |
307 | { | 333 | { |
308 | list_databases_needed_on_replica | 334 | pv -f | mariadb |
309 | comm -13 -- \ | 335 | } |
310 | <(list_databases_needed_on_replica | tee /dev/stderr) \ | 336 | |
311 | <(printf '%s\n' "$@") | 337 | mariadbdump_transfer_missing_databases() |
338 | { | ||
339 | declare -a primary_dbs | ||
340 | mapfile -t primary_dbs < \ | ||
341 | <(</dev/null run_primary \ | ||
342 | mariadb_list_databases | sort -u) | ||
343 | |||
344 | declare -a replica_dbs | ||
345 | mapfile -t replica_dbs < \ | ||
346 | <(</dev/null run_replica \ | ||
347 | mariadb_list_databases | sort -u) | ||
348 | |||
349 | declare -a dbs_missing_from_replica | ||
350 | mapfile -t dbs_missing_from_replica < \ | ||
351 | <(comm -23 -- \ | ||
352 | <(printarray primary_dbs) \ | ||
353 | <(printarray replica_dbs)) | ||
354 | |||
355 | declare -a to_replicate | ||
356 | if [ $# = 0 ] | ||
357 | then | ||
358 | to_replicate=("${dbs_missing_from_replica[@]}") | ||
359 | else | ||
360 | mapfile -t to_replicate < \ | ||
361 | <(comm -12 -- \ | ||
362 | <(printarray dbs_missing_from_replica) \ | ||
363 | <(printlines "$@")) | ||
364 | fi | ||
365 | if [ ${#to_replicate[@]} -gt 0 ] | ||
366 | then | ||
367 | run_primary \ | ||
368 | send_mariadb_dump "$replica_host" \ | ||
369 | --master-data \ | ||
370 | --gtid \ | ||
371 | --single-transaction \ | ||
372 | --databases "${to_replicate[@]}" | ||
373 | fi | ||
312 | } | 374 | } |
313 | 375 | ||
314 | main() | 376 | main() |
315 | { | 377 | { |
316 | set -e | 378 | set -e |
317 | check_input | 379 | check_input |
380 | |||
318 | run_primary set_server_id | 381 | run_primary set_server_id |
319 | run_both list_databases | ||
320 | 382 | ||
321 | # run_primary check_db | 383 | # run_primary check_db |
322 | # run_replica check_db | 384 | # run_replica check_db |
323 | # showvars replica | 385 | # showvars replica |
324 | 386 | ||
325 | run_primary create_replication_user \ | 387 | mariadbdump_transfer_missing_databases "$@" |
326 | "$replica_host" \ | 388 | run_replica list_databases |
327 | "$replication_user" \ | ||
328 | "$replication_password" | ||
329 | |||
330 | # mariadbdump_create_replica | ||
331 | run_both list_databases | ||
332 | mariabackup_create_replica_databases | ||
333 | run_both list_databases | ||
334 | |||
335 | run_replica check_db | ||
336 | run_replica enable_replication \ | ||
337 | "$primary_host" \ | ||
338 | "$replication_user" \ | ||
339 | "$replication_password" | ||
340 | run_both list_databases | 389 | run_both list_databases |
341 | } | 390 | } |
342 | 391 | ||
@@ -358,7 +407,7 @@ from | |||
358 | . | 407 | . |
359 | } | 408 | } |
360 | 409 | ||
361 | if true | 410 | if false |
362 | then | 411 | then |
363 | cleanup_after_test | 412 | cleanup_after_test |
364 | fi | 413 | fi |