使用脚本自建本地yum源

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用脚本自建本地yum源相关的知识,希望对你有一定的参考价值。

     有一段时间连不上网,搭建服务器环境所需要的远端yum源也无法连接,于是想到了自建一个本地的yum源来解决这个问题。搭建本地yum源需要两个东西:一个是web服务器,另一个就是rpm包及包元素据。

     搭建web服务器很简单,那么主要就是把远端的yum源镜像到本地,所以,找一块比较大的磁盘做好分区挂载到web服务器根目录下,然后执行脚本镜像远端数据到本地就可以了。根据自己的需求,我需要CentOS6和7两个版本的yum源,其中的源包括base、updates、extras和epel且不需要系统iso镜像。于是,连上一个速度快的网络开始同步吧!脚本的内容如下:

    #!/bin/bash    
    ## rsync mirror centos and epel with yun-idc 
    ## visit http://mirrors.yun-idc.com/centos/6/
    ##     http://mirrors.yun-idc.com/centos/7/
    ##     http://mirrors.yun-idc.com/epel/6/
    ##     http://mirrors.yun-idc.com/epel/7/
    
    Rsync="/usr/bin/rsync"
    Options="-azvL  --exclude=SRPMS/  --exclude=xen --exclude=SCL/  --exclude=ppc64/ --exclude=ppc/--exclude=cr/ --exclude=fasttrack/ --exclude=isos/ --bwlimit=2000"
    Reporoot="/var/www/html/centos"
    Centos6base="$Reporoot/6/os/x86_64/"
    Centos6updates="$Reporoot/6/updates/x86_64/"
    Centos6extras="$Reporoot/6/extras/x86_64/"
    Centos6plus="$Reporoot/6/centosplus/x86_64/"
    Centos6epel="$Reporoot/epel/6/x86_64/"
    Centos7base="$Reporoot/7/os/x86_64/"
    Centos7updates="$Reporoot/7/updates/x86_64/"
    Centos7extras="$Reporoot/7/extras/x86_64/"
    Centos7plus="$Reporoot/7/centosplus/x86_64/"
    Centos7epel="$Reporoot/epel/7/x86_64/"
    
    Logdir="$Reporoot/logs/"
    Date=`date +%Y-%m-%d`
    Dateis=`date +%F-%H:%M:%S`
    Logfile="$Logdir/$Date.log"
    Resultfile="$Logdir/$Date.log.res"
    
    Checkres(){
        if [ $? == 0 ];then
           echo -e "\033[1;32m $1 Rsync is success! $Dateis\033[0m" >> $Resultfile
        else
           echo -e "\033[1;31m $1 Rsync is fail! $Dateis\033[0m" >> $Resultfile
        fi
    }
    
    Checkdir(){
        if [ ! -d $1 ];then
            mkdir -p $1
        fi
    }
    Checkdir $Logdir
    Rsyncrepo(){
        Checkdir $2
        $Rsync $Options $1 $2 |tee $Logfile
        Checkres $1
    }
    
    Rsyncrepo rsync://mirrors.yun-idc.com/centos/6/os/x86_64/ $Centos6base
    Rsyncrepo rsync://mirrors.yun-idc.com/centos/6/updates/x86_64/ $Centos6updates
    Rsyncrepo rsync://mirrors.yun-idc.com/centos/6/extras/x86_64/ $Centos6extras
    #Rsyncrepo rsync://mirrors.yun-idc.com/centos/6/centosplus/x86_64/ $Centos6plus
    Rsyncrepo rsync://mirrors.yun-idc.com/centos/7.2.1511/os/x86_64/ $Centos7base
    Rsyncrepo rsync://mirrors.yun-idc.com/centos/7.2.1511/updates/x86_64/ $Centos7updates
    Rsyncrepo rsync://mirrors.yun-idc.com/centos/7.2.1511/extras/x86_64/ $Centos7extras
    #Rsyncrepo rsync://mirrors.yun-idc.com/centos/7.2.1511/centosplus/x86_64/ $Centos7plus
    Rsyncrepo rsync://mirrors.yun-idc.com/epel/6/x86_64/ $Centos6epel
    Rsyncrepo rsync://mirrors.yun-idc.com/epel/7/x86_64/ $Centos7epel

    

接下来配置客户端指向本地yum源就好了,配置文件内容如下:

    [base]    
    name=CentOS-$releasever - Base
    baseurl=http://$host/centos/$releasever/os/$basearch/
            http://mirrors.aliyun.com/centos/$releasever/os/$basearch
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    
    #released updates 
    [updates]
    name=CentOS-$releasever - Updates
    baseurl=http://$host/centos/$releasever/updates/$basearch/
            http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    enabled=1
    
    #additional packages that may be useful
    [extras]
    name=CentOS-$releasever - Extras
    baseurl=http://$host/centos/$releasever/extras/$basearch/
            http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    enabled=1
    
    #epel
    [epel]
    name=Aliyun EPEL
    baseurl=http://$host/centos/epel/$releasever/$basearch/
    gpgcheck=0


本文出自 “3255641” 博客,请务必保留此出处http://xugen.blog.51cto.com/6117098/1900403

以上是关于使用脚本自建本地yum源的主要内容,如果未能解决你的问题,请参考以下文章

使用脚本自建本地yum源

rpm与yum的使用方法,通过yum自建仓库;通过at和crontab命令定期执行脚本

自建yum仓库,分别为网络源和本地源

自建yum仓库(网络源和本地源)

Centos下自建本地yum仓库

Linux之如何配置yum源网络源|自建仓库|hhtp分享源|本地源|超详细新手教程