> CentOS > CentOS服务器 > 文件同步 >

用rsync同步公网centos yum源做本地yum源服务器

用rsync同步公网centos yum源做本地yum源服务器


1,安装httpd,rsync,vsftpd
yum -y install httpd rsync vsftpd


mkdir /var/www/html/centos


#由于vsftpd不支持软链接改变目录,所以下面操作效果一样
mount --bind /var/www/html/centos /var/ftp/pub


chkconfig httpd on
chkconfig vsftpd on
service httpd start
service vsftpd start


2,rsync同步脚本如下

#!/bin/bash

# base value
# 要同步的源
YUM_SITE="rsync://mirrors.kernel.org/centos/"
# 本地存放目录
LOCAL_PATH="/var/www/html/centos/"
# 需要同步的版本,我只需要5和6版本还有7的,总共在120G左右
LOCAL_VER="5 5* 6 6* 7 7*"
# 同步时要限制的带宽
BW_limit=2048
# 记录本脚本进程号
LOCK_FILE="/var/log/yum_server.pid"
# 同步日志文件
LogFile=/root/rsyumlog/`date +"%Y-%m-%d"`.log
# 如用系统默认rsync工具为空即可。
# 如用自己安装的rsync工具直接填写完整路径
RSYNC_PATH=""

# check update yum server  pid
MY_PID=$$
if [ -f $LOCK_FILE ]; then
    get_pid=`/bin/cat $LOCK_FILE`
    get_system_pid=`/bin/ps -ef|grep -v grep|grep $get_pid|wc -l`
    if [ $get_system_pid -eq 0] ; then
        echo $MY_PID>$LOCK_FILE
    else
        echo "Have update yum server now!"
        exit 1
    fi
else
    echo $MY_PID>$LOCK_FILE
fi

# check rsync tool
if [ -z $RSYNC_PATH ]; then
    RSYNC_PATH=`/usr/bin/whereis rsync|awk ' ''{print $2}'`
    if [ -z $RSYNC_PATH ]; then
        echo 'Not find rsync tool.'
        echo 'use comm: yum install -y rsync'
    fi
fi

# sync yum source
echo "rsync start at $(date +"%Y-%m-%d %H:%M:%S")" >$LogFile
echo "--------------------------------------------------" >>$LogFile
for VER in $LOCAL_VER;
do
    # Check whether there are local directory
    if [ ! -d "$LOCAL_PATH$VER" ] ; then
        echo "Create dir $LOCAL_PATH$VER"
        `/bin/mkdir -p $LOCAL_PATH$VER`
    fi
    # sync yum source
     echo "Start sync $LOCAL_PATH$VER"  >>$LogFile
     echo "--------------------------------------------------" >>$LogFile
    $RSYNC_PATH -avrtH --delete --bwlimit=$BW_limit  $YUM_SITE$VER $LOCAL_PATH  >>$LogFile
done
echo "rsync end at $(date +"%Y-%m-%d %H:%M:%S")" >>$LogFile
echo "--------------------------------------------------" >>$LogFile
done

# clean lock file
`/bin/rm -rf $LOCK_FILE`

echo 'sync end.'
exit 1

3,计划任务
每天凌晨更新
23 59 * * *  /root/yumrsync

4,客户端配置
编辑/etc/yum.repos.d/CentOS-Base.repo
find /etc/yum.repos.d/  -type f  -name "*.repo"  -exec cp {} {}.bak \;
find /etc/yum.repos.d/  -name "*.repo" | xargs sed -i 's/#baseurl=http:\/\/mirror.centos.org/baseurl=http:\/\/192.168.0.100/g'
可用上面命令修改,得到结果如下:


#base
[base]
name=CentOS-$releasever – Base
baseurl=http://192.168.0.100/centos/$releasever/os/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6


#released updates
[updates]
name=CentOS-$releasever – Updates
baseurl=http://192.168.0.100/centos/$releasever/updates/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6


#additional packages that may be useful
[extras]
name=CentOS-$releasever – Extras
baseurl=http://192.168.0.100/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6


#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever – Plus
baseurl=http://192.168.0.100/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6


#contrib – packages by Centos Users
[contrib]
name=CentOS-$releasever – Contrib
baseurl=http://192.168.0.100/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6



(责任编辑:IT)