您的位置:首页 > 运维架构 > Linux

Linux-测试环境构建本地YUM全类型仓库(Base,Extras,epel)

2015-04-01 22:12 676 查看
1.同步yum源的脚本#cat /data/yum_repo/rsync_yum_shell/rsync_yumrepo.sh#!/bin/bash#script name:rsync_yumrepo.shRsyncBin="/usr/bin/rsync"RsyncPerm='-avrt --delete --no-iconv --bwlimit=100000'Centos_7_base='/data/yum_repo/Centos-7/Base/'Centos_7_epel='/data/yum_repo/Centos-7/Epel/'Centos_7_extras='/data/yum_repo/Centos-7/extras/'Centos_6_base='/data/yum_repo/Centos-6/Base/'Centos_6_epel='/data/yum_repo/Centos-6/Epel/'Centos_6_extras='/data/yum_repo/Centos-6/extras/'LogFile='/data/yum_repo/rsync_yum_log'Date=`date +%Y-%m-%d`#checkfunction check {if [ $? -eq 0 ];then echo -e "\033[1;32mRsync is success!\033[0m" >>$LogFile/$Date.logelse echo -e "\033[1;31mRsync is fail!\033[0m" >>$LogFile/$Date.logfi}if [ ! -d "$Centos_7_base" ];then mkdir -pv $Centos_7_basefiif [ ! -d "$Centos_7_epel" ];then mkdir -pv $Centos_7_epelfiif [ ! -d "$Centos_7_extras" ];then mkdir -pv $Centos_7_extrasfiif [ ! -d "$Centos_6_base" ];then mkdir -pv $Centos_6_basefiif [ ! -d "$Centos_6_epel" ];then mkdir -pv $Centos_6_epelfiif [ ! -d "$Centos_6_extras" ];then mkdir -pv $Centos_6_extrasfiif [ ! -d "$LogFile" ];then mkdir $LogFilefi#rsync centos 6 baseecho 'Now start to rsync centos 6 base!' >>$LogFile/$Date.log$RsyncBin $RsyncPerm --exclude=i386/ rsync://mirrors.yun-idc.com/centos/6/os/ $Centos_6_base >>$LogFile/$Date.logcheck#rsync centos 6 epelecho 'Now start to rsync centos 6 epel!' >>$LogFile/$Date.log$RsyncBin $RsyncPerm --exclude=SRPMS/ --exclude=ppc64/ --exclude=i386/ rsync://mirrors.yun-idc.com/epel/6/ $Centos_6_epel >>$LogFile/$Date.logcheck#rsync centos 6 extrasecho 'Now start to rsync centos 6 extras!' >>$LogFile/$Date.log$RsyncBin $RsyncPerm --exclude=i386/ rsync://mirrors.yun-idc.com/centos/6/extras/ $Centos_6_extras >>$LogFile/$Date.logcheck#rsync centos 7 baseecho 'Now start to rsync centos 7 base!' >>$LogFile/$Date.log$RsyncBin $RsyncPerm rsync://mirrors.yun-idc.com/centos/7/os/ $Centos_7_base >>$LogFile/$Date.logcheck#rsync centos 7 epelecho 'Now start to rsync centos 7 epel!' >>$LogFile/$Date.log$RsyncBin $RsyncPerm --exclude=SRPMS/ --exclude=ppc64/ rsync://mirrors.yun-idc.com/epel/7/ $Centos_7_epel >>$LogFile/$Date.logcheck#rsync centos 7 extrasecho 'Now start to rsync centos 7 extras!' >>$LogFile/$Date.log$RsyncBin $RsyncPerm rsync://mirrors.yun-idc.com/centos/7/extras/ $Centos_7_extras >>$LogFile/$Date.logcheck

脚本参考大神的博客:http://dl528888.blog.51cto.com/2382721/1342653
任务计划:周六的凌晨0点20分开始执行更新20 00 * * 6 /bin/bash /data/yum_repo/rsync_yum_shell/rsync_yumrepo.sh趁周末下载的,大概46GB。

2.配置nginx服务器参考nginx编译安装http://sohudrgon.blog.51cto.com/3088108/1596485配置虚拟主机参考:http://sohudrgon.blog.51cto.com/3088108/1596655我的nginx配置文件:server { listen 8000; server_name yum.stu15.cn; index index.html index.htm; root /data/yum_repo/; location / { autoindex on; autoindex_exact_size off; autoindex_localtime on; } error_page 502 = /502.html; log_format access '$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 access;} 3.配置repo文件CentOS-Base-6.repo[Base]name=CentOS-Base6 $basearchbaseurl=http://192.168.1.15:8000/Centos-6/Base/$basearchenable=1gpgcheck=1gpgkey=http://192.168.1.15:8000/Centos-6/Base/$basearch/RPM-GPG-KEY-CentOS-6 [extras]name=CentOS-Base6 $basearchbaseurl=http://192.168.1.15:8000/Centos-6/extras/$basearchenable=1gpgcheck=0[Epel]name=CentOS-Base6 $basearchbaseurl=http://192.168.1.15:8000/Centos-6/Epel/$basearchenable=1gpgcheck=0 CentOS-Base-7.repo[Base]name=CentOS-Base7 $basearchbaseurl=http://192.168.1.15:8000/Centos-7/Base/$basearchenable=1gpgcheck=1gpgkey=http://192.168.1.15:8000/Centos-7/Base/$basearch/RPM-GPG-KEY-CentOS-7[extras]name=CentOS-Base6 $basearchbaseurl=http://192.168.1.15:8000/Centos-7/extras/$basearchenable=1gpgcheck=0[Epel]name=CentOS-Base6 $basearchbaseurl=http://192.168.1.15:8000/Centos-7/Epel/$basearchenable=1gpgcheck=0
根据需要选择repo文件。
然后进行下面操作:按照不同的系统选择不同的repo文件,放到服务器的/etc/yum.repos.d里;删除之前/etc/yum.repos.d里的repo;清理缓存yum clean all;将服务器上的软件包信息先在本地缓存,以提高搜索安装软件的速度 yum makecache.现在本地yum源就已经完成,可以控制软件源,并且由于走内网所有不浪费机房公网网络流量。 批量重命名系统已经存在的repo文件:
# ls
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo  CentOS-Vault.repo  epel-aliyun.repo
#  for i in `ls`; do mv -f $i `echo $i".bak"`; done
# ls
CentOS-Base.repo.bak  CentOS-Debuginfo.repo.bak  CentOS-Media.repo.bak  CentOS-Vault.repo.bak  epel-aliyun.repo.bak
或者删除目录下面的repo文件:
#ansible all -m shell -a 'find /etc/yum.repos.d/ -name"*.bak" -exec rm -f {} \; '


4.脚本批量更新测试环境的所有服务器的yum源为本地15的:
#cat createrepo.sh
#!/bin/bash
cd /etc/yum.repos.d/
for i in `ls`; do mv -f $i `echo $i".bak"`; done
cat
<<EOF>>CentOS-Base-6.repo
[Base]
name=CentOS-Base6 \$basearch
baseurl=http://192.168.1.15:8000/Centos-6/Base/\$basearch
enable=1
gpgcheck=1
gpgkey=http://192.168.1.15:8000/Centos-6/Base/\$basearch/RPM-GPG-KEY-CentOS-6
[extras]
name=CentOS-Base6 \$basearch
baseurl=http://192.168.1.15:8000/Centos-6/extras/\$basearch
enable=1
gpgcheck=0
[Epel]
name=CentOS-Base6 \$basearch
baseurl=http://192.168.1.15:8000/Centos-6/Epel/\$basearch
enable=1
gpgcheck=0
EOF
yum clean all
yum makecache
find /etc/yum.repos.d/ -name "*.bak" -exec rm -f {} \;


5.在15上使用ansible批量推送脚本到服务器,然后执行脚本:# ansible all -m copy -a 'src="./createrepo.sh" dest="/root/createrepo.sh"'# ansible all -m shell -a 'bash /root/createrepo.sh' 测试使用本地yum源安装zsh,然后卸载:# ansible all -m yum -a "name=zsh state=present"# ansible all -m yum -a "name=zsh state=removed"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux repo ansible