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

时间:2020-02-23 14:30:19  来源:igfitidea点击:

如何创建本地CentOS 6和CentOS 7存储库镜像的提示。
当我们拥有良好数量的系统时,这是有益的,并且我们希望在带宽上保存,请拧紧包装安装,网络安装和系统更新。

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

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

下面的脚本将同步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

这些Scipt可以在Cron中运行,以保持镜子电流。

用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;
        }
}

对于欧莱克:

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缓存来测试Repos工作正常。

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