1. 程式人生 > >互聯網同步yum服務器,中科大 rsync createrepo

互聯網同步yum服務器,中科大 rsync createrepo

modules client exc dir this pack 無法 端口 color

參考文章

https://blog.csdn.net/chenjia6605/article/details/82734945

1、本機安裝所需工具:

yum -y install rsync createrepo

2、創建目錄(位置隨意):

1)、centos倉庫目錄,centosplus可以不同步,一般用不到:

mkdir -p /storage/repos/centos/7/{os,updates,extras,centosplus}/x86_64

2)、epel倉庫目錄:

mkdir -p /storage/repos/epel/7/x86_64

#如果需要EPEL軟件的源碼,請同時創建以下目錄

mkdir -p /storage/repos/epel/7/SRPMS/

3、同步遠程鏡像(該過程需要很長時間,與你的外網帶寬有關,如果你需要將centos的官方資源和epel資源都同步的話,則至少需要80G的磁盤空間,為了避免走彎路,磁盤分區的時候要考慮一下存放rpm包目錄被掛載的大小)

以下為我做完centos官方資源和epel資源之後磁盤使用情況

技術分享圖片

#同步centos官方資源

rsync -avz --delete --exclude=‘repodata‘ rsync://mirrors.ustc.edu.cn/centos/7/os/x86_64/ /storage/repos/centos/7/os/x86_64/

rsync -avz --delete --exclude=‘repodata‘ rsync://mirrors.ustc.edu.cn/centos/7/updates/x86_64/ /storage/repos/centos/7/updates/x86_64/

rsync -avz --delete --exclude=‘repodata‘ rsync://mirrors.ustc.edu.cn/centos/7/extras/x86_64/ /storage/repos/centos/7/extras/x86_64/

rsync -avz --delete --exclude=‘repodata‘ rsync://mirrors.ustc.edu.cn/centos/7/centosplus/x86_64/ /storage/repos/centos/7/centosplus/x86_64/

#同步epel資源(不知道是rsync本身的事還是中科大對epel源的速度做了限制,上面同步centos官方源的資源的時候速度很快,基本上能達到本地網絡的包和帶寬,但是同步epel資源的時候,速度極慢,如果哪位大牛有更快的方法可以在評論區教一下小弟我)

rsync -avz --delete --exclude=‘repodata‘ rsync://mirrors.ustc.edu.cn/epel/7/x86_64/ /storage/repos/epel/7/x86_64/

rsync -avz --delete --exclude=‘repodata‘ rsync://mirrors.ustc.edu.cn/epel/7/SRPMS/ /storage/repos/epel/7/SRPMS/

#同步gpgkey

rsync -avz --delete --exclude=‘repodata‘ rsync://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-7 /storage/repos/centos/

---------------------

創建索引centos官方包的索引

createrepo /storage/repos/centos/7/os/x86_64/

createrepo /storage/repos/centos/7/updates/x86_64/

createrepo /storage/repos/centos/7/extras/x86_64/

createrepo /storage/repos/centos/7/centosplus/x86_64/

epel擴展源索引

createrepo /storage/repos/epel/7/x86_64/

createrepo /storage/repos/epel/7/SRPMS/

5、同步腳本,如果你的服務器一直連接外網可以配置在定時任務裏,定期與遠程鏡像保持同步:

vi /etc/cron.daily/update-repos

#腳本內容開始

# create new

#!/bin/bash

VER=‘7‘

ARCH=‘x86_64‘

CENTOS_REPOS=(os updates extras centosplus)

#同步centos鏡像

for REPO in ${CENTOS_REPOS[@]}

do

rsync -avz --delete --exclude=‘repodata‘ rsync://mirrors.ustc.edu.cn/centos/${VER}/${CENTOS_REPOS}/${ARCH}/ /storage/repos/centos/${VER}/${CENTOS_REPOS}/${ARCH}/

createrepo /storage/repos/centos/${VER}/${CENTOS_REPOS}/${ARCH}/

done

#同步gpgkey

rsync -avz --delete --exclude=‘repodata‘ rsync://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-7 /storage/repos/centos/

#同步epel鏡像

rsync -avz --delete --exclude=‘repodata‘ rsync://mirrors.ustc.edu.cn/epel/7/x86_64/ /storage/repos/epel/7/x86_64/

createrepo /storage/repos/epel/7/x86_64/

#如果需要epel軟件的源碼,同步epel軟件源碼倉庫

#rsync -avz --delete --exclude=‘repodata‘ rsync://mirrors.ustc.edu.cn/epel/7/SRPMS/ /storage/repos/epel/7/SRPMS/

#createrepo /storage/repos/epel/7/SRPMS/

#同步gpgkey

rsync -avz --delete --exclude=‘repodata‘ rsync://mirrors.ustc.edu.cn/epel/RPM-GPG-KEY-EPEL-7 /storage/repos/epel/

# wq 保存退出後,給腳本賦予可執行權限

# chmod 755 /etc/cron.daily/update-repo

#腳本內容結束

6、安裝nginx

yum install -y nginx

以下為nginx.conf配置文件的內容

#nginx.conf內容開始

# For more information on configuration, see:

# * Official English Documentation: http://nginx.org/en/docs/

# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;

worker_processes auto;

error_log /var/log/nginx/error.log;

pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.

include /usr/share/nginx/modules/*.conf;

events {

worker_connections 1024;

}

http {

log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘

‘$status $body_bytes_sent "$http_referer" ‘

‘"$http_user_agent" "$http_x_forwarded_for"‘;

access_log /var/log/nginx/access.log main;

sendfile on;

tcp_nopush on;

tcp_nodelay on;

keepalive_timeout 65;

types_hash_max_size 2048;

include /etc/nginx/mime.types;

default_type application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.

# See http://nginx.org/en/docs/ngx_core_module.html#include

# for more information.

include /etc/nginx/conf.d/*.conf;

server {

listen 80 default_server;

listen [::]:80 default_server;

server_name _;

root /storage/repos; #此處為網站根目錄,請指向以上創建的repos目錄

# Load configuration files for the default server block.

include /etc/nginx/default.d/*.conf;

location / {

autoindex on; #打開目錄瀏覽功能

autoindex_exact_size off; # off:以可讀的方式顯示文件大小

autoindex_localtime on; # onoff:是否以服務器的文件時間作為顯示的時間

charset utf-8,gbk; #展示中文文件名

index index.html;

}

error_page 404 /404.html;

location = /40x.html {

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

}

}

# Settings for a TLS enabled server.

#

# server {

# listen 443 ssl http2 default_server;

# listen [::]:443 ssl http2 default_server;

# server_name _;

# root /usr/share/nginx/html;

#

# ssl_certificate "/etc/pki/nginx/server.crt";

# ssl_certificate_key "/etc/pki/nginx/private/server.key";

# ssl_session_cache shared:SSL:1m;

# ssl_session_timeout 10m;

# ssl_ciphers HIGH:!aNULL:!MD5;

# ssl_prefer_server_ciphers on;

#

# # Load configuration files for the default server block.

# include /etc/nginx/default.d/*.conf;

#

# location / {

# }

#

# error_page 404 /404.html;

# location = /40x.html {

# }

#

# error_page 500 502 503 504 /50x.html;

# location = /50x.html {

# }

# }

}

#nginx.conf內容結束

7、測試nginx

重啟nginx服務

systemctl restart nginx.service

之後使用其他主機的瀏覽器連接該主機的80端口,能夠看到yum源的目錄即可,如果無法訪問,請檢查防火墻,selinux等,和nginx.conf。

8、客戶端配置

修改客戶端的repo文件內容

repo文件位於/etc/yum.repos.d/

以下為CentOS-Base.repo文件的全部內容,也可以刪除所有註釋行,僅保留生效的代碼,也可以僅使用絕對路徑的url連接

#CentOS-Base.repo文件內容開始

# CentOS-Base.repo

#

# The mirror system uses the connecting IP address of the client and the

# update status of each mirror to pick mirrors that are updated to and

# geographically close to the client. You should use this for CentOS updates

# unless you are manually picking other mirrors.

#

# If the mirrorlist= does not work for you, as a fall back you can try the

# remarked out baseurl= line instead.

#

#

[base]

name=CentOS-$releasever - Base

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra

#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/

baseurl=http://192.168.197.40/centos/$releasever/os/$basearch/

gpgcheck=1

#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

gpgkey=http://192.168.197.40/centos/RPM-GPG-KEY-CentOS-7

#released updates

[updates]

name=CentOS-$releasever - Updates

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra

#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/

baseurl=http://192.168.197.40/centos/$releasever/updates/$basearch/

gpgcheck=1

#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

gpgkey=http://192.168.197.40/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful

[extras]

name=CentOS-$releasever - Extras

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra

#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/

baseurl=http://192.168.197.40/centos/$releasever/extras/$basearch/

gpgcheck=1

#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

gpgkey=http://192.168.197.40/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages

[centosplus]

name=CentOS-$releasever - Plus

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra

#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/

baseurl=http://192.168.197.40/centos/$releasever/centosplus/$basearch/

gpgcheck=1

enabled=1

#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

gpgkey=http://192.168.197.40/centos/RPM-GPG-KEY-CentOS-7

[epel]

name=Extra Packages for Enterprise Linux 7 - $basearch

#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch

#metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch

baseurl=http://192.168.197.40/epel/7/$basearch

failovermethod=priority

enabled=1

gpgcheck=1

#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

gpgkey=http://192.168.197.40/epel/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]

name=Extra Packages for Enterprise Linux 7 - $basearch - Debug

#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch/debug

#metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch

baseurl=http://192.168.197.40/epel/7/$basearch/debug

failovermethod=priority

enabled=0 #此項1表示開啟,0表示關閉

#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

gpgkey=http://192.168.197.40/epel/RPM-GPG-KEY-EPEL-7

gpgcheck=1

[epel-source] #如果已同步SRPMS倉庫,請取消該配置註釋

name=Extra Packages for Enterprise Linux 7 - $basearch - Source

#baseurl=http://download.fedoraproject.org/pub/epel/7/SRPMS

#metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch

baseurl=http://192.168.197.40/epel/7/SRPMS

failovermethod=priority

enabled=1

#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

gpgkey=http://192.168.197.40/epel/RPM-GPG-KEY-EPEL-7

gpgcheck=1

#CentOS-Base.repo文件內容結束

9,客戶端清除yum緩存,並重新制作緩存

yum clean all

yum makecache

10,後記

讓yum服務器支持yum grouplist 需要添加xml文件,具體步驟為:

插入centos的系統的everything包,然後掛載光盤,找到光盤目錄下/mnt/cdrom/repodata中的xml文件,改文件名字為:*-c7-x86_64-comps.xml,*代表亂碼。

這裏的名字是d87379a47bc2060f833000b9cef7f9670195fe197271d37fce5791e669265e8b-c7-x86_64-comps.xml

拷貝該文件到yum服務器中,例如拷貝到/storage/repos/,然後使用ceraterepo -g 使用該文件重建組索引

具體命令為

createrepo -g /storage/repos/d87379a47bc2060f833000b9cef7f9670195fe197271d37fce5791e669265e8b-c7-x86_64-comps.xml /storage/repos/centos/7/os/x86_64/

指定的xml文件要使用絕對路徑,後面的路徑是rpm包的上層Packages目錄的所在目錄。

執行成功後,會在Packages同級目錄下的repodata目錄下創建新的xml文件。

同理,如果yum源服務器內除了base源服務,還提供其他源服務的話,那麽如果存在軟件包組的話,那麽也需要得到對應源服務的xml文件,然後使用createrepo -g 命令來重新創建軟件包組的索引信息。

11,疑問

技術分享圖片

如圖所示,上面的yum makecache 信息為通過以上教程自建的yum源,下面的使用centos默認配置的yum源,有沒有叼大的說一下為啥我使用人家的yum生成緩存的時候就有prestodelta 用我自建的yum就沒有?prestodelta 裏面的內容又是啥,何種應用場景?

互聯網同步yum服務器,中科大 rsync createrepo