Centos-automount

发布时间:2020-07-16 19:01:21 作者:在你的左边
来源:网络 阅读:497
#!/bin/bash
#=======================================================================================
#
#               FILE:    automount      
#              USAGE:    automount [-h] [Copy Bash_Code_Style]
#     DESCRIPTION:    Copy bash style guide and coding standard .
#            OPTIONS:    see fuction 'usage' below 
#    REQUIREMENTS:    ---
#               BUGS:    ---
#              NOTES:    --- 
#             AUTHOR:    Dr.-Vision. Simple(sp).
#            COMPANY:    China
#            VERSION:    1.0
#            CREATED:    28.09.2016 - 10:15:30
#        REVISION:    28.09.2016
#            PROJECT:    XXX
#          COPYRITHT:    Copyright(c)2002-2020 Shell, All Rights Reserved
#=======================================================================================

#If required, additional information has to be added (e.g copyright note,project assignment).

TODAY=`date +%Y-%m-%d`
NOW=`date +%H-%M`
BASE=$0
BASEDIR=$(dirname $0)
USAGE=$(echo -e "${BASE%%.*}")
LOG_PATH="${BASEDIR}/proclog/${TODAY}/${USAGE}"
mkdir -p ${LOG_PATH}
CMD_LOG="${LOG_PATH}/${NOW}_${USAGE}.cmd"
INFO_LOG="${LOG_PATH}/${NOW}_${USAGE}.info"
ERROR_LOG="${LOG_PATH}/${NOW}_${USAGE}.err"
FSTAB_NAME="/etc/fstab"
BAK_FSTAB="/etc/fstab.bak"
MOUNT_FILE_INFO="/tmp/mount_info"
MOUNT_FILE="/tmp/mount_file"
SFDISK_FILE_INFO="/tmp/sfdisk_info"
DIFF_INFO="/tmp/sfdisk_diff_info"
DIFF_FILE="/tmp/sfdisk_diff_file"
MOUNT_POINT="/data/dev"
MTYPE="ext4"
FLAG=0
exec 4>${MOUNT_FILE_INFO}
exec 5>${SFDISK_FILE_INFO}
exec 6>${DIFF_INFO}

#   Function comments

#===   FUNCTION     ====================================================================
#          NAME:            
#   DESCRIPTION:    Display usage information for this script.
#  PARAMETER  1:    ---
#=======================================================================================
function readme {
echo -e '\033[01;33m'
cat << EOF
*******************************************
Keep Warnning. 
*******************************************
Keys:
        1.Check /etc/fstab.bak is exists and set up basedir.
        2.read /etc/fstab and save in /etc/fstab.bak .
        3.get disk device info
        4.format and mount disk
        5.write /etc/fstab.
*******************************************
EOF
echo -e '\033[01;0m'
read -p "线上设备已经格式化,确认?(y/n)" answer
case $answer in
    y|Y|yes|Yes|YES)
        echo ""
        ;;
    n|N|no|No|NO)
                rm -rf ${DIFF_INFO} ${MOUNT_FILE_INFO} ${SFDISK_FILE_INFO} 
        echo "退出脚本!" 
        exit 0
        ;;
        *)
        echo "退出脚本!" 
        exit 0
        ;;
esac
}

function check_env {
#>${INFO_LOG}
        # FIRST RUN
        if test ! -d "${LOG_PATH}";then
                mkdir -p "${LOG_PATH}"
                exec 2>${ERROR_LOG}
                exec 8>>${CMD_LOG}
                echo -e "################################  1.1 Check Env Start ################################\n" 1>&8
        else
                exec 2>${ERROR_LOG}
                exec 8>>${CMD_LOG}
                echo -e "################################  1.1 Check Env Start ################################\n" 1>&8
        fi

        if [[ -e ${BAK_FSTAB} ]];then
                printf "\e[32m ############# ${NOW} Check File /etc/fstab [ Already Exists ] ############\e[0m\n"
                printf "\e[32m ############# ${NOW} Check File /etc/fstab [ Already Exists ] ############\e[0m\n">${INFO_LOG}  
                echo "rm -rf ${DIFF_INFO} ${MOUNT_FILE_INFO} ${MOUNT_FILE} ${SFDISK_FILE_INFO}  ${SFDISK_FILE}\n" 1>&8
                rm -rf ${DIFF_INFO} ${MOUNT_FILE_INFO} ${MOUNT_FILE} ${SFDISK_FILE_INFO}  ${SFDISK_FILE}
                exit 0
        else
                exec 3>${BAK_FSTAB}
                FLAG=1
                echo "exec 3>${BAK_FSTAB} FLAG=1"1>&8
                printf "\e[32m ################ First Write /etc/fstab [ Running ] ##############\e[0m\n"
                printf "\e[32m ################ First Write /etc/fstab [ Running ] ##############\e[0m\n">${INFO_LOG} 
        fi      
        echo "################################  1.1 Check Env End ################################  " 1>&8
}

function read_fstab {
        echo "################################  1.2 Read fstab Start ################################  " 1>&8
        while read DATA
        do
                echo "${DATA}"
        done<${FSTAB_NAME}>&3
        wait;
        printf "\e[32m ################ Read /etc/fstab [ OK ] ##############\e[0m\n"
        printf "\e[32m ################ Read /etc/fstab [ OK ] ##############\e[0m\n">>${INFO_LOG} 
        echo "################################  1.2 Read fstab End ################################  " 1>&8
}

function get_device_info() {
        #mount_device_info=`df -Th|tail -n +2|awk '{print $1}'|grep -v "tmpfs"|awk '{print substr($0,1,length-1)}'|uniq`
        echo "#############################  1.3 Get Device info Start #############################  " 1>&8
        mount_device_info=`df -Th|tail -n +2|awk '{print $1}'|grep -v "tmpfs\|sda[0-9]"|uniq`
        sfdisk_device_info=`ls /dev/sd*|grep -v "[0-9]\|sda"`
        echo "${mount_device_info}">&4
        echo "${sfdisk_device_info}">&5
        diff_info="`sort ${MOUNT_FILE_INFO} ${SFDISK_FILE_INFO}|uniq`"
        echo "diff_info:sort ${MOUNT_FILE_INFO} ${SFDISK_FILE_INFO}|uniq" 1>&8
        if [[ ${diff_info} != "" ]];then
                echo  "${diff_info}|tr -d \"[0-9]\"|awk '{if(length !=0) print $0}'>&6 " 1>&8
                echo -e "${diff_info}"|tr -d "[0-9]"|awk '{if(length !=0) print $0}'>&6
                printf "\e[32m ################ Get Device Info [ Success ] ##############\e[0m\n"
                printf "\e[32m ################ Get Device Info [ Success ] ##############\e[0m\n">>${INFO_LOG} 
        else
                echo "cat /dev/null>&6" 1>&8
                cat /dev/null>&6
                printf "\e[32m ################ Get Device Info [ Failed ] ##############\e[0m\n"
                printf "\e[32m ################ Get Device Info [ Failed ] ##############\e[0m\n">>${INFO_LOG}
        fi
        echo "#############################  1.3 Get Device info End #############################  " 1>&8
}

function write_device_info {
        echo "#############################  1.4 Pre Write fstab Start #########################  " 1>&8
        echo "#############################  1.4 Pre Write fstab Start #########################  ">>${INFO_LOG} 
        DIFF_SIZE=$(du ${DIFF_INFO}|awk '{print $1}')
        echo -e "################ Write Fstab.bak Start-Time:${TODAY} ################\n">>${BAK_FSTAB}
        if [[ -s ${DIFF_INFO} && ${DIFF_SIZE} -ge 4 ]];then
                devices=$(cat ${DIFF_INFO}|wc -l)
                awk '$0=""NR" "$0' ${DIFF_INFO}>${DIFF_FILE}
                if [[ ${DIFF_SIZE} -gt 1 && ${FLAG} != 0 ]];then
                        for((id=1;id<=${devices};id++))
                        do      
                                eval `awk -vid=$id '{if($1==id) print "name="$2";id="$1}' ${DIFF_FILE}`
                                num=$(expr ${id} - 1)
                                #echo "mount -o remount,rw -t ${MTYPE} ${name} ${MOUNT_POINT}${num}" 1>&1|tee ${MOUNT_FILE}>>${FSTAB_NAME}
                                remount=`df -Th|grep "${MOUNT_POINT}${num}"|wc -l`
                                if [[ ${remount} -eq 0 ]];then
                                        echo "mount -t ${MTYPE} ${name} ${MOUNT_POINT}${num}" 1>&8
                                        mount -t ${MTYPE} ${name} "${MOUNT_POINT}${num}"
                                else
                                        echo "mount -o remount,rw -t ${MTYPE} ${name} ${MOUNT_POINT}${num}" 1>&8
                                        mount -o remount,rw -t ${MTYPE} ${name} "${MOUNT_POINT}${num}"
                                fi 
                                if [ ! -f "${MOUNT_POINT}${num}/${num}" ];then
                                        mkdir -p "${MOUNT_POINT}${num}" && touch "${MOUNT_POINT}${num}/${num}"
                                        echo "mkdir -p ${MOUNT_POINT}${num} && touch ${MOUNT_POINT}${num}/${num}" 1>&8
                                        echo "${name}\t\t${MOUNT_POINT}${num}\t\t${MTYPE}\tdefaults 0 0 >>${BAK_FSTAB}" 1>&8
                                        echo -e "${name}\t\t${MOUNT_POINT}${num}\t\t${MTYPE}\tdefaults 0 0">>${BAK_FSTAB}
                                        printf "\e[32m ################ Disk ${name} is Alread Mount [ OK ] ##############\e[0m\n"
                                        printf "\e[32m ################ Disk ${name} is Alread Mount [ OK ] ##############\e[0m\n">>${INFO_LOG}
                                else
                                        printf "\e[32m ################ Disk ${name} is Alread Mount [ OK ] ##############\e[0m\n">>${INFO_LOG}
                                        printf "\e[32m ################ Disk ${name} is Alread Mount [ OK ] ##############\e[0m\n"
                                        echo "${name}\t\t${MOUNT_POINT}${num}\t\t${MTYPE}\tdefaults 0 0 >>${BAK_FSTAB}" 1>&8
                                        echo -e "${name}\t\t${MOUNT_POINT}${num}\t\t${MTYPE}\tdefaults 0 0">>${BAK_FSTAB}
                                fi 
                        done            
                fi
                echo "rm -rf ${DIFF_INFO} ${MOUNT_FILE_INFO} ${MOUNT_FILE} ${SFDISK_FILE_INFO}  ${SFDISK_FILE}" 1>&8
                rm -rf ${DIFF_INFO} ${MOUNT_FILE_INFO} ${MOUNT_FILE} ${SFDISK_FILE_INFO}  ${SFDISK_FILE}
        else
                printf "\e[32m ################ All Disk is Alread Mount [ OK ] ##############\e[0m\n"
                printf "\e[32m ################ All Disk is Alread Mount [ OK ] ##############\e[0m\n" >>${INFO_LOG}
        fi      
        echo -e "################ Write Fstab.bak End-Time:${TODAY} ################\n">>${BAK_FSTAB}
        echo "#############################  1.4 Pre Write fstab End #########################  " 1>&8
        echo "#############################  1.4 Pre Write fstab End #########################  ">>${INFO_LOG} 
}

function write_fstab {
        echo "#############################  1.5 Write fstab Start #########################  "1>&8
        if [[ -f ${BAK_FSTAB} && -f ${FSTAB_NAME} ]];then
                export comm_result="$(comm -3 ${BAK_FSTAB} ${FSTAB_NAME})"
        fi
        echo -e '\033[01;33m'
        cat << EOF >>${INFO_LOG}
****************************************************************************************
Write Warnning. 
****************************************************************************************
Write Keys:
        1.Write mount device info and auto mount.
        2.result=$(echo -e "$comm_result")
****************************************************************************************
EOF
        echo "${comm_result}">>${FSTAB_NAME}
        awk '!a[$0]++' ${BAK_FSTAB} >${FSTAB_NAME}
        echo "#############################  1.5 Write fstab End #########################  "1>&8
}

function main_func {
        readme
        check_env
        read_fstab
        get_device_info
        write_device_info
        write_fstab
}

#   Section comments

#---------------------------------------------------------------------------------------
#   define main.
#---------------------------------------------------------------------------------------

main_func 

#   line end comments
推荐阅读:
  1. 有符号数和无符号数在一起如何处理的
  2. php redis如何设置过期时间

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

centos自动挂载 ce tos

上一篇:详解XStream别名

下一篇:cocos2d 场景切换

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》