部署阿里镜像yum源及epel源

#!/bin/bash
#一键自动部署安装阿里云yum镜像源
#Date:2020.04.17

#set -euo pipefail

#判断yum源安装是否完成,并将输出结果写入日志
install_result(){
    if [ $? -eq 0 ];then
       echo "安装 aliyun yum源成功"    
    else
        echo "安装 aliyun yum源失败"
        exit 1 
    fi
}

#检查网络情况
ping -c 1 developer.aliyun.com >/dev/null && ping -c 1 www.baidu.com >/dev/null &&
[ $? -eq 0 ] && echo "网络检测正常,正在进行下一步" || echo “网络异常,请检查当前网络情况”

#检查wget命令是否安装
wget --version >/dev/null || yum install wget -y


#备份系统yum文件

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup


#获取当前系统型号
Centos_Version=`cat /etc/redhat-release |awk -F"[. ]+" '{print $4}'`

echo "正在为您安装Centos ${Centos_Version} 阿里云镜像源,请稍等..."


#通过wget方式下载阿里云yum源、epel源
case ${Centos_Version} in
    
    6)
        yum_6="wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo"
        epel_6="wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo"
        ${yum_6} && ${epel_6}
        install_result   # 调用结果判断函数
        ;;
    7)
        yum_7="wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo"
        epel_7="wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo"
        ${yum_7} && ${epel_7}
        install_result  # 调用结果判断函数
        ;;
    8)
        yum_8="wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo"
        epel_8="yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm"
        ${yum_8} && ${epel_8}
	install_result  # 调用结果判断函数
        #将repo配置中的地址替换为阿里云镜像站地址
        sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
        sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
        ;;
    *)
	
        echo "获取该系统版本信息为为:${Centos_Version},请检查当前系统信息"
	exit 1
	;;
esac

#yum缓存
echo "正在进行yum makecache, 请勿操作..."
yum clean all >/dev/null && yum makecache fast >>Dev_install_yum.log
if [ $? -eq 0 ];then
    echo "清除yum源或生成yum缓存成功"
else
    echo "清除yum源或生成yum缓存失败"
    echo "清除yum源或生成yum缓存失败"
fi

 

你可能感兴趣的:(Shell)