From b2dec60e05d38630d567900ef668aadf66ddfc33 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 13 Feb 2024 16:10:38 -0500 Subject: refactor like a mastor --- src/mariadb-push-replica.sh | 161 +++++++++++++++++++++++++++++++------------- 1 file changed, 114 insertions(+), 47 deletions(-) diff --git a/src/mariadb-push-replica.sh b/src/mariadb-push-replica.sh index 7134864..e133fad 100755 --- a/src/mariadb-push-replica.sh +++ b/src/mariadb-push-replica.sh @@ -124,18 +124,18 @@ stop_mariadb_server_and_remove_database_files() { livedb=/var/lib/mysql set -e - if [ -e "$livedb" ] + set -o pipefail + [ -e "$livedb" ] || return 0 + + if [ "$(systemctl is-active mariadb)" = active ] 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" + 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() @@ -321,56 +321,117 @@ printarray() # Call run_replica from here to avoid # piping the database back to caller -# unnecessarily +# 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 pipe_into_mariadb + ( + set -x + mariadb-dump "${@:2}" + ) | + replica_host="$1" run_replica receive_mariadb_dump } -pipe_into_mariadb() +receive_mariadb_dump() { - pv -f | mariadb + pv -f | mariadb --skip-reconnect } -mariadbdump_transfer_missing_databases() +save_array() { - declare -a primary_dbs - mapfile -t primary_dbs < \ - <(