配置yum源并与公网同步更新
尽管sohu和163都推出了mirrors服务,但当要配置多台服务器时仍然需要占用很多公网带宽,因此配置一个本地yum源并时常与公网同步就显得非常有必要了。本文介绍的就是针对CentOS 5.4 配置yum源, 并且每日同步公网数据。每日的同步使用rsync协议,这里要注意并不是所有的mirrors站点都支持rsync协议,不过不用担心,下文有更新脚本,里面有我测试过的几个支持rsync协议的mirros站点。
配置yum源
本文要配置3个yum软件仓库,分别为:CentOS标准软件仓库、epel、以及rpmforge。经过多次尝试,定位了速度和更新频率都比较理想的几个站点。更多的mirrors站点参见以下网址:
CentOS:http://www.centos.org/modules/tinycontent/index.php?id=32
epel:https://fedoraproject.org/wiki/EPEL
rpmforge:http://apt.sw.be/
同步yum源脚本
#!/bin/bash
#==========================================
# Program : update_yum_source.sh
# Info : 定期同步官方 yum 源到本机
# Version : 2010.01.28 First Release
#==========================================
Date=`date +%Y%m%d`
LogFile=”/data/logs/rsync_yum/$Date.log”
CentOSTrunkVer=”5″
CentOSCurrentVer=”5.4″
ReceiveMail=”nathanzhou@mysite.com”
RsyncBin=”/usr/bin/rsync”
RsyncPerm=”-avrt –delete –exclude=debug/ –exclude=isos/”
CentOS_Trunk_Ver_Path=”/data/soft/mirror.mysite.com/centos/$CentOSTrunkVer”
CentOS_Current_Ver_Path=”/data/soft/mirror.mysite.com/centos/$CentOSCurrentVer”
YumSiteList=”rsync://mirror.averse.net/centos”
#============ epel ==============
epelSite=”rsync://mirrors.sohu.com/fedora-epel/”
epelLocalPath=”/data/soft/mirror.mysite.com/epel”
# rpmforge
rpmforgeSite=”rsync://apt.sw.be/freshrpms/pub/dag/redhat/el5/”
rpmforgeLocalPath=”/data/soft/mirror.mysite.com/rpmforce”
echo “—- $Date `date +%T` Begin —-” >>$LogFile
# centos 5
$RsyncBin $RsyncPerm $YumSiteList/$CentOSTrunkVer/ \
$CentOS_Trunk_Ver_Path >> $LogFile
# centos 5.4
$RsyncBin $RsyncPerm $YumSiteList/$CentOSCurrentVer/ \
$CentOS_Current_Ver_Path >> $LogFile
# epel
$RsyncBin $RsyncPerm –exclude=4/ –exclude=4AS/ –exclude=4AS/ \
–exclude=4WS/ –bwlimit=500 $epelSite $epelLocalPath >> $LogFile
# rpmforge
$RsyncBin $RsyncPerm $rpmforgeSite $rpmforgeLocalPath >> $LogFile
echo “—- $Date `date +%T` End —-” >> $LogFile
/bin/mail -s “opt001 – update yum source – $Date” $ReceiveMail<$LogFile
该脚本只更新了CentOS 5.4的软件包,其它的版本都略过了。根据不同的情况,脚本中还有很多变量需要做相应的修改,如yum保存路径,更新后发送的邮件地址等等。
首次执行该脚本会下载很多资源,大概60G左右,以后就会差异更新了。同步好软件后,下一步就要配置web服务了,这样client端的yum程序才能下载我配置好的yum源,当然也可以配置ftp服务。我这里利用nginx作为web服务,并且编译时加入了fancyindex第三方插件,这样nginx在输出列表时看着舒服些。nginx配置mirrors.mysite.com站点如下:
server
{
listen 80;
server_name mirrors.mysite.com;
index index.html index.htm index.php;
root /data/soft/mirror.mysite.com;
location / {
fancyindex on;
fancyindex_exact_size off;
fancyindex_localtime on;
allow 124.42.53.137;
allow 203.86.81.196;
allow 192.168.99.0/24;
deny all;
}
access_log /var/log/nginx/access_yum.log access;
}

