企业生产环境Shell脚本案例分享
生产环境下的Shell脚本还是挺多的,这里介绍几个企业常见的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/* [email protected]:/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" [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 # 建议每10分钟运行一次: shell> crontab -e */10 * * * * root /bin/sh /root/check_mysql_slave.sh
4、系统初始化脚本
相关推荐
huha 2020-10-16
laisean 2020-11-11
大牛牛 2020-10-30
firefaith 2020-10-30
liguojia 2020-10-20
wangzhaotongalex 2020-10-20
以梦为马不负韶华 2020-10-20
JohnYork 2020-10-16
Julyth 2020-10-16
applecarelte 2020-10-16
laisean 2020-09-27
flycappuccino 2020-09-27
liguojia 2020-09-27
wangzhaotongalex 2020-09-22
流年浅滩 2020-10-23
liujianhua 2020-10-22
woaimeinuo 2020-10-21
tufeiax 2020-09-03
laisean 2020-09-01
vvu 2020-09-16
libao 2020-09-16
Yyqingmofeige 2020-08-18
zhushixia 2020-08-17