如何创建本地CentOS 7/CentOS 6/EPEL存储库同步镜像

时间:2020-02-23 14:37:58  来源:igfitidea点击:

本教程将介绍有关如何创建本地CentOS 6和CentOS 7存储库镜像的提示。当我们拥有大量系统并且想要节省带宽,加快软件包安装,网络安装和系统更新时,这将非常有用。

本指南将使用rsync从上游同步镜像。用于保存同步内容的路径是/var/mirrors /。确保调整路径到具有足够空间的文件系统,或者安装新的磁盘/分区。

同步CentOS 6和7镜像的Bash脚本:

下面的脚本将同步CentOS 6和CentOS 7镜像。用所需的目录变量替换。将脚本保存到文件,制作

#!/bin/bash

if [ -f /var/lock/subsys/rsync_updates ]; then
    echo "Updates via rsync already running."
    exit 0
fi

base_dir="/var/mirrors/centos/"
centos_7_mirror_dir="/var/mirrors/centos/7/"
centos_6_mirror_dir="/var/mirrors/centos/6/"

touch /var/lock/subsys/rsync_updates

if [[ -d "$centos_7_mirror_dir"  && -d "$centos_6_mirror_dir" ]] ; then
    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/7/ "$centos_7_mirror_dir" && \
    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/6/ "$centos_6_mirror_dir" && \
    # sync keys
    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/RPM-GPG-KEY-CentOS-7  "$base_dir" && \
    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/RPM-GPG-KEY-CentOS-6  "$base_dir" && \
    rm -rf /var/lock/subsys/rsync_updates

else
	echo "Directories doesn't exist"

fi

Bash脚本同步Epel镜像:

#!/bin/bash

if [ -f /var/lock/subsys/rsync_updates ]; then
    echo "Updates via rsync already running."
    exit 0
fi

epel6_mirror_dir="/var/mirrors/epel/6/x86_64"
epel7_mirror_dir="/var/mirrors/epel/7/x86_64"
base_dir="/var/mirrors/epel/"

touch /var/lock/subsys/rsync_updates

if [[ -d "$epel6_mirror_dir"  && -d "$epel7_mirror_dir" ]] ; then
    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/6/x86_64/"$epel6_mirror_dir" && \
    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/7/x86_64/"$epel7_mirror_dir" && \
    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/RPM-GPG-KEY-EPEL-7 "$base_dir" && \
    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/RPM-GPG-KEY-EPEL-6 "$base_dir" && \
    rm -rf /var/lock/subsys/rsync_updates

else
	echo "Directories doesn't exist"

fi

if [[ $? -eq '0' ]]; then
    echo ""
    echo "Sync successful.."
else
    echo " Syncing failed"
    exit 1
fi

可以在cron中运行这些cipt,以使镜像保持最新状态。

用Nginx服务镜子

同步完成后,我们可以通过与nginx一起使用来开始使用它们,

安装nginx:

yum -y install epel-release
yum -y install nginx

然后添加配置文件:

cat /etc/nginx/conf.d/centos.conf
server {
	listen 80;
	server_name centos.mirrors.domain.com;
        root /var/mirrors/centos/;

       location/{
                autoindex on;
        }
}

对于EPEL:

cat /etc/nginx/conf.d/epel.conf
server {
	listen 80;
	server_name epel.mirrors.domain.com;
        root /var/mirrors/epel;

       location/{
                autoindex on;
        }
}

启动并启用nginx:

systemctl start nginx
systemctl enable nginx

配置CentOS服务器以使用镜像

CentOS基本镜像:

cd /etc/yum.repos.d/
sudo mkdir old
sudo mv CentOS* old
sudo vim centos-local.repo

在下面添加内容。

[base]
name=CentOS-$releasever - Base
baseurl=http://centos.mirrors.domain.com/$releasever/os/$basearch/
enabled=1
gpgcheck=1
gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-$releasever - Updates
baseurl=http://centos.mirrors.domain.com/$releasever/updates/$basearch/
enabled=1
gpgcheck=1
gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-$releasever - Extras
baseurl=http://centos.mirrors.domain.com/$releasever/extras/$basearch/
enabled=0
gpgcheck=1
gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7

[centosplus]
name= CentOS-$releasever - Plus
baseurl=http://centos.mirrors.domain.com/$releasever/centosplus/$basearch/
enabled=0
gpgcheck=1
gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7

通过更新仓库Repo缓存来测试仓库是否正常运行。

sudo yum clean all
sudo yum makecache fast
sudo yum -v repolist

epel镜像:

$sudo vim /etc/yum.repos.d/epel.repo
[epel]                                    
name=Extra Packages for Enterprise Linux 7 - $basearch                               
baseurl=http://epel.mirrors.domain.com/$releasever/$basearch/                        
enabled=1                                 
gpgcheck=1                                
gpgkey=http://epel.mirrors.domain.com/RPM-GPG-KEY-EPEL-$releasever