构建本地yum源

近期准备重新搭建最新的zabbix4.2,ansible等等,显而易见这些软件都需要通过yum安装会方便的多,而且便于管理以及以后的升级。公司千台以上的机器,基本都放在内网环境当中,于是就有必要搭建本地源环境,之前搭建的不是太完善,结合这个机会,重新整理一遍,尽可能涵盖到日常所用的所有软件。

首先当然需要准备一台可以访问互联网的机器

1
2
yum -y install createrepo
yum -y install yum-utils

Nginx源

1
2
3
4
5
6
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

reposync -r nginx repo

cd /repodata/data/nginx/7
createrepo .

PIP源

创建pip源目录

1
2
mkdir -p /repodata/data/pypi
cd /repodata/data/pypi

安装pip2pi软件

1
pip install pip2pi

需要安装哪些包,可以一起写在requirements.txt文件里

1
pip2pi /repodata/data/pypi -r requirements.txt

也可以单独安装某个包pymysql

1
pip2pi /repodata/data/pypi pymysql

在对应目录下会生成一个simple目录,和相关下载的文件

1
2
3
4
[root@localhost pypi]# ll
total 52
-rw-r--r-- 1 root root 47738 May 19 16:00 PyMySQL-0.9.3-py2.py3-none-any.whl
drwxr-xr-x 3 root root 4096 May 19 16:00 simple

建立索引

1
dir2pi /repodata/data/pypi

这样源就配置好了,接下只需要在其他机器上配置这个源地址就可以了

编辑~/.pip/pip.conf

1
2
3
4
[global]
index-url = https://127.0.0.1/pypi/simple/
[install]
trusted-host= 127.0.0.1

内网机安装pymysql

1
2
3
4
5
6
[root@localhost .pip]# pip install pymysql
Collecting pymysql
Downloading http://127.0.0.1/pypi/simple/pymysql/PyMySQL-0.9.3-py2.py3-none-any.whl (47kB)
100% |████████████████████████████████| 51kB 43.7MB/s
Installing collected packages: pymysql
Successfully installed pymysql-0.9.3

互联网源

这里主要是通过互联网同步zabbix源、epel源和centos源,(centos主要是因为自带的redhat有些包不存在,只有通过centos源去获取)。

一个shell脚本,通过rsync连接到互联网上的rsync服务器去定时同步

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
RsyncPerm='-avSH --delete-after --no-iconv --bwlimit=10000'
Epel='/repodata/data/epel'
Redhat_zabbix='/repodata/data/zabbix/4.4'
zabbix_nosupport='/repodata/data/zabbix/non-supported'
Centos='/repodata/data/centos'
Mysql='/repodata/data/mysql'
Percona='/repodata/data/percona'
Elasticstack='/repodata/data/elasticstack'
pypi='/repodata/data/pypi'
LogFile='/repodata/log'
Date=`date +%Y-%m-%d`


function CheckStatus(){
if [ $? -eq 0 ];then
echo -e "Rsync is success!">>$LogFile/$Date.log
else
echo -e "Rsync is fail!">>$LogFile/$Date.log
fi
}

epel()
{
###rsync epel
echo "=====================================================================================================" >>$LogFile/$Date.log
echo `date` >>$LogFile/$Date.log
echo 'Now start to rsync redhat epel!' >>$LogFile/$Date.log
echo "=====================================================================================================" >>$LogFile/$Date.log
rsync $RsyncPerm rsync://ftp.iij.ad.jp/pub/linux/Fedora/archive/epel/5/x86_64/ $Epel/5 >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.tuna.tsinghua.edu.cn/epel/6/x86_64/ $Epel/6 >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/ $Epel/7 >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.tuna.tsinghua.edu.cn/epel/8/Everything/x86_64/ $Epel/8 >>$LogFile/$Date.log
CheckStatus
}

centos()
{
###rsync centos
echo "=====================================================================================================" >>$LogFile/$Date.log
echo `date` >>$LogFile/$Date.log
echo 'Now start to rsync centos !' >>$LogFile/$Date.log
echo "=====================================================================================================" >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.163.com/centos/6/os/x86_64/ $Centos/6 >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.163.com/centos/7/os/x86_64/ $Centos/7 >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.163.com/centos/8/BaseOS/x86_64/os/ $Centos/8 >>$LogFile/$Date.log
CheckStatus
}

mysql()
{
###rsync mysql
echo "=====================================================================================================" >>$LogFile/$Date.log
echo `date` >>$LogFile/$Date.log
echo 'Now start to rsync Mysql!' >>$LogFile/$Date.log
echo "=====================================================================================================" >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.tuna.tsinghua.edu.cn/mysql/yum/ $Mysql >>$LogFile/$Date.log
CheckStatus
}

percona()
{
###rsync percona
echo "=====================================================================================================" >>$LogFile/$Date.log
echo `date` >>$LogFile/$Date.log
echo 'Now start to rsync Percona!' >>$LogFile/$Date.log
echo "=====================================================================================================" >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.tuna.tsinghua.edu.cn/percona/yum/release/5/RPMS/x86_64/ $Percona/5 >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.tuna.tsinghua.edu.cn/percona/yum/release/6/RPMS/x86_64/ $Percona/6 >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.tuna.tsinghua.edu.cn/percona/yum/release/7/RPMS/x86_64/ $Percona/7 >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.tuna.tsinghua.edu.cn/percona/yum/release/8/RPMS/x86_64/ $Percona/8 >>$LogFile/$Date.log
CheckStatus
}

elasticstack()
{
###rsync elasticstack
echo "=====================================================================================================" >>$LogFile/$Date.log
echo `date` >>$LogFile/$Date.log
echo 'Now start to rsync Elasticstack!' >>$LogFile/$Date.log
echo "=====================================================================================================" >>$LogFile/$Date.log
rsync $RsyncPerm rsync://mirrors.tuna.tsinghua.edu.cn/elasticstack/yum/ $Elasticstack >>$LogFile/$Date.log
CheckStatus
}

zabbix()
{
###rsync zabbix 4.4
echo "=====================================================================================================" >>$LogFile/$Date.log
echo `date` >>$LogFile/$Date.log
echo 'Now start to rsync redhat zabbix version 4.4!' >>$LogFile/$Date.log
echo "=====================================================================================================" >>$LogFile/$Date.log
rsync $RsyncPerm rsync://repo.zabbix.com/mirror/zabbix/4.4/rhel/5/x86_64/ $Redhat_zabbix/5 >>$LogFile/$Date.log
rsync $RsyncPerm rsync://repo.zabbix.com/mirror/zabbix/4.4/rhel/6/x86_64/ $Redhat_zabbix/6 >>$LogFile/$Date.log
rsync $RsyncPerm rsync://repo.zabbix.com/mirror/zabbix/4.4/rhel/7/x86_64/ $Redhat_zabbix/7 >>$LogFile/$Date.log
rsync $RsyncPerm rsync://repo.zabbix.com/mirror/non-supported/rhel/ $zabbix_nosupport >>$LogFile/$Date.log
CheckStatus
}

action=$1
[ -z $1 ] && action=all
case "$action" in
epel)
epel
;;
centos)
centos
;;
mysql)
mysql
;;
percona)
percona
;;
elasticstack)
elasticstack
;;
zabbix)
zabbix
;;
all)
epel
centos
mysql
percona
elasticstack
zabbix
;;
*)
echo -e "Usage: ./`basename $0` [epel|centos|mysql|percona|elasticstack|zabbix|all]"
;;
esac

构建本地yum源
https://www.xbdba.com/2019/05/17/build-local-yum-repository/
作者
xbdba
发布于
2019年5月17日
许可协议