centos搭建本地yum源

分享一个内网Yum同步脚本,可同时支持Centos6、Centos7(当然调整一下verlist参数也可以支持Centos8),可通过计划任务每天自动同步,经过多次修改完善,目前己稳定运行一年多。

 

1、rsync.sh同步脚本

#!/bin/bash

rsync="rsync -avrtH --bwlimit=10240 --delete --delete-delay --delay-updates"

# bakup sources 
# mirror_centos="rsync://mirror.xtom.com.hk/repo/centos"
# mirror_epel="rsync://mirror.xtom.com.hk/repo/epel"
mirror_centos="rsync://mirrors.tuna.tsinghua.edu.cn/centos"
mirror_epel="rsync://mirrors.tuna.tsinghua.edu.cn/epel"

local="/yum-repo/data"
verlist="6 7"
archlist="x86_64"

#only for centos
baselist="os updates extras centosplus"

echo "Start rsync at $(date +"%Y-%m-%d %H:%M:%S") ..." &>> /var/log/rsync_$(date +%Y%m%d).log
LOCK_FILE="/var/log/yum_repo_rsync.pid"

YUM_REPO_RSYNC_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
        #thread is gone,but lock file exist
    echo $YUM_REPO_RSYNC_PID>$LOCK_FILE
    else
    #thread exist,need check if thread is hangls
    time_now=`date +%s`
    time_lock=`stat -c %Y $LOCK_FILE`
    if [ $[ $time_now - $time_lock ] -gt 172800 ];then
        #if last task start over two days ,maybe is hang, kill it 
            kill -9 $get_pid
        echo $YUM_REPO_RSYNC_PID>$LOCK_FILE
        else
            echo "Update(rsync) process is running now!" &>> /var/log/rsync_$(date +%Y%m%d).log
            exit 1
    fi
    fi
else
    echo $YUM_REPO_RSYNC_PID>$LOCK_FILE
fi

echo "[1] Start sync centos GPG-KEY at $(date +"%Y-%m-%d %H:%M:%S") ..." &>> /var/log/rsync_$(date +%Y%m%d).log
mkdir -p $local/centos/
$rsync $mirror_centos/RPM-GPG-KEY-CentOS-6 $local/centos/ &>> /var/log/rsync_$(date +%Y%m%d).log
$rsync $mirror_centos/RPM-GPG-KEY-CentOS-7 $local/centos/ &>> /var/log/rsync_$(date +%Y%m%d).log

echo "[2] Start sync centos repo at $(date +"%Y-%m-%d %H:%M:%S") ..." &>> /var/log/rsync_$(date +%Y%m%d).log
for ver in $verlist
do
    for arch in $archlist
    do
        for base in $baselist
        do
            remote=$mirror_centos/$ver/$base/$arch/
        localdir=$local/centos/$ver/$base/$arch/
        echo "[3] Start sync [ $remote ]->[ $localdir ] at $(date +"%Y-%m-%d %H:%M:%S") ..." &>> /var/log/rsync_$(date +%Y%m%d).log
            mkdir -p $localdir
            $rsync $remote $localdir &>> /var/log/rsync_$(date +%Y%m%d).log
        done
    done
done

echo "[4] Start sync centos 7 openstack cloud repo  at $(date +"%Y-%m-%d %H:%M:%S") ..." &>> /var/log/rsync_$(date +%Y%m%d).log
mkdir -p $local/centos/7/cloud/x86_64/
$rsync $mirror_centos/7/cloud/x86_64/ $local/centos/7/cloud/x86_64/ &>> /var/log/rsync_$(date +%Y%m%d).log

echo "[5] Start sync epel GPG-KEY at $(date +"%Y-%m-%d %H:%M:%S") ..." &>> /var/log/rsync_$(date +%Y%m%d).log
mkdir -p $local/epel/
$rsync $mirror_epel/RPM-GPG-KEY-EPEL-6 $local/epel/ &>> /var/log/rsync_$(date +%Y%m%d).log
$rsync $mirror_epel/RPM-GPG-KEY-EPEL-7 $local/epel/ &>> /var/log/rsync_$(date +%Y%m%d).log

echo "[6] Start sync epel repo at $(date +"%Y-%m-%d %H:%M:%S") ..." &>> /var/log/rsync_$(date +%Y%m%d).log
for ver in $verlist
do
    for arch in $archlist
    do
        remote=$mirror_epel/$ver/$arch/
        localdir=$local/epel/$ver/$arch/
        echo "[7] Start sync [ $remote ]->[ $localdir ] at $(date +"%Y-%m-%d %H:%M:%S") ..." &>> /var/log/rsync_$(date +%Y%m%d).log
        mkdir -p $localdir
        $rsync $remote $localdir &>> /var/log/rsync_$(date +%Y%m%d).log
    done
done

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

echo "All repo sync end at $(date +"%Y-%m-%d %H:%M:%S") !" &>> /var/log/rsync_$(date +%Y%m%d).log
exit 1

 

2、计划任务设置,每天1:30开始自动同步
echo " 30  1  *  *  *   root    /yum-repo/rsync.sh" >>/etc/crontab
3、安装nginx(过程略),并修改nginx配置,启用文件目录服务
vi /etc/nginx/conf.d/default.conf

server {
     listen 80;
#     server_name localhost;

     location /
     {
        root /yum-repo/data/;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
     }

     location /nginx_status
     {
        stub_status on;
        access_log off;
        allow 192.168.0.0/16;
        deny all;
     }
}
4、在Yum服务器根目录放置内网源配置文件,192.168.0.1为Yum服务器地址,根据实际情况替换。
vi /yum-repo/data/CentOS-Internal.repo

# CentOS-Internal.repo

[base-internal]
name=CentOS-$releasever-internal
baseurl=http://192.168.0.1/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://192.168.0.1/centos/RPM-GPG-KEY-CentOS-$releasever

#released updates
[updates-internal]
name=CentOS-$releasever-internal
baseurl=http://192.168.0.1/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://192.168.0.1/centos/RPM-GPG-KEY-CentOS-$releasever

#additional packages that may be useful
[extras-internal]
name=CentOS-$releasever-internal
baseurl=http://192.168.0.1/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://192.168.0.1/centos/RPM-GPG-KEY-CentOS-$releasever

[epel-internal]
name=CentOS-Epel-$releasever-internal
baseurl=http://192.168.0.1/epel/$releasever/$basearch/
gpgcheck=1
gpgkey=http://192.168.0.1/epel/RPM-GPG-KEY-EPEL-$releasever
5、内网使用
cd /etc/yum.repos.d/
wget http://192.168.0.1/CentOS-Internal.repo
yum repolist

 

发表评论