Blog/Linux/Keepalived/sync_nginx.sh

45 lines
1.2 KiB
Bash
Raw Permalink Normal View History

2023-12-17 20:17:25 +01:00
#!/bin/bash
KEYPATH="/root/.ssh/reverseproxy2"
REMOTEHOST="reverseproxy-2.domaine"
# La synchro se fait par une récupération depuis le backup de master.
# Si keepalived est master, on ne fait pas de synchro
STATE=`/bin/snmpget -v2c -c public 127.0.0.1 KEEPALIVED-MIB::vrrpInstanceState.1 | /bin/sed -Er 's/(.*)INTEGER: (.*)\(([0-9])\)/\2/g'`
echo "State : ${STATE}"
if [[ "$STATE" == "master" ]]; then
echo "Master : No sync"
exit 2
fi
# Si keepalived est down, on ne fait pas de synchro (maintenance)
systemctl is-active --quiet keepalived
if [ $? -ne 0 ]; then
echo "Keepalived down : no sync"
exit 0
fi
echo "Run sync nginx"
RSYNCNGINX=$(/bin/rsync -e "ssh -i $KEYPATH" -aiuh root@$REMOTEHOST:/etc/nginx/ /etc/nginx)
if [ $? -eq 0 ]; then
echo "Run sync Let's Encrypt'"
RSYNCLE=$(/bin/rsync -e "ssh -i $KEYPATH" -aiuh root@$REMOTEHOST:/etc/letsencrypt/ /etc/letsencrypt)
else
exit 3
fi
if [ $? -eq 0 ]; then
echo "Run sync www"
RSYNCWWW=$(/bin/rsync -e "ssh -i $KEYPATH" -aiuh root@$REMOTEHOST:/var/www/ /var/www)
else
exit 2
fi
if [ $? -eq 0 ]; then
if [ -n "${RSYNCNGINX}" ] || [ -n "${RSYNCWWW}" ]; then
/usr/sbin/nginx -s reload
echo "reloaded."
fi
else
exit 1
fi