因目前企业生产网络禁止联网,对于使用Linux的我们来说,非常不方便,想要使用yum源都很困难,挂dvd又不能完全满足要求,所以自建一个企业级的yum源,定时从公网同步到本地,然后生产网络直接配置在本地yum源,完美解决问题。当然如果网络允许,你也可以协调网络维护的兄弟开通生产网络访问公有的yum源,比如阿里云yum,网易yum,清华大学yum。本文仅介绍本地yum源的搭建和同步方法。

1、资源准备

  • 准备一台Linux主机,磁盘不少于1T
  • 在网络上面找个rsync共享的yum源,比如清华大学的yum,mirrors.ustc.edu.cn
  • 编写同步脚本,下面为示例,各位兄弟可以根据自己的环境进行调整
  • 客户端yum配置文件编写及测试

2、同步脚本编写

1)CentOS8 yum源同步脚本

#!/bin/bash
#Script name:rsync_yumrepo.sh

RsyncBin="/usr/bin/rsync"
RsyncPerm='-avrt --delete --no-iconv'
# RsyncPerm='-avzP --delete --no-iconv'

Centos_8_base='/data/yum_repo/Centos-8/'

LogFile='/data/yum_repo/rsync_yum_log'
Date=`date +%Y-%m-%d`

#check
function check {
if [ $? -eq 0 ];then
    echo -e "\033[1;32mRsync is success!\033[0m" >>$LogFile/$Date.log
else
    echo -e "\033[1;31mRsync is fail!\033[0m" >>$LogFile/$Date.log
fi
}
if [ ! -d "$LogFile" ];then
    mkdir $LogFile
fi

#rsync centos 8
echo 'Now start to rsync centos 8 base!' >>$LogFile/$Date.log
$RsyncBin $RsyncPerm rsync://mirrors.ustc.edu.cn/centos/8/ $Centos_8_base >>$LogFile/$Date.log
check

2)CentOS7 yum源同步脚本

#!/bin/bash
#Script name:rsync_yumrepo.sh

RsyncBin="/usr/bin/rsync"
RsyncPerm='-avrt --delete --no-iconv'

Centos_7_base='/data/yum_repo/Centos-7/Base/'
Centos_7_epel='/data/yum_repo/Centos-7/Epel/'
Centos_7_rpmforge='/data/yum_repo/Centos-7/Rpmforge/'

LogFile='/data/yum_repo/rsync_yum_log'
Date=`date +%Y-%m-%d`

#check
function check {
if [ $? -eq 0 ];then
    echo -e "\033[1;32mRsync is success!\033[0m" >>$LogFile/$Date.log
else
    echo -e "\033[1;31mRsync is fail!\033[0m" >>$LogFile/$Date.log
fi
}
if [ ! -d "$LogFile" ];then
    mkdir $LogFile
fi

#rsync centos 7 base
echo 'Now start to rsync centos 7 base!' >>$LogFile/$Date.log
$RsyncBin $RsyncPerm rsync://mirrors.ustc.edu.cn/centos/7/os/ $Centos_7_base >>$LogFile/$Date.log
check
#rsync centos 7 epel
echo 'Now start to rsync centos 7 epel!' >>$LogFile/$Date.log
$RsyncBin  $RsyncPerm --exclude=SRPMS/ --exclude=ppc64/ rsync://mirrors.ustc.edu.cn/epel/7/ $Centos_7_epel  >>$LogFile/$Date.log
check
#rsync centos 7 rpmforge
echo 'Now start to rsync centos 7 x86_64 rpmforge!' >>$LogFile/$Date.log
$RsyncBin $RsyncPerm rsync://ftp-stud.fht-esslingen.de/dag/redhat/el7/en/x86_64/rpmforge/ $Centos_7_rpmforge/x86_64/ >>$LogFile/$Date.log
check

3)CentOS6 yum源同步脚本

#!/bin/bash
#Script name:rsync_yumrepo.sh

RsyncBin="/usr/bin/rsync"
RsyncPerm='-avrt --delete --no-iconv --bwlimit=1000'
Centos_6_base='/data/yum_repo/Centos-6/Base/'
Centos_6_epel='/data/yum_repo/Centos-6/Epel/'
Centos_6_rpmforge='/data/yum_repo/Centos-6/Rpmforge/'

LogFile='/data/yum_repo/rsync_yum_log'
Date=`date +%Y-%m-%d`
#check
function check {
if [ $? -eq 0 ];then
    echo -e "\033[1;32mRsync is success!\033[0m" >>$LogFile/$Date.log
else
    echo -e "\033[1;31mRsync is fail!\033[0m" >>$LogFile/$Date.log
fi
}
if [ ! -d "$LogFile" ];then
    mkdir $LogFile
fi
#rsync centos 6 base
echo 'Now start to rsync centos 6 base!' >>$LogFile/$Date.log
$RsyncBin $RsyncPerm rsync://mirrors.yun-idc.com/centos/6/os/ $Centos_6_base >>$LogFile/$Date.log
check
#rsync centos 6 epel
echo 'Now start to rsync centos 6 epel!' >>$LogFile/$Date.log
$RsyncBin  $RsyncPerm --exclude=SRPMS/ --exclude=ppc64/ rsync://mirrors.yun-idc.com/epel/6/ $Centos_6_epel  >>$LogFile/$Date.log
check
#rsync centos 6 rpmforge
echo 'Now start to rsync centos 6 i386 rpmforge!' >>$LogFile/$Date.log
$RsyncBin $RsyncPerm rsync://ftp-stud.fht-esslingen.de/dag/redhat/el6/en/i386/rpmforge/ $Centos_6_rpmforge/i386/ >>$LogFile/$Date.log
check
echo 'Now start to rsync centos 6 x86_64 rpmforge!' >>$LogFile/$Date.log
$RsyncBin $RsyncPerm rsync://ftp-stud.fht-esslingen.de/dag/redhat/el6/en/x86_64/rpmforge/ $Centos_6_rpmforge/x86_64/ >>$LogFile/$Date.log
check

3、定时任务同步示例

crontab -e  # 填入如下内容
8 2 * * * /data/yum_repo/rsync_yum_shell/rsync_centos_yum.sh > /dev/null 2>&1

4、客户端配置实例

[Base]
name=CentOS-Base7-$basearch
baseurl=http://yum.evs168.com/Centos-7/Base/$basearch
enable=1
gpgcheck=1
gpgkey=http://yum.evs168.com/Centos-7/Base/$basearch/RPM-GPG-KEY-CentOS-7

[Rpmforge]
name=CentOS-Base7-Rpmforge-$basearch
baseurl=http://yum.evs168.com/Centos-7/Rpmforge/$basearch
enable=1
gpgcheck=0

[Epel]
name=CentOS-Base7-Epel-$basearch
baseurl=http://yum.evs168.com/Centos-7/Epel/$basearch
enable=1
gpgcheck=0

配置完成后,执行如下命令清除本地yum缓存,重新建立即可。

[root@yum-server01 ~]# yum clean all
Loaded plugins: fastestmirror, security
Cleaning repos: CentOS-6-Base Epel Rpmforge evunrpms
Cleaning up Everything
Cleaning up list of fastest mirrors
[root@yum-server01 ~]# yum makecache
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
CentOS-6-Base                                                                                                                                                                                                                                   | 3.7 kB     00:00 ...
CentOS-6-Base/group_gz                                                                                                                                                                                                                          | 226 kB     00:00 ...
CentOS-6-Base/filelists_db                                                                                                                                                                                                                      | 6.4 MB     00:00 ...
CentOS-6-Base/other_db                                                                                                                                                                                                                          | 2.8 MB     00:00 ...
Epel                                                                                                                                                                                                                                            | 4.3 kB     00:00 ...
Epel/filelists_db                                                                                                                                                                                                                               | 7.7 MB     00:00 ...
Epel/other_db                                                                                                                                                                                                                                   | 2.9 MB     00:00 ...
Rpmforge                                                                                                                                                                                                                                        | 1.9 kB     00:00 ...
Rpmforge/filelists_db                                                                                                                                                                                                                           | 2.3 MB     00:00 ...
Rpmforge/other_db                                                                                                                                                                                                                               | 575 kB     00:00 ...
localrpms                                                                                                                                                                                                                                        | 2.9 kB     00:00 ...
localrpms/filelists_db                                                                                                                                                                                                                           | 190 kB     00:00 ...
localrpms/other_db                                                                                                                                                                                                                               | 8.8 kB     00:00 ...
Metadata Cache Created

4、自定义rpm包加入yum源

实际生产过程中,我们可能要自定义各类的源码包,打包成rpm包,也可以将这些rpm包加入到本地yum源中

在yum服务器上面创建目录,使用如下命令创建本地yum仓库,然后使用http或ftp共享出去,在客户机配置下即可。

1)创建本地自定义rpm包存放目录

mkdir /data/yum_repo/Centos-8

2)删除repodata,如果有(第一次是没有这个目录的)

rm -fr repodata

3)创建新的repodata目录

createrepo .

4)客户端配置

[localrpms]
name=LocalRepo
baseurl=http://yum.evs168.com/localrpms
enable=1
gpgcheck=0
12-30 23:26