Skip to content

Instantly share code, notes, and snippets.

@chrispage1
Created October 23, 2020 08:57
Show Gist options
  • Save chrispage1/0f2d0a2ccc6065cce6ad2ab73ca87cac to your computer and use it in GitHub Desktop.
Save chrispage1/0f2d0a2ccc6065cce6ad2ab73ca87cac to your computer and use it in GitHub Desktop.
Synchronise service configurations across servers
#!/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