Tool to bootstrap mariadb replication

This commit is contained in:
Johan Wassberg 2024-10-11 15:42:52 +02:00
parent 160877f19a
commit 51ef2983e9
Signed by: jocar
GPG key ID: BE4EC2EEADF2C31B

View file

@ -0,0 +1,48 @@
#!/bin/bash
set -x
set -e
FROM="${1}"
TO="${2}"
function usage() {
echo "Usage: ${0} <from> <to>"
echo "Example: ${0} db-1.example.com db-2.example.com"
exit 1
}
if [ -z "${FROM}" ]; then
usage
fi
if [ -z "${TO}" ]; then
usage
fi
# Make sure latest code is in use
ssh -o StrictHostKeyChecking=off "${FROM}" "systemctl restart sunet-mariadb.service"
echo -n "Waiting for service to restart."
for _ in $(seq 1 35); do
sleep 1s
echo -n '.'
done
echo ""
# Do backup
ssh -t "${FROM}" /usr/local/bin/run_manual_backup_dump
dump=$(ssh -t "${FROM}" "ls -atr /opt/mariadb/backups/*sql.gz | tail -1| tr -d '\n'")
# Copy backup to backup server
scp -3 -o StrictHostKeyChecking=off "${FROM}":"${dump}" "${TO}":/opt/mariadb/backups/init.sql.gz
# Make sure everything is clean
ssh -o StrictHostKeyChecking=off -t "${TO}" "systemctl stop "sunet-mariadb.service
ssh -t "${TO}" "rm -rf /opt/mariadb/datadir/*"
ssh -t "${TO}" "systemctl restart sunet-mariadb.service"
echo -n "Waiting for service to restart."
for _ in $(seq 1 10); do
sleep 1s
echo -n '.'
done
echo ""
# Start replication
ssh -t "${TO}" "docker exec -ti mariadb-db-1 /scripts/start_replica_from_init.sh"
ssh -t "${TO}" "rm -rf /opt/mariadb/backups/init.sql.gz"