定制化rpm包及本地yum仓库搭建以及同步支持rsync的源和同步不支持rsync源

https://www.cnblogs.com/keithtt/p/7108248.html

wget递归下载整站

由于线上跑的系统还有CentOS5.4、6.4、6.5、6.5、6.6、6.8,而各镜像站维护的最早的版本已经是6.9,所以需要爬archive站点的rpm包来自建yum仓库。

# wget -r -p -np -k http://archives.fedoraproject.org/pub/archive/epel/5Server/x86_64/
# wget -r -p -np -k http://archives.fedoraproject.org/pub/epel/6Server/x86_64/

-c, --continue resume getting a partially-downloaded file. 断点续传
-nd, --no-directories don't create directories. 不创建层级目录,所有文件下载到当前目录
-r, --recursive specify recursive download. 递归下载
-p, --page-requisites get all images, etc. needed to display HTML page.
下载页面所有文件,使页面能在本地打开
-k, --convert-links make links in downloaded HTML or CSS point to local files.
转换链接指向本地文件
-np, --no-parent don't ascend to the parent directory. 不下载父级目录的文件
-o, --output-file=FILE log messages to FILE. 指定日志输出文件
-O, --output-document=FILE write documents to FILE. 指定文件下载位置
-L, --relative follow relative links only. 只下载相对链接,如果页面嵌入其他站点不会被下载

wget -r -p -np -k -P . linux.csie.nctu.edu.tw

-P 表示下载到哪个目录
-r 表示递归下载 
-np 表示不下载旁站连接. 
-k 表示将下载的网页里的链接修改为本地链接.
-p 获得所有显示网页所需的元素

================================================

为方便本地yum的管理,一般都是在公司局域网内搭建本地yum仓库,实现公司内部快速安装常用软件。

步骤如下:

1、搭建要实现本地yum管理的软件,测试该软件搭建成功与否;

2、定制rpm包及其相关依赖;

3、搭建本地yum源

4、配置客户端yum,实现局域网取包安装。

 

首先声明:该本地yum仓库中的包要尽量全,不然有些定制包可能安装不上。原因很简单,该包原本就需要一些其他的依赖包,而你的本地仓库没有。

 

安装nginx,测试安装是否成功

1.

#先保留平常下载下来的rpm包

sed -i 's#keepcache=0#keepcache=1#g' /etc/yum.conf

这样当我们yum安装软件的时候,文件就是被保存下来:

/var/cache/yum/

#部署nginx,提供80端口服务

#安装依赖包

yum install  -y pcre-devel openssl-devel ;

rpm -qa  pcre-devel openssl-devel

#下载nginx-10.0.2

mkdir /tmp/tools

cd /tmp/tools/

wget -q http://nginx.org/download/nginx-1.10.2.tar.gz

tar xf nginx-1.10.2.tar.gz

cd nginx-1.10.2

mkdir /application

#配置

./configure  --user=www --group=www --prefix=/application/nginx-1.10.2 --with-http_stub_status_module  --with-http_ssl_module

#编译

make && make install

#做软连接

ln -s /application/nginx-1.10.2 /application/nginx

添加www用户:

useradd -u 789 www -s /sbin/nologin  -M

#启动nginx进行测试

/applicaton/nginx/sbin/nginx

 

lsof -i:80

关闭nginx 为定制rpm包做准备:

pkill   nginx

 

#定制rpm包

####下载工具及配置

FPM安装

#fpm是ruby写的,因此系统环境需要ruby,且ruby版本号大于1.8.5

#安装ruby模块

yum -y install ruby rubygems ruby-devel

检查安装成功与否

rpm -qa ruby rubygems ruby-devel

#添加阿里云的rubygems仓库,外国的源太慢

gem sources -ahttp://mirrors.aliyun.com/rubygems/ http://mirrors.aliyun.com/rubygems/ added to sources

#移除原生的Ruby仓库

gem sources --remove http://rubygems.org/ http://rubygems.org/ removed from sources

#安装fpm

#指定安装fpm1.3.3版本的软件,fpm这个工具升级很频繁,而且在每次升级后会出现各种各样的问题。但是老版本的fpm工具能够满足我们的需求,因此就用这个版本了。

#这里一定要指定fpm的版本,不然会报错,报错原因可追溯到版本频繁升级的问题导致。

gem install fpm -v 1.3.3

开始定制nginx 的rpm包

#创建nginx的rpm包要执行的脚本

cat  >  /server/scripts/nginx_rpm.sh   << eof

#!/bin/bash

 

useradd -u 789 www -M -s /sbin/nologin

ln -s /application/nginx-1.10.2/ /application/nginx

eof

#通过fpm命令打包rpm

fpm -s dir -t rpm -n nginx -v 1.10.2 -d 'pcre-devel,openssl-devel' --post-install /server/scripts/nginx_rpm.sh  -f /application/nginx-1.10.2/

#fpm相关命令说明

#    fpm -h #查看命令的帮助,下面对常用的参数进行简单的说明

#    -s:指定源类型

#    -t:指定目标类型

#    -n:指定名字

#    -v:指定版本号

#    -C:指定打包的相对路径

#    -d:指定依赖于哪些包

#    -f:第二次打包时目录下如果有同名安装包存在,则覆盖它

#    -p:输出的安装包的目录,不想放在当前目录下就需要指定

#    --post-install  软件包安装完成之后所要运行的脚本;同--after-install

#    --pre-install  软件包安装完成之前所要运行的脚本;同--before-install

#    --post-uninstall   软件包卸载完成之后所要运行的脚本;同--after-remove

#    --pre-uninstall    软件包卸载完成之前所要运行的脚本;同--before-remove

#查看rpm包信息

rpm -qpi nginx-1.10.2-1.x86_64.rpm

#查看rpm包内容

rpm -qpl nginx-1.10.2-1.x86_64.rpm

#查看rpm的包依赖

rpm -qpR nginx-1.10.2-1.x86_64.rpm

#查看rpm自带的执行脚本,执行脚本不是以文件的形式存在rpm包中

rpm -qp --scripts nginx-1.10.2-1.x86_64.rpm

 

创建本地yum仓库

创建仓库目录

mkdir /application/yum/centos6/x86_64 -p

将生成的nginx  rpm包放到该目录

mv nginx-1.10.2-1.x86_64.rpm /application/yum/centos6/x86_64/

安装createrepo

yum -y install createrepo

初始化repodata索引文件

createrepo -pdo /application/yum/centos6/x86_64/  /application/yum/centos6/x86_64/

#修改nginx配置文件,让它默认访问浏览器找到/application/yum/centos6/x86_64/目录

cat  >/application/nginx/conf/nginx.conf<<EOF

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;

        server_name  localhost;

        location / {

            root   /application/yum/centos6/x86_64/;

            autoindex on;         ##开启目录

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

}

EOF

检查nginx配置问,启动nginx:

/application/nginx/sbin/nginx -t

/application/nginx/sbin/nginx

使用yum下载nginx依赖包,拷贝到本地yum源目录:

yumdownloader pcre-devel openssl-devel

mv openssl* pcre* /application/yum/centos6/x86_64/

#每加入一个rpm包就要执行一下如下命令,用来更新索引

createrepo --update /application/yum/centos6/x86_64

 

配置客户端

创建脚本

cat >  /etc/yum.repos.d/local.repo  << eof

[local]

name=Server

baseurl=http://172.16.1.61

enable=1

gpgcheck=0

eof

yum操作、清除缓存、重新建立缓存;

yum clean all

指定指定使用local源,此命令重启linux系统失效:

yum --enablerepo=local --disablerepo=base,extras,updates,epel list

# 这条命令中的local名称一定要对应local.repo中的[local]

#注意最下面白色的文件列表

永久生效法:

vi CentOS-Base.repo

#在每一个启动的源加上

enabled=0   #改为1就启用,没有此参数也是启用。

安装nginx

yum install nginx

 

~~~~~~~~~~~~~~~~~~~~~~~~~

 

同步支持rsync:上游yum源必须要支持rsync协议,否则不能使用rsync进行同步。

http://www.ppzedu.com/archives/18.html

rsync同步公网yum源

通常在生产中,我们的内网机器是无法通信到公网的。但是当我们需要安装软件的时候,就需要使用yum源。每次通过公网的机器下载rpm包,然后上传当然也可以,但是工作量过于麻烦。因此搭建一个内网可通信yum源既安全,又是自动化运维的第一个步骤。下面就来记录下,使用rsync同步yum源的方案。欢迎拍砖!

[root@hxzy-test yum.repos.d]# yum install nginx
[root@hxzy-test ~]# cd /etc/nginx/conf.d/
[root@hxzy-test conf.d]# vi yum.conf
server {
     listen *:80;
     server_name localhost;
     autoindex on;   
     autoindex_exact_size off;   
     autoindex_localtime on; 
#   limit_conn one 8;       
     limit_rate 5000k;         
 
      allow 192.168.1.0/24;    
#     allow 172.16.0.0/16;
#     deny  all; 
 
     location /
     {
             index index.html index.htm;
             root /usr/share/nginx/html;
     }
     location /nginx_status {       
     stub_status on;
     access_log off;
     allow 192.168.1.0/24;
     deny all;
 
     }
}
 
[root@hxzy-test conf.d]# service nginx start
[20160624_10:47:51]Starting nginx:                                            [  OK  ]

对于我们不需要的yum源版本,我们排除掉。

[root@xmapp100-030 yum]# cat exclude_centos.list
2
2.1
3
3.1
3.3
3.4
3.5
3.6
3.7
3.8
3.9
4
7
Archive-Update-in-Process-mirrors.ustc.edu.cn
HEADER.html
RPM-GPG-KEY-CentOS-3
RPM-GPG-KEY-CentOS-4
RPM-GPG-KEY-CentOS-5
RPM-GPG-KEY-CentOS-7
RPM-GPG-KEY-CentOS-Debug-7
RPM-GPG-KEY-CentOS-Testing-7
RPM-GPG-KEY-beta
RPM-GPG-KEY-centos4
TIME
dir_sizes
filelist.gz
timestamp.txt
2.1
3.9
4.0
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
4.9
5.0
5.1
5.10
5.11
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9
5
6.0
6.1
6.2
6.3
6.4
6.5
6.6
6.7
7.0.1406
7.1.1503
7.2.1511
HEADER.images
build
dostools
graphics

排除掉epel源中的镜像源目录

[root@hxzy-test ~]# cat exclude_epel.list
4AS
4ES
4WS
5Client
5Server
RPM-GPG-KEY-EPEL-4
RPM-GPG-KEY-EPEL-5
RPM-GPG-KEY-EPEL-7
epel-release-latest-5.noarch.rpm
epel-release-latest-6.noarch.rpm
epel-release-latest-7.noarch.rpm
fullfilelist
4
5
7
testing
 
 
[root@hxzy-test conf.d]# mkdir -p /usr/local/nginx/html/centos
[root@hxzy-test conf.d]# mkdir -p /usr/local/nginx/html/repoforge
[root@hxzy-test conf.d]# mkdir -p /usr/local/nginx/html/epel

##rsync_同步脚本

[root@hxzy-test ~]#
#!/bin/sh
/usr/bin/rsync -avrt rsync://rsync.mirrors.ustc.edu.cn/centos/ --exclude-from="/etc/nginx/conf.d/yum/exclude_centos.list" /usr/share/nginx/html/centos/
#/usr/bin/rsync -avrt rsync://mirrors.ispros.com.bd/repoforge/ --exclude-from="/etc/nginx/conf.d/yum/exclude_repoforge.list" /usr/share/nginx/html/repoforge/
/usr/bin/rsync -avrt rsync://rsync.mirrors.ustc.edu.cn/epel/ --exclude-from="/etc/nginx/conf.d/yum/exclude_epel.list" /usr/share/nginx/html/epel/
 
计划任务如下
[root@hxzy-test yum]# vi rsync_yum.sh 
23 19 * * * /bin/sh /etc/nginx/conf.d/yum/yum_rsync.sh  >> /tmp/yum.log
赞 11
赏

 

 

扩展:

但还有一种企业需求,说的更具体一点,平时大家yum安装软件都是从公网下载的,占用带宽,因此在公司里搭建一个内网yum服务器,但又考虑到如果yum软件的数据库文件repodata不一样,就会有问题。因此我想到的解决方法就是直接使用公网yum源的repodata。

 

 

镜像同步公网yum源

上游yum源必须要支持rsync协议,否则不能使用rsync进行同步。

http://mirrors.ustc.edu.cn/status/

CentOS官方标准源:rsync://mirrors.ustc.edu.cn/centos/

epel源:rsync://mirrors.ustc.edu.cn/epel/

 

同步命令:

# 使用rsync同步yum源,为了节省带宽、磁盘和下载时间,我只同步了CentOS6的rpm包,这样所有的rpm包只占用了21G,全部同步需要300G左右。

# 同步base源,小技巧,我们安装系统的光盘镜像含有部分rpm包,大概3G,这些就不用重新下载。

 

#创建四个目录,用于同步公网yum源

mkdir -p /application/yum/centos/6/os/x86_64/

mkdir -p /application/yum/centos/6/extras/x86_64/

mkdir -p /application/yum/centos/6/updates/x86_64/

mkdir -p /application/yum/epel/6/x86_64/

 

#下面四条命令,同时复制执行,就会开始同步官网yum源到本地。

1 /usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/os/x86_64/ /application/yum/centos/6/os/x86_64/

2 /usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/extras/x86_64/ /application/yum/centos/6/extras/x86_64/

3 /usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/updates/x86_64/ /application/yum/centos/6/updates/x86_64/

4 /usr/bin/rsync -av --exclude=debug rsync://mirrors.ustc.edu.cn/epel/6/x86_64/ /application/yum/epel/6/x86_64/

 
 

同步不支持rsync:无所谓上游是什么情况,都可以

扩展:

 

 之前总结的搭建本地yum源是通过rsync直接同步其他在线的yum源,例如清华大学大YUM源,但是类似的阿里云的yum源,因为其不支持rsync所以不能进行同步,

  同样的,想要同步其他一些官方的YUM源,也要对方支持rsync才能支持同步,而有很希望安装的YUM源并不支持rsync,这样怎么办呢?

  因上述原因我想到了另一种思路去同步“所有”我想同步的YUM源,并且搭建成本地可用的YUM源,下面记录下我的思路。

 

1、首先预备好一台nginx或者apache的server来做本地的YUM源(我喜欢nginx),该操作可以去参考“YUM本地源搭建,且Rsync同步官方”的操作。

 

2、搭建好的本地YUM源server后,再下载想要同步YUM源的repo文件。

  例如:

wget -O /etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-6.repo

3、安装几个工具,默认的centos是没有安装,yum installcreaterepo yum-utils -y

  安装这两个工具主要使用 createrepo 和reposync 这两个命令

4、上面的操作完毕后,执行命令yum repolist

  例如:其中“仓库标识:中的名字是我们下面将要用到的

 

 

5、例如我想要把wmj作为本地YUM源(这是我在做Ambari + hadoop实验的yum源,因为该源可能被很多台hadoop datanode端用到所有,搭建本地yum源会节约大量的出口带宽

提高工作效率。)那么我执行如下命令行

     [root@localhosttmp]# reposync -r wmj

     如下开始自动更新yum源到本地文件夹

当然在这一步,我们可以参考下reposync的帮助,直接把想要同步的yum源直接定位到希望下载的某个目录,

例如使用参数 -p

也可以使用 -d 来删除本地老旧,yum源已经不存的安装包。

 

6、当通过reposync命令同步yum源到想要指定的路径之下后,然后使用createrepo命令创对该路径下的rpm包创建为本地的YUM仓库

 

 6.1、到这里的时候,其实想要的yum本地仓库已经算是制作完毕, 为了简化工作量,我们可以把上面的操作制作成bash脚本

 思路如下:

 reposync自动同步想要的yum 源到指定的路径,因为每次同步后,内容可能有所改变,所以需要重新执行createrepo命令重新创建YUM仓库。

  

7、上面的操作完毕后,就是在client端制作repo文件并指向我们本地的YUM源了,因为我们同步YUM源的时候,可能会忽略掉对方的gpgkey,那么记得“gpgcheck=0”

 

例子:

 

为方便日常工作,需要在本地搭建CentOS\EPEL\Ovirt等的本地yum源,根据一般网上文章步骤搭建的本地源基本上都缺少软件分组信息,无法使用yum group*的相关命令,在认真研究相关命令的文档的前提下,经过实践,整理形成完美的,基于reposync/createrepo命令的YUM本地源搭建过程,具体步骤如下:

一、上游源配置目录结构及信息如下:

1、目录结构如下:

/app/yumconfig/centos6

├── cache

├── log

├── yum.conf

└── yum.repos.d

    └── Centos-6.repo

2、yum.conf的内容:

[main]

cachedir=/app/yumconfig/centos6/cache/yum/x86_64/6

keepcache=0

debuglevel=2

logfile=/app/yumconfig/centos6/log/yum.log

exactarch=1

obsoletes=0

gpgcheck=1

plugins=0

installonly_limit=3

reposdir=/app/yumconfig/centos6/yum.repos.d

3、Centos-6.repo的内容:(上游源为阿里云的软件源)

 [base]
name=CentOS-6 - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/6/os/x86_64/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6
 
#released updates 
[updates]
name=CentOS-6 - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/6/updates/x86_64/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6
 
#additional packages that may be useful
[extras]
name=CentOS-6 - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/6/extras/x86_64/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-6 - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/6/centosplus/x86_64/
gpgcheck=1
enabled=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6
 

二、同步脚本sync_centos6.sh的内容如下:

#!/bin/bash

#set -x

date=`date +%Y%m%d` 

log_file="/app/log/sync_centos6.$date.log" #同步日志文件,请根据实际情况进行修改

echo "---- $Date `date` Begin ----" >>$log_file

cd /app/centos6 #本地软件源目目录,请根据实际情况进行修改

reposync -m -c /app/yumconfig/centos6/yum.conf -n   >> $log_file 2>&1

#从上游源同频软件包及软件分组信息:

#-m选项指明需下载软件分组信息文件——comps.xml,

#-c选项指明上游源的配置文件

#-n选项指明只下载最新的软件包

for file in `ls `

do

  if [ -d $file ]

  then

    if [ -f ./$file/comps.xml ]

    then

      createrepo -g comps.xml --update ./$file >> $log_file 2>&1 #如果存在comps.xml文件,则创建含软件分组信息的本地包索引信息

    else

      createrepo --update ./$file >> $log_file 2>&1 

    fi

  fi

done

#createrepo ./ >> $log_file 2>&1

echo "---- $Date `date` End ----" >>$log_file

 

三、配置apache,通过http服务发布"/app/centos6"目录的本地源

   发布时需注意目录文件的权限问题,建议执行:chown -R apache:apache /app/centos6命令,确保权限设置正确。

 

 

 

 

 

  

 
 

 

 

分类: linux 服务

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值