shell脚本案例

1、MySQL数据库备份脚本,下面的脚本是Mysql全量备份+异地备份

一般Mysql数据库备份会采用在MYSQL从库上执行全量备份+增量备份方式。在从库备份避免Mysql主库备份的时候锁表造成业务影响。

shell> vim db_backup.sh

#!/bin/bash  
# description: MySQL buckup shell script  
# author:       magedu.com
# 192.168.10.10 为专门的备份服务器,需要做一下服务器之间免密码登录

#备份的数据库名
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  
done
#判断数据库备份是否全部成功,全部成功就同步到异地备份f服务器
if [ $? == 0 ];then
 /usr/bin/rsync -zrtopg   --delete  /data/backup/* root@192.168.10.10:/data/backup/  >/dev/null 2>&1
else
 echo "Database Backup Fail!" >> $LOGFILE  
 #备份失败后向管理者发送邮件提醒
 mail -s "database Daily Backup Fail!" $MAIL  
fi

#删除30天以上的备份文件  
find $BACKUP_DIR  -type f -mtime +30 -name "*.gz" -exec rm -f {} \;

2、Nginx负载均衡服务器上监控Nginx进程的脚本

企业负载均衡层如果用到Nginx+Keepalived架构,而Keepalived无法进行Nginx服务的实时切换,所以这里用了一个监控脚本check_nginx_pid.sh,每隔5秒就监控一次Nginx的运行状态,如果发现有问题就关闭本机的Keepalived程序,让VIP切换到从Nginx负载均衡器上

shell> vim check_nginx_pid.sh

#!/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
done

3、MySQL主从监控邮件报警脚本

  • 此脚本应该能适应各种各样不同的内外网环境。

  • 让脚本也顺便监控下MySQL是否正常运行。

  • Slave机器的IO和SQL状态都必须为YES,缺一不可,这里用到了多重条件判断-a。


shell> check_mysql_slave.sh
#!/bin/bash
#check MySQL_Slave Status

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 -dbpwd123 -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" magedu@gmail.com
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" magedu@gmail.com << /data/log/check_mysql_slave.log
fi

# 建议每10分钟运行一次:
shell> crontab -e
*/10 * * * * root /bin/sh /root/check_mysql_slave.sh

4、系统初始化脚本

此脚本用于新装Linux的相关配置工作,比如更换默认yum源,优化系统内核、停掉一些没必要启动的系统服务等。此脚本尤其适合大批新安装的CentOS系列的服务器。适用于Centos7


shell>vim cenots_7_system_init.sh
#!/bin/bash
# Filename:   centos7-init.sh
# Author:     [email protected]
#判断是否为root用户
if [ `whoami` != "root" ];then
echo " only root can run it"
exit 1
fi
#执行前提示
echo -e "\033[31m 这是centos7系统初始化脚本,将更新系统内核至最新版本,请慎重运行!\033[0m"
read -s -n1 -p "Press any key to continue or ctrl+C to cancel"
echo "Your inputs: $REPLY"
#1.定义配置yum源的函数
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
}
#2.定义配置NTP的函数
ntp_config(){
yum y install chrony
systemctl start chronyd && systemctl enable chronyd
timedatectl set-timezone Asia/Shanghai && timedatectl set-ntp yes
}
#3.定义关闭防火墙的函数
close_firewalld(){
systemctl stop firewalld.service &> /dev/null
systemctl disable firewalld.service &> /dev/null
}
#4.定义关闭selinux的函数
close_selinux(){
setenforce 0
sed -i 's/enforcing/disabled/g' /etc/selinux/config
}
#5.定义安装常用工具的函数
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
}
#6.定义升级最新内核的函数
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;
}
main

你可能感兴趣的:(shell脚本案例)