17 个实用 shell 脚本,建议收藏( 二 )

7、批量检测网站是否异常并邮件通知#!/bin/bashURL_LIST="www.baidu.com www.ctnrs.com www.der-matech.net.cn www.der-matech.com.cn www.der-matech.cn www.der-matech.top www.der-matech.org"for URL in $URL_LIST; doFAIL_COUNT=0for ((i=1;i<=3;i++)); doHTTP_CODE=$(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" $URL)if [ $HTTP_CODE -eq 200 ]; thenecho "$URL OK"breakelseecho "$URL retry $FAIL_COUNT"let FAIL_COUNT++fidoneif [ $FAIL_COUNT -eq 3 ]; thenecho "Warning: $URL Access failure!"echo "网站$URL坏掉 , 请及时处理" | mail -s "$URL网站高危" 1794748404@qq.comfidone8、批量主机远程执行命令脚本#!/bin/bashCOMMAND=$*HOST_INFO=host.infofor IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); doUSER=$(awk -v ip=$IP 'ip==$1{print $2}' $HOST_INFO)PORT=$(awk -v ip=$IP 'ip==$1{print $3}' $HOST_INFO)PASS=$(awk -v ip=$IP 'ip==$1{print $4}' $HOST_INFO)expect -c "spawn ssh -p $PORT $USER@$IPexpect {"(yes/no)" {send "yesr"; exp_continue}"password:" {send "$PASSr"; exp_continue}"$USER@*" {send "$COMMANDr exitr"; exp_continue}}"echo "-------------------"done9、一键部署LNMP网站平台脚本#!/bin/bashNginx_V=1.15.6php_V=5.6.36TMP_DIR=/tmp INSTALL_DIR=/usr/local PWD_C=$PWD echoecho -e "tMenun"echo -e "1. Install Nginx"echo -e "2. Install PHP"echo -e "3. Install MySQL"echo -e "4. Deploy LNMP"echo -e "9. Quit" function command_status_check() { if [ $? -ne 0 ]; thenecho $1exit fi } function install_nginx() {cd $TMP_DIRyum install -y gcc gcc-c++ make openssl-devel pcre-devel wgetwget http://nginx.org/download/nginx-${NGINX_V}.tar.gztar zxf nginx-${NGINX_V}.tar.gzcd nginx-${NGINX_V}./configure --prefix=$INSTALL_DIR/nginx--with-http_ssl_module--with-http_stub_status_module--with-streamcommand_status_check "Nginx - 平台环境检查失败!"make -j 4command_status_check "Nginx - 编译失败!"make installcommand_status_check "Nginx - 安装失败!"mkdir -p $INSTALL_DIR/nginx/conf/vhostalias cp=cp ; cp -rf $PWD_C/nginx.conf $INSTALL_DIR/nginx/confrm -rf $INSTALL_DIR/nginx/html/*echo "ok" > $INSTALL_DIR/nginx/html/status.htmlecho '<?php echo "ok"?>' > $INSTALL_DIR/nginx/html/status.php$INSTALL_DIR/nginx/sbin/nginxcommand_status_check "Nginx - 启动失败!"} function install_php() { cd $TMP_DIRyum install -y gcc gcc-c++ make gd-devel libxml2-devellibcurl-devel libjpeg-devel libpng-devel openssl-devellibmcrypt-devel libxslt-devel libtidy-develwget http://docs.php.net/distributions/php-${PHP_V}.tar.gztar zxf php-${PHP_V}.tar.gzcd php-${PHP_V}./configure --prefix=$INSTALL_DIR/php--with-config-file-path=$INSTALL_DIR/php/etc--enable-fpm --enable-opcache--with-mysql --with-mysqli --with-pdo-mysql--with-openssl --with-zlib --with-curl --with-gd--with-jpeg-dir --with-png-dir --with-freetype-dir--enable-mbstring --enable-hashcommand_status_check "PHP - 平台环境检查失败!"make -j 4command_status_check "PHP - 编译失败!"make installcommand_status_check "PHP - 安装失败!"cp php.ini-production $INSTALL_DIR/php/etc/php.inicp sapi/fpm/php-fpm.conf $INSTALL_DIR/php/etc/php-fpm.confcp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpmchmod +x /etc/init.d/php-fpm/etc/init.d/php-fpm startcommand_status_check "PHP - 启动失败!"} read -p "请输入编号:" numbercase $number in1)install_nginx;;2)install_php;;3)install_mysql;;4)install_nginxinstall_php;;9)exit;;esac10、监控MySQL主从同步状态是否异常脚本#!/bin/bashHOST=localhostUSER=rootPASSWD=123.comIO_SQL_STATUS=$(mysql -h$HOST -u$USER -p$PASSWD -e 'show slave statusG' 2>/dev/null |awk '/Slave_.*_Running:/{print $1$2}')for i in $IO_SQL_STATUS; doTHREAD_STATUS_NAME=${i%:*}THREAD_STATUS=${i#*:}if [ "$THREAD_STATUS" != "Yes" ]; thenecho "Error: MySQL Master-Slave $THREAD_STATUS_NAME status is $THREAD_STATUS!" |mail -s "Master-Slave Staus" xxx@163.comfidone11、MySql数据库备份脚本分库备份mysqldump -uroot -pxxx -B A > A.sql#!/bin/bashDATE=$(date +%F_%H-%M-%S)HOST=localhostUSER=backupPASS=123.comBACKUP_DIR=/data/db_backupDB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys") for DB in $DB_LIST; doBACKUP_NAME=$BACKUP_DIR/${DB}_${DATE}.sqlif ! mysqldump -h$HOST -u$USER -p$PASS -B $DB > $BACKUP_NAME 2>/dev/null; thenecho "$BACKUP_NAME 备份失败!"fidone分表备份mysqldump -uroot -pxxx -A t > t.sql#!/bin/bashDATE=$(date +%F_%H-%M-%S)HOST=localhostUSER=backupPASS=123.comBACKUP_DIR=/data/db_backupDB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys") for DB in $DB_LIST; doBACKUP_DB_DIR=$BACKUP_DIR/${DB}_${DATE}[ ! -d $BACKUP_DB_DIR ] && mkdir -p $BACKUP_DB_DIR &>/dev/nullTABLE_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "use $DB;show tables;" 2>/dev/null)for TABLE in $TABLE_LIST; doBACKUP_NAME=$BACKUP_DB_DIR/${TABLE}.sqlif ! mysqldump -h$HOST -u$USER -p$PASS $DB $TABLE > $BACKUP_NAME 2>/dev/null; thenecho "$BACKUP_NAME 备份失败!"fidonedone


推荐阅读