千家信息网

企业生产环境shell脚本案例分享

发表于:2024-11-28 作者:千家信息网编辑
千家信息网最后更新 2024年11月28日,1)Mysql数据库备份脚本(完整备份+异地备份)一般Mysql数据库备份会采用在MYSQL从库上执行全量备份+增量备份方式。在从库备份避免Mysql主库备份的时候锁表造成业务影响。#!/bin/ba
千家信息网最后更新 2024年11月28日企业生产环境shell脚本案例分享

1)Mysql数据库备份脚本(完整备份+异地备份)

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

#!/bin/bash  set -e                         #当脚本有错误时,便停止执行脚本#备份的数据库名DATABASES=(            "magedu01"            "magedu02")USER="root"PASSWORD="dbpwd123"MAIL="123456@163.com"BACKUP_DIR=/data/backupLOGFILE=/data/backup/data_backup.logDATE=`date +%Y%m%d_%H%M`cd $BACKUP_DIR#开始备份之前,将备份信息头写入日记文件   echo "--------------------" >> $LOGFILEecho "BACKUP DATE:" $(date +"%y-%m-%d %H:%M:%S") >> $LOGFILEecho "-------------------" >> $LOGFILEfor 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  fidone#判断数据库备份是否全部成功,全部成功就同步到异地备份f服务器if [ $? == 0 ];then  /usr/bin/rsync -zrtopg   --delete  /data/backup/* root@192.168.10.10:/data/backup/  >/dev/null 2>&1else  echo "Database Backup Fail!" >> $LOGFILE  #备份失败后向管理者发送邮件提醒  mail -s "database Daily Backup Fail!" $MAILfi#删除30天以上的备份文件  find $BACKUP_DIR  -type f -mtime +30 -name "*.gz" -exec rm -f {} \;

2)nginx进行日志切割的脚本

#!/bin/bashset -e                         #当脚本有错误时,便停止执行脚本LOG_PATH="/var/log/nginx"      #定义日志存放路径ACCESS_LOG="access.log"ERROR_LOG="error.log"YESTERDAY=$(date -d '1 day ago' +%F)        #定义昨天的日期for i in $(find ${LOG_PATH} -name ${ACCESS_LOG});do    cd $(dirname ${i})    if  [[ -f ${ACCESS_LOG} ]];then        cp {,${YESTERDAY}-}${ACCESS_LOG}         #将原本的日志文件复制一份        : > ${ACCESS_LOG}                        #将原本的日志内容清空    fi    if  [[ -f ${ERROR_LOG} ]];then        ERROR_SIZE=$(ls -l   ${ERROR_LOG} | awk '{print $5}')        if  [[ ${ERROR_SIZE} -gt 20971520 ]];then               #如果error.log日志大于20M,进行切割            cp {,${YESTERDAY}-}${ERROR_LOG}            : > ${ERROR_LOG}        fi    fidonefind ${LOG_PATH} -type f -name "*.log" -mtime +7 -delete            #将一周前的日志进行删除

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

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

#!/bin/bashwhile :donginxpid=`ps -C nginx --no-header | wc -l`if [ $nginxpid -eq 0 ];then  ulimit -SHn 65535  /usr/local/nginx/sbin/nginx    sleep 5else /etc/init.d/keepalived stopfisleep 5done

4)Mysql主从监控邮件报警脚本

1)此脚本应该能适应各种各样不同的内外网环境。
2)让脚本也顺便监控下MySQL是否正常运行。
3)Slave机器的IO和SQL状态都必须为YES,缺一不可,这里用到了多重条件判断-a。

#!/bin/bashMYSQLPORT='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.comfiif [ "$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.logfi

建议此脚本10分钟执行一次,可编写计划任务!

5)系统初始化脚本

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

#!/bin/bash#判断是否为root用户if [ `whoami` != "root" ];thenecho " only root can run it"exit 1fi#执行前提示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.backupwget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repoyum clean all && yum makecache}#2.定义配置NTP的函数ntp_config(){yum -y install chronysystemctl start chronyd && systemctl enable chronydtimedatectl set-timezone Asia/Shanghai && timedatectl set-ntp yes}#3.定义关闭防火墙的函数close_firewalld(){systemctl stop firewalld.service &> /dev/nullsystemctl disable firewalld.service &> /dev/null}#4.定义关闭selinux的函数close_selinux(){setenforce 0sed -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-develyum install -y gcc gcc-c++ make cmake autoconf openssl-devel openssl-perl net-toolssource /usr/share/bash-completion/bash_completion}#6.定义升级最新内核的函数update_kernel (){rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.orgrpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpmyum --enablerepo=elrepo-kernel install -y kernel-mlgrub2-set-default 0grub2-mkconfig -o /boot/grub2/grub.cfg}#执行脚本main(){    yum_config;    ntp_config;    close_firewalld;    close_selinux;    yum_tools;    update_kernel;}main
0