CentOS搭建本地YUM源

作者: 聂扬帆博客 | 来源:发表于2017-01-12 11:27 被阅读707次

    为了减少公司内部大量vps使用外部yum源导致带宽不足情况,于是就搭建一台本地yum源服务器,通过脚本服务器定时去镜像站点更新yum数据。

    1. 建立yum源存放路径(本文以搭建CentOS 5,6,7YUM源为例)
    #分别创建5,6,7三个文件夹
    $ mkdir -p /cache1/{5,6,7}
    
    1. 创建获取镜像站点数据脚本/CentOS/sysyum.sh(本文选用的是rsync.mirrors.ustc.edu.cn镜像站点)
    #!/bin/bash  
    *****************************CentOS5*****************************
    /usr/bin/rsync -avzL --exclude=addons --exclude=centosplus --exclude=contrib --exclude=cr --exclude=fasttrack --exclude=isos --exclude=RELEASE-NOTES* --exclude=i386 --exclude=*i386.rpm --exclude=repodata rsync://rsync.mirrors.ustc.edu.cn/centos/5/ /cache1/5
    for i in /cache1/5/extras/x86_64 /cache1/5/os/x86_64 /cache1/5/updates/x86_64 ; 
    do 
    /bin/rm $i/repodata -rf && /usr/bin/createrepo -s sha --workers 8 $i ;
    done
    /usr/bin/rsync -avzL --exclude=i386 --exclude=ppc --exclude=repodata --exclude=repoview --exclude=SRPMS rsync://rsync.mirrors.ustc.edu.cn/epel/5 /cache1/epel/
    for i in /cache1/epel/5/x86_64 ; 
    do 
    /bin/rm $i/repodata -rf && /usr/bin/createrepo -s sha --worker 8 $i ;
    done
    *****************************CentOS6*****************************
    /usr/bin/rsync -avzL --exclude=addons --exclude=centosplus --exclude=contrib --exclude=cr --exclude=fasttrack --exclude=isos --exclude=sclo --exclude=storage --exclude=virt --exclude=cloud --exclude=RELEASE-NOTES* --exclude=i386 --exclude=*i386.rpm --exclude=repodata --exclude=SCL --exclude=xen4 rsync://rsync.mirrors.ustc.edu.cn/centos/6/ /cache1/6
    for i in /cache1/6/extras/x86_64 /cache1/6/os/x86_64 /cache1/6/updates/x86_64 ; 
    do 
    /bin/rm $i/repodata -rf && /usr/bin/createrepo --workers 8 $i ;
    done
    /usr/bin/rsync -avzL --exclude=i386 --exclude=ppc64 --exclude=repodata --exclude=repoview --exclude=SRPMS rsync://rsync.mirrors.ustc.edu.cn/epel/6 /cache1/epel/
    for i in /cache1/epel/6/x86_64 ;
     do 
    /bin/rm $i/repodata -rf && /usr/bin/createrepo --worker 8 $i ;
    done
    *****************************CentOS7*****************************
    /usr/bin/rsync -avzL --exclude=atomic --exclude=centosplus --exclude=contrib --exclude=cloud  --exclude=cr --exclude=fasttrack --exclude=isos --exclude=paas --exclude=sclo --exclude=virt --exclude=storage --exclude=RELEASE-NOTES* --exclude=i386 --exclude=*i386.rpm --exclude=repodata rsync://rsync.mirrors.ustc.edu.cn/centos/7/ /cache1/7
    for i in /cache1/7/extras/x86_64 /cache1/7/os/x86_64 /cache1/7/updates/x86_64 ; 
    do 
    /bin/rm $i/repodata -rf && /usr/bin/createrepo -s sha --workers 8 $i ;
    done
    /usr/bin/rsync -avzL --exclude=i386 --exclude=ppc64 --exclude=ppc64le --exclude=repodata --exclude=repoview --exclude=SRPMS rsync://rsync.mirrors.ustc.edu.cn/epel/7 /cache1/epel/
    for i in /cache1/epel/7/x86_64 ; 
    do 
    /bin/rm $i/repodata -rf && /usr/bin/createrepo --worker 8 $i ;
    done
    
    1. 添加排程,定时去爬数据(flock是防止旧的排程没有结束,而新的排程有开始运行)
    01 02 * * * flock -xn /tmp/mytest.lock -c '/bin/bash /CentOS/sysyum.sh 2>&1 >> /dev/null'
    
    1. 创建内网机器获取内网YUM源的脚本yum.sh
    #!/bin/bash
    [ -f /etc/init.d/functions ] && . /etc/init.d/functions
    # Configure yum Packgae Manager
    ARCH=`/usr/bin/getconf LONG_BIT`
    REPOD="/etc/yum.repos.d"
    #判断5和6系统的版本命令
    OSVER=`/bin/awk -F "[ .]" '{print $3}' /etc/redhat-release`
    #判断7系统的版本命令
    OSVER7=`/bin/awk -F "[ .]" '{print $4}' /etc/redhat-release`
    #创建yum源备份文件,移动所有repo文件到备份目录
    if [ "$(ls -A $REPOD)" ] ; then
        [ -d ${REPOD}/repos.old ] || /bin/mkdir ${REPOD}/repos.old
        /bin/mv ${REPOD}/*.repo ${REPOD}/repos.old/ -f
    fi
    #判断系统版本,并生成新的repo文件
    #判断是否为CentOS5版本
    if [ ${ARCH}x == 64x -a ${OSVER}x == 5x ] ; then
        /bin/cat >>${REPOD}/CentOS-Base.repo <<EOF
    [base]
    name=CentOS-\$releasever - Base
    baseurl=http://ip:port/5/os/x86_64
    gpgcheck=0 
    [updates]
    name=CentOS-\$releasever - Updates
    baseurl=http://ip:port/5/updates/x86_64
    gpgcheck=0
    [extras]
    name=CentOS-\$releasever - Extras
    baseurl=http://ip:port/5/extras/x86_64
    gpgcheck=0
    [epel]
    name=CentOS-\$releaserver - Epel
    baseurl=http://ip:port/epel/5/x86_64
    gpgcheck=0
    EOF
    #判断是否为CentOS6版本
    elif [ ${ARCH}x == 64x -a ${OSVER}x == 6x ] ; then
        /bin/cat >>${REPOD}/CentOS-Base.repo <<EOF
    [base]
    name=CentOS-\$releasever - Base
    baseurl=http://ip:port/6/os/x86_64
    gpgcheck=0
    [updates]
    name=CentOS-\$releasever - Updates
    baseurl=http://ip:port/6/updates/x86_64
    gpgcheck=0
    [extras]
    name=CentOS-\$releasever - Extras
    baseurl=http://ip:port/6/extras/x86_64
    gpgcheck=0
    [epel]
    name=CentOS-\$releaserver - Epel
    baseurl=http://ip:port/epel/6/x86_64
    gpgcheck=0
    EOF
    #判断是否为CentOS7版本
    elif [ ${ARCH}x == 64x -a ${OSVER7}x == 7x ] ; then
        /bin/cat >>${REPOD}/CentOS-Base.repo <<EOF
    [base]
    name=CentOS-\$releasever - Base
    baseurl=http://ip:port/7/os/x86_64
    gpgcheck=0
    [updates]
    name=CentOS-\$releasever - Updates
    baseurl=http://ip:port/7/updates/x86_64
    gpgcheck=0
    [extras]
    name=CentOS-\$releasever - Extras
    baseurl=http://ip:port/7/extras/x86_64
    gpgcheck=0
    [epel]
    name=CentOS-\$releaserver - Epel
    baseurl=http://ip:port/epel/7/x86_64
    gpgcheck=0
    EOF
    else
        echo "only support CentOS [5/6/7].x86_64"
        exit 1
    fi
    
    1. 配置nginx(即第4步中url的ip和port)
    server {
     listen port;
     server_name localhost;
     index index.html index.htm;
     #yum.sh脚本目录,可自行替换,
     root /CentOS;
     autoindex on;
     access_log logs/yum.access.log access;
     }
    
    1. 使用内网YUM源
      一切准备就绪后,我们内网机器就可以通过下面命令去安装内网YUM并使用。
    $ wget -O - http://ip:port/yum.sh | sh
    

    博客地址:http://yangfannie.com

    相关文章

      网友评论

        本文标题:CentOS搭建本地YUM源

        本文链接:https://www.haomeiwen.com/subject/wyedbttx.html