Created
October 23, 2020 08:57
-
-
Save chrispage1/0f2d0a2ccc6065cce6ad2ab73ca87cac to your computer and use it in GitHub Desktop.
Synchronise service configurations across servers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# define our parameters | |
hostArray=("mariadb-90" "mariadb-91" "mariadb-92") | |
configPath="/etc/maxscale.cnf" | |
service="maxscale" | |
# auto variables | |
currentHost=$(hostname) | |
# create error logging function | |
function fatal_error() { | |
printf "\033[0;31mFailed to restart $service on $1\033[0m\n" | |
printf "Halting syncronistation. Fix errors and try again.\n" | |
exit | |
} | |
# apply the configuration on our current node | |
echo "Restarting ${service} on ${currentHost}" | |
systemctl restart ${service} | |
# verify we were able to restart on current node. | |
if [ ! $? -eq 0 ]; then | |
fatal_error $currentHost | |
fi | |
# apply the configuration to our remaining hosts | |
for host in ${hostArray[@]}; do | |
# dont sync our current host | |
if [ "$host" == "$currentHost" ]; then | |
continue | |
fi | |
# perform SCP to sync our config across | |
echo "Syncronising to ${host}" | |
scp ${configPath} ${host}:${configPath} | |
# check we were able to perform our sync | |
if [ ! $? -eq 0 ]; then | |
echo "Failed to sync config to $host." | |
continue | |
fi | |
# sync performed, do a restart | |
ssh $USER@$host systemctl restart $service | |
# check our sync has worked | |
if [ ! $? -eq 0 ]; then | |
fatal_error $host | |
fi | |
# output success | |
echo "Restarted $service on $host" | |
done | |
echo "Sync complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment