码迷,mamicode.com
首页 > 其他好文 > 详细

使用脚本自建本地yum源

时间:2017-02-23 14:29:56      阅读:657      评论:0      收藏:0      [点我收藏+]

标签:shell   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/1900407

使用脚本自建本地yum源

标签:shell   yum   镜像   

原文地址:http://xugen.blog.51cto.com/6117098/1900407

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!