Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case sharing of shell script in Enterprise production Environment

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1) Mysql database backup script (full backup + remote backup)

Generally, Mysql database backup adopts the mode of full backup + incremental backup on MYSQL slave database. Locking tables causes business impact when backing up the slave database to avoid backup of the Mysql master database.

#! / bin/bash set-e # stop executing script # database name of backup 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# before starting backup Write the backup header to the journal file 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# to determine whether all database backups are successful, and synchronize them to the remote backup f server 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 # send an email to the manager to remind mail-s "database Daily Backup Fail!" $MAILfi# to delete the backup file find $BACKUP_DIR-type f-mtime + 30-name "* .gz"-exec rm-f {}\ after the backup failed. 2) nginx script for log slicing #! / bin/bashset-e # when there is an error in the script Stop executing the script LOG_PATH= "/ var/log/nginx" # define the log storage path ACCESS_LOG= "access.log" ERROR_LOG= "error.log" YESTERDAY=$ (date-d'1 day ago' +% F) # define yesterday's date for i in $(find ${LOG_PATH}-name ${ACCESS_LOG}) Do cd $(dirname ${I}) if [[- f ${ACCESS_LOG}]]; then cp {, ${YESTERDAY} -} ${ACCESS_LOG} # copy the original log file: > ${ACCESS_LOG} # empty the original log contents of fi if [[- f ${ERROR_LOG}]] Then ERROR_SIZE=$ (ls-l ${ERROR_LOG} | awk'{print $5}') if [[${ERROR_SIZE}-gt 20971520]] Then # if the error.log log is greater than 20m, cut cp {, ${YESTERDAY} -} ${ERROR_LOG}: > ${ERROR_LOG} fi fidonefind ${LOG_PATH}-type f-name "* .log"-mtime + 7-delete # to delete the log from a week ago. 3) script for monitoring Nginx process on Nginx load balancer server

If the Nginx+Keepalived architecture is used in the enterprise load balancer layer, but Keepalived cannot switch Nginx services in real time, a monitoring script check_nginx_pid.sh is used here to monitor the running status of Nginx every 5 seconds. If a problem is found, close the local Keepalived program and let VIP switch to the load balancer from 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 5done4) Mysql master-slave monitoring email alarm script

1) this script should be able to adapt to a variety of different intranet and extranet environments.

2) Let the script monitor whether MySQL is running properly.

3) the IO and SQL status of Slave machines must be YES, which is indispensable. Multiple conditional judgment-an is used here.

#! / 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 "warnkeeper 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 / dev/nullsystemctl disable firewalld.service & > / dev/null} # 4. Define the function close_selinux () {setenforce 0sed-I's etc/selinux/config to turn off the selinux. Define the function 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 that installs common tools. Define the function 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} # execute the script main () {kernel Yum_tools; update_kernel;} main

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report