Essential Production Shell Scripts: MySQL Backup, Nginx Monitoring & System Init
This guide presents four practical Bash scripts for production Linux servers, covering MySQL full backup with remote sync, Nginx process monitoring that triggers Keepalived failover, MySQL slave status alerts, and a comprehensive CentOS 7 system initialization routine.
Production environments often rely on various shell scripts; this article presents several commonly used scripts for enterprise Linux servers.
1. MySQL Full Backup and Remote Sync Script
This Bash script performs a full MySQL dump for listed databases, compresses the output, logs the operation, synchronizes the backup to a remote server via rsync, and removes backups older than 30 days.
#!/bin/bash
# description: MySQL backup shell script
# author: magedu.com
# 192.168.10.10 is the backup server; set up passwordless SSH
DATABASES=(
"magedu01"
"magedu02"
)
USER="root"
PASSWORD="dbpwd123"
MAIL="[email protected]"
BACKUP_DIR=/data/backup
LOGFILE=/data/backup/data_backup.log
DATE=`date +%Y%m%d_%H%M`
cd $BACKUP_DIR
echo "--------------------" >> $LOGFILE
echo "BACKUP DATE:" $(date +"%y-%m-%d %H:%M:%S") >> $LOGFILE
echo "-------------------" >> $LOGFILE
for DATABASE in ${DATABASES}; do
/usr/local/mysql/bin/mysqldump -u$USER -p$PASSWORD --events -R --opt $DATABASE | gzip > ${BACKUP_DIR}/${DATABASE}_${DATE}.sql.gz
if [ $? == 0 ]; then
echo "$DATE--$DATABASE is backup succeed" >> $LOGFILE
else
echo "Database Backup Fail!" >> $LOGFILE
fi
done
if [ $? == 0 ]; then
/usr/bin/rsync -zrtopg --delete /data/backup/* [email protected]:/data/backup/ > /dev/null 2>&1
else
echo "Database Backup Fail!" >> $LOGFILE
mail -s "database Daily Backup Fail!" $MAIL
fi
# Delete backups older than 30 days
find $BACKUP_DIR -type f -mtime +30 -name "*.gz" -exec rm -f {} \;2. Nginx Process Monitoring and Keepalived Switch Script
This script checks the Nginx process every five seconds; if Nginx is not running, it restarts Nginx and stops Keepalived to trigger VIP failover.
#!/bin/bash
while :
do
nginxpid='ps -C nginx --no-header | wc -l'
if [ $nginxpid -eq 0 ]; then
ulimit -SHn 65535
/usr/local/nginx/sbin/nginx
sleep 5
nginxpid='ps -C nginx --no-header | wc -l'
if [ $nginxpid -eq 0 ]; then
/etc/init.d/keepalived stop
fi
fi
sleep 5
done3. MySQL Slave Status Monitoring and Alert Script
The script verifies that MySQL is listening on port 3306, checks that both the slave IO and SQL threads report “Yes”, and sends email alerts when conditions are not met; it is intended to run via cron every ten minutes.
#!/bin/bash
MYSQLPORT='netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ {print $4}'
MYSQLIP='ifconfig eth0|grep "inet addr" | awk -F[:" "]+ {print $4}'
STATUS=$(/usr/local/mysql/bin/mysql -u dbuser -pdbpwd123 -S /tmp/mysql.sock -e "show slave status\G" | grep -i "running")
IO_env='echo $STATUS | grep IO | awk {print $2}'
SQL_env='echo $STATUS | grep SQL | awk {print $2}'
if [ "$MYSQLPORT" == "3306" ]; then
echo "mysql is running"
else
mail -s "warn! server: $MYSQLIP mysql is down" [email protected]
fi
if [ "$IO_env" = "Yes" -a "$SQL_env" = "Yes" ]; then
echo "Slave is running!"
else
echo "####### $date #########" >> /data/log/check_mysql_slave.log
echo "Slave is not running!" >> /data/log/check_mysql_slave.log
mail -s "warn! $MySQLIP_replicate_error" [email protected] < /data/log/check_mysql_slave.log
fi
# Suggested cron entry (run every 10 minutes)
# */10 * * * * root /bin/sh /root/check_mysql_slave.sh4. CentOS 7 System Initialization Script
This comprehensive script prepares a newly installed CentOS 7 server: it replaces the default YUM repository, installs NTP, disables firewalld and SELinux, installs common tools, and upgrades the kernel to the latest mainline version.
#!/bin/bash
# Filename: centos7-init.sh
# Author: [email protected]
if [ `whoami` != "root" ]; then
echo "only root can run it"
exit 1
fi
echo -e "\033[31m This is the centos7 system initialization script, it will update the kernel to the latest version, proceed with caution! \033[0m"
read -s -n1 -p "Press any key to continue or ctrl+C to cancel"
echo "Your inputs: $REPLY"
yum_config() {
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all && yum makecache
}
ntp_config() {
yum -y install chrony
systemctl start chronyd && systemctl enable chronyd
timedatectl set-timezone Asia/Shanghai && timedatectl set-ntp yes
}
close_firewalld() {
systemctl stop firewalld.service &>/dev/null
systemctl disable firewalld.service &>/dev/null
}
close_selinux() {
setenforce 0
sed -i 's/enforcing/disabled/g' /etc/selinux/config
}
yum_tools() {
yum install -y vim wget curl curl-devel bash-completion lsof iotop iostat unzip bzip2 bzip2-devel
yum install -y gcc gcc-c++ make cmake autoconf openssl-devel openssl-perl net-tools
source /usr/share/bash-completion/bash_completion
}
update_kernel() {
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install -y kernel-ml
grub2-set-default 0
grub2-mkconfig -o /boot/grub2/grub.cfg
}
main() {
yum_config
ntp_config
close_firewalld
close_selinux
yum_tools
update_kernel
}
mainSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
