搭建本地yum源并定时同步

时间:2021-09-12 21:50:34

在生产中内网的机器都是不能访问外网,所以需要搭建本地yum源。以中国科学科技大学的yum源为基准。http://mirrors.ustc.edu.cn/

有些模块会同步失败,可以wget下载至指定位置。

代码如下: 

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

RsyncPerm='-avSH --delete-after --no-iconv --bwlimit=5000'
Centos_6_base='/app/YumRepo/Centos6/x86_64/Base/'
Centos_6_update='/app/YumRepo/Centos6/x86_64/update/'
Centos_6_epel='/app/YumRepo/Centos6/x86_64/Epel'
Centos_6_salt='/app/YumRepo/Centos6/x86_64/Salt'
Centos_6_docker='/app/YumRepo/Centos6/x86_64/Docker'
Centos_7_base='/app/YumRepo/Centos7/x86_64/Base/'
Centos_7_update='/app/YumRepo/Centos7/x86_64/update/'
Centos_7_epel='/app/YumRepo/Centos7/x86_64/Epel'
Centos_7_salt='/app/YumRepo/Centos7/x86_64/Salt'
Centos_7_docker='/app/YumRepo/Centos7/x86_64/Docker'
LogFile='/app/YumRepo/rsync_yum_log'
Date=`date +%Y-%m-%d`

function CheckDir(){
if [ -d $1 ];then
        echo "$1 dir is exist,Please check it!"
else
        echo "$1 dir is not exist,will create it!"
    mkdir $1 -p
fi
}
function CheckFile(){
if [ -e $1 ];then
        echo "$1 dir is exist,Please check it!"
else
        echo "$1 dir is not exist,Please install on there!"
fi
}
function CheckStatus(){
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
}
CheckDir ${LogFile}
CheckDir ${Centos_6_base}
CheckDir ${Centos_6_update}
CheckDir ${Centos_6_epel}
CheckDir ${Centos_6_salt}
CheckDir ${Centos_6_docker}
CheckDir ${Centos_7_base}
CheckDir ${Centos_7_update}
CheckDir ${Centos_7_epel}
CheckDir ${Centos_7_salt}
CheckDir ${Centos_7_docker}


###rsync centos  base
echo 'Now start to rsync centos 6 base!' >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.ustc.edu.cn/centos/6/os/x86_64/ $Centos_6_base >>$LogFile/$Date.log
CheckStatus
echo 'Now start to rsync centos 7 base!' >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.ustc.edu.cn/centos/7/os/x86_64/ $Centos_7_base >>$LogFile/$Date.log
CheckStatus

###rsync epel
echo 'Now start to rsync centos 6 epel!' >>$LogFile/$Date.log
rsync  $RsyncPerm  rsync://mirrors.ustc.edu.cn/epel/6/x86_64/ $Centos_6_epel  >>$LogFile/$Date.log
CheckStatus
echo 'Now start to rsync centos 7 epel!' >>$LogFile/$Date.log
rsync  $RsyncPerm  rsync://mirrors.ustc.edu.cn/epel/7/x86_64/ $Centos_7_epel  >>$LogFile/$Date.log
CheckStatus

###rsync salt
echo 'Now start to rsync centos 6 salt!' >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.ustc.edu.cn/salt/yum/redhat/6/x86_64/ $Centos_6_salt >>$LogFile/$Date.log
CheckStatus
echo 'Now start to rsync centos 7 salt!' >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.ustc.edu.cn/salt/yum/redhat/7/x86_64/ $Centos_7_salt >>$LogFile/$Date.log
CheckStatus

###rsync docker
#echo 'Now start to rsync centos 6 docker!' >>$LogFile/$Date.log
#rsync $RsyncPerm rsync://mirrors.ustc.edu.cn/docker-yum/repo/centos6/ $Centos_6_docker >>$LogFile/$Date.log
#CheckStatus
echo 'Now start to rsync centos 7 docker!' >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.ustc.edu.cn/docker-ce/linux/centos/7/x86_64/stable/ $Centos_7_docker >>$LogFile/$Date.log
CheckStatus

###rsync golang
rsync $RsyncPerm rsync://mirrors.ustc.edu.cn/golang/go1.9.linux-arm64.tar.gz /app/YumRepo/golang/go1.9.linux-arm64.tar.gz 
CheckStatus

###rsync centos  update
echo 'Now start to rsync centos 6 update!' >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.ustc.edu.cn/centos/6/updates/x86_64/ $Centos_6_update >>$LogFile/$Date.log
CheckStatus
echo 'Now start to rsync centos 7 base!' >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.ustc.edu.cn/centos/7/updates/x86_64/ $Centos_7_update >>$LogFile/$Date.log
CheckStatus

同步完成后,启用httpd服务

配置DocumentRoot "/app/YumRepo"

ServerName *:80

<Directory "/app/YumRepo/">

  Options Indexes FollowSymLinks

  AllowOverride None

  Order allow,deny

  Allow from all

</Dircetory>

检查语法错误httpd -t

重载配置service httpd reload

配置客户端

把/etc/yum.repo.d/目录下的所有.repo文件。mv改为*.bak

新建文件vim /etc/yum.repo.d/CentOS-Base.repo

[base]
name=Centos6 - base
baseurl=baseurl=http://HOSTADDR/Centos6/x86_64/Base
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
 
[updates]
name=CentOS6 - updates
baseurl=http://HOSTADDR/Centos6/x86_64/update
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

重新生成缓存 

  yum clean all

  yum makecache