48小时系统班试听入口

linux shell脚本,入门到实战详解(三)

发布作者:新盟教育 发布日期:2022-03-07 浏览人数:8754人

红帽认证技术入门


前文指路:

linux shell脚本,入门到实战详解(一)

linux shell脚本,入门到实战详解(二)


四、shell 编程实战 案例

4.1 系统备份脚本—案例

Tar工具全备、增量备份网站,Shell脚本实现自动打包备份:

 #!/bin/bash
    #Auto Backup Linux System Files
    #by author rivers on 2021-09-28

  SOURCE_DIR=(
        $*
    )
    TARGET_DIR=/data/backup/
    YEAR=`date +%Y`
    MONTH=`date +%m`
    DAY=`date +%d`
    WEEK=`date +%u`
    A_NAME=`date +%H%M`
    FILES=system_backup.tgz
    CODE=$?
    if
        [ -z "$*" ];then
        echo -e "\033[32mUsage:\nPlease Enter Your Backup Files or Directories\n--------------------------------------------\n\nUsage: { $0 /boot /etc}\033[0m"
        exit
    fi
    #Determine Whether the Target Directory Exists
    if
        [ ! -d $TARGET_DIR/$YEAR/$MONTH/$DAY ];then
        mkdir -p $TARGET_DIR/$YEAR/$MONTH/$DAY
        echo -e "\033[32mThe $TARGET_DIR Created Successfully !\033[0m"
    fi
    #EXEC Full_Backup Function Command
    Full_Backup()
    {
    if
        [ "$WEEK" -eq "7" ];then
        rm -rf $TARGET_DIR/snapshot
        cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $FILES ${SOURCE_DIR[@]}
        [ "$CODE" == "0" ]&&echo -e  "--------------------------------------------\n\033[32mThese Full_Backup System Files Backup Successfully !\033[0m"
    fi
    }
    #Perform incremental BACKUP Function Command
    Add_Backup()
    {
       if
            [ $WEEK -ne "7" ];then
            cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $A_NAME$FILES ${SOURCE_DIR[@]}
            [ "$CODE" == "0" ]&&echo -e  "-----------------------------------------\n\033[32mThese Add_Backup System Files $TARGET_DIR/$YEAR/$MONTH/$DAY/${YEAR}_$A_NAME$FILES Backup Successfully !\033[0m"
       fi
    }
    sleep 3 
    Full_Backup;Add_Backup


4.2 收集系统信息—案例

Shell脚本实现服务器信息自动收集:

 cat <<EOF
    ++++++++++++++++++++++++++++++++++++++++++++++
    ++++++++Welcome to use system Collect+++++++++
    ++++++++++++++++++++++++++++++++++++++++++++++
    EOF
    ip_info=`ifconfig |grep "Bcast"|tail -1 |awk '{print $2}'|cut -d: -f 2`
    cpu_info1=`cat /proc/cpuinfo |grep 'model name'|tail -1 |awk -F: '{print $2}'|sed 's/^ //g'|awk '{print $1,$3,$4,$NF}'`
    cpu_info2=`cat /proc/cpuinfo |grep "physical id"|sort |uniq -c|wc -l`
    serv_info=`hostname |tail -1`
    disk_info=`fdisk -l|grep "Disk"|grep -v "identifier"|awk '{print $2,$3,$4}'|sed 's/,//g'`
    mem_info=`free -m |grep "Mem"|awk '{print "Total",$1,$2"M"}'`
    load_info=`uptime |awk '{print "Current Load: "$(NF-2)}'|sed 's/\,//g'`
    mark_info='BeiJing_IDC'
    echo -e "\033[32m-------------------------------------------\033[1m"
    echo IPADDR:${ip_info}
    echo HOSTNAME:$serv_info
    echo CPU_INFO:${cpu_info1} X${cpu_info2}
    echo DISK_INFO:$disk_info
    echo MEM_INFO:$mem_info
    echo LOAD_INFO:$load_info
    echo -e "\033[32m-------------------------------------------\033[0m"
    echo -e -n "\033[36mYou want to write the data to the databases? \033[1m" ;read ensure


    if      [ "$ensure" == "yes" -o "$ensure" == "y" -o "$ensure" == "Y" ];then
            echo "--------------------------------------------"
            echo -e  '\033[31mmysql -uaudit -p123456 -D audit -e ''' "insert into audit_system values('','${ip_info}','$serv_info','${
    cpu_info1} X${cpu_info2}','$disk_info','$mem_info','$load_info','$mark_info')" ''' \033[0m '
            mysql -uroot -p123456 -D test -e "insert into audit_system values('','${ip_info}','$serv_info','${cpu_info1} X${cpu_info2}
    ','$disk_info','$mem_info','$load_info','$mark_info')"
    else
            echo "Please wait,exit......"
            exit
    fi

4.3 一键部署lnmp架构—案例

批量部署lnmp 架构:

[root@web-server01~/script]# vim lnmp.sh 
    #!/bin/bash
    #install lnmp
    #by author rivers on 2021-9-28

    # nginx 环境准备
    Nginx_url=https://nginx.org/download/nginx-1.20.1.tar.gz
    Nginx_prefix=/usr/local/nginx

    # mysql 环境准备
    Mysql_version=mysql-5.5.20.tar.gz
    Mysql_dir=mysql-5.5.20
    Mysql_url=https://downloads.mysql.com/archives/get/p/23/file/mysql-5.5.20.tar.gz
    Mysql_prefix=/usr/local/mysql/

    # php 环境准备
    Php_version=php-7.2.10.tar.gz
    Php_prefix=/usr/local/php-7.2.10/


    function nginx_install(){

   if [[ "$1" -eq "1" ]];then
            if [ $? -eq 0 ];then
                    make && make install
            fi

    fi
    }



    function mysql_install(){
    if [[ "$1" -eq "2" ]];then
    -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
    -DMYSQL_DATADIR=/data/mysql \
    -DSYSCONFDIR=/etc \
    -DMYSQL_USER=mysql \
    -DMYSQL_TCP_PORT=3306 \
    -DWITH_XTRADB_STORAGE_ENGINE=1 \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_PARTITION_STORAGE_ENGINE=1 \
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
    -DWITH_MYISAM_STORAGE_ENGINE=1 \
    -DWITH_READLINE=1 \
    -DENABLED_LOCAL_INFILE=1 \
    -DWITH_EXTRA_CHARSETS=1 \
    -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci \
    -DEXTRA_CHARSETS=all \
                    echo -e "\033[32mThe $Mysql_dir Server Install Success !\033[0m"
            else
                    echo -e "\033[32mThe $Mysql_dir Make or Make install ERROR,Please Check......"
                    exit 0
    fi
    /bin/cp support-files/my-small.cnf  /etc/my.cnf
    /bin/cp support-files/mysql.server /etc/init.d/mysqld
    chmod +x /etc/init.d/mysqld
    chkconfig --add mysqld
    chkconfig mysqld on
    fi
    }


    function php_install(){
    if [[ "$1" -eq "3" ]];then
            if [ $? -eq 0 ];then
                    make ZEND_EXTRA_LIBS='-liconv' && make install
    if [[ "$1" -eq "3" ]];then
     wget $Php_url && tar xf $Php_version && cd $Php_dir && yum install   bxml2* bzip2* libcurl*  libjpeg* libpng* freetype* gmp* libm
    crypt* readline* libxslt* -y && ./configure --prefix=$Php_prefix --disable-fileinfo --enable-fpm --with-config-file-path=/etc --wi
      -config-file-scan-dir=/etc/php.d --with-openssl --with-zlib --with-curl --enable-ftp --with-gd --with-xmlrpc --with-jpeg-dir --w
    ith-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-mbstring --with-mcrypt=/usr/local/libmcrypt --enable-zip --enable-
    mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock=/var/lib/mysql/mysql.sock --without-pear --enable-bcmath
            if [ $? -eq 0 ];then
                    make ZEND_EXTRA_LIBS='-liconv' && make install
                    echo -e "\n\033[32m-----------------------------------------------\033[0m"
                    echo -e "\033[32mThe $Php_version Server Install Success !\033[0m"
            else
                    echo -e "\033[32mThe $Php_version Make or Make install ERROR,Please Check......"
                    exit 0
            fi
    fi
    }


    PS3="Please enter you select install menu:"
    select i in nginx mysql php quit
    do
    "lnmp.sh" 113L, 3516C written                                                                                   
    [root@web-server01~/script]# vim lnmp.sh 
    chkconfig --add mysqld
    chkconfig mysqld on
    fi
    }


    function php_install(){
    if [[ "$1" -eq "3" ]];then
            if [ $? -eq 0 ];then
                    make ZEND_EXTRA_LIBS='-liconv' && make install
                    echo -e "\n\033[32m-----------------------------------------------\033[0m"
                    echo -e "\033[32mThe $Php_version Server Install Success !\033[0m"
            else
                    echo -e "\033[32mThe $Php_version Make or Make install ERROR,Please Check......"
                    exit 0
            fi
    fi
    }


    PS3="Please enter you select install menu:"
    select i in nginx mysql php quit
    do

    case $i in
            nginx)
            nginx_install 1
            ;;
            mysql)
            mysql_install 2
            ;;
            php)
            php_install 3
            ;;
            quit)
            exit
    esac
    done



推荐阅读


>>>新手必备-Linux入门之云计算是什么

>>>红帽认证入门-Linux系统介绍及企业版本选型

>>>新手必备-Linux系统安装配置+Xshell远程连接

>>>Linux常用命令行合集之绝对路径和相对路径

>>>软连接与硬连接


运维界升职加薪必备的云计算技术,你学了吗?


学完高级运维云计算课程之后,你可以:



资源放送 

2022新版Linux运维高级云计算-红帽RHCE实战课程合集,扫描下方二维码即可领取!

图片

客服电话 15822722968 邮箱 799830458@qq.com 地址 天津市红桥区光荣道157号宝能创业中心2号楼-1、2门-907号
官方公众号
商务合作