sysadmin-tools/scripts/mariadb-cluster-bootstrap-replication

49 lines
1.2 KiB
Bash
Executable file

#!/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"