File: //bigscoots/wpo/failover/syncfiles.sh
#!/bin/bash
serverip=$($(which ifconfig) | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | head -1)
mkdir -p /root/.bigscoots/failover
if [ ! -f /root/.bigscoots/failover/failover.servers ]; then
bash /bigscoots/general/slack.sh "#failovers" ":warning: $(hostname) - ${serverip} - File Sync - No Failover servers defined in /root/.bigscoots/failover/failover.servers"
exit
fi
# Define the paths that need to be synced with a placeholder for domain if needed
sync_domain_paths=(
"/home/nginx/domains/{domain}/"
"/usr/local/nginx/conf/conf.d/{domain}.conf"
"/usr/local/nginx/conf/conf.d/{domain}.ssl.conf"
"/usr/local/nginx/conf/wpincludes/{domain}/"
"/usr/local/nginx/conf/ssl/{domain}/"
)
sync_global_paths=(
"/etc/pure-ftpd/"
"/home/nginx/.bashrc"
"/root/.acme.sh/"
"/usr/local/lib/php.ini"
"/etc/centminmod/php.d/a_customphp.ini"
)
# Add dynamically expanded paths for multiphp
for multiphp_ini in /etc/opt/remi/*/php.d/zzz_customphp.ini /etc/opt/remi/*/php-fpm.d/*; do
if [ -f "$multiphp_ini" ]; then
sync_global_paths+=("$multiphp_ini")
fi
done
# Iterate over each server
while read -r server; do
# Sync global paths (once per server)
for path in "${sync_global_paths[@]}"; do
destination="${server}:${path}"
# Get the current epoch time
epochtime=$(date +%s)
if ! rsync -ah --delete --exclude .user.ini --exclude log/access.log* --log-file="/root/.bigscoots/failover/syncto${server}.log" -e "ssh -p 2222" "${path}" "${destination}"; then
# Copy the log file to a failed log with the epoch time appended
cp "/root/.bigscoots/failover/syncto${server}.log" "/root/.bigscoots/failover/syncto${server}.${epochtime}.failed.log"
# Send the Slack notification
bash /bigscoots/general/slack.sh "#failovers" ":warning: $(hostname) - ${serverip} - File Sync - Issue with syncing files from ${path} to ${destination}"
# Send the email notification if contact emails exist
if [ -s /root/.bigscoots/failover/contact.emails ]; then
mail -s "$(hostname) - ${serverip} - File Sync - Issue with syncing files from ${path} to ${destination}" $(cat /root/.bigscoots/failover/contact.emails | tr '\n' ',') < /root/.bigscoots/failover/syncto"${server}".log
rm -f /root/.bigscoots/failover/syncto"${server}".log
fi
fi
done
# Sync domain-specific paths (once per domain per server)
for domain in $(ls -1 /home/nginx/domains/); do
for path in "${sync_domain_paths[@]}"; do
# Replace {domain} placeholder with the actual domain name
source_path="${path/\{domain\}/$domain}"
# Ensure that the destination does not duplicate the domain
destination="${server}:${path/\{domain\}/$domain}"
if ! rsync -ah --delete --exclude .user.ini --exclude log/access.log* --log-file="/root/.bigscoots/failover/syncto${server}.log" -e "ssh -p 2222" "${source_path}" "${destination}"; then
bash /bigscoots/general/slack.sh "#failovers" ":warning: $(hostname) - ${serverip} - File Sync - Issue with syncing files from ${source_path} to ${destination}"
if [ -s /root/.bigscoots/failover/contact.emails ]; then
mail -s "$(hostname) - ${serverip} - File Sync - Issue with syncing files from ${source_path} to ${destination}" $(cat /root/.bigscoots/failover/contact.emails | tr '\n' ',') < /root/.bigscoots/failover/syncto"${server}".log
rm -f /root/.bigscoots/failover/syncto"${server}".log
fi
fi
done
done
done < /root/.bigscoots/failover/failover.servers