layout: post
title: 创建yum本地仓库,将阿里仓库同步到本地,并定时更新
date: 2017-12-05
tags: [“Linux”,”包管理”,”软件安装”]


很多时候为了加速自己内部的rpm包安装速度,都会搭建自己的yum源仓库,而使用系统光盘自带的源,由于软件版本比较落后,所以不太适用,而大家都在用的阿里仓库比较好用,所以就想到了把阿里仓库的rpm全部拉到本地,并做yum仓库的定时更新。这样既能保证软件包是最新的,也能保证软件的安装速度。那么下面来具体实施,搭建自己的yum本地仓库,并定时从阿里仓库同步过来。

第一步:下载阿里镜像的repo安装包,centos6就下载6的,7的就下载7的地址:https://mirrors.aliyun.com/repo/

我们以centos7的镜像为例

  1. cd /etc/yum.repos.d/
  2.  
  3. mkdir bak
  4.  
  5. mv Centos* bak #将系统自带的源备份到bak目录下
  6.  
  7. wget https://mirrors.aliyun.com/repo/Centos-7.repo
  8.  
  9. cat Centos-7.repo
  10.  
  11. # CentOS-Base.repo
  12. #
  13. # The mirror system uses the connecting IP address of the client and the
  14. # update status of each mirror to pick mirrors that are updated to and
  15. # geographically close to the client. You should use this for CentOS updates
  16. # unless you are manually picking other mirrors.
  17. #
  18. # If the mirrorlist= does not work for you, as a fall back you can try the
  19. # remarked out baseurl= line instead.
  20. #
  21. #
  22.  
  23. [base]
  24. name=CentOS-$releasever - Base - mirrors.aliyun.com
  25. failovermethod=priority
  26. baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
  27. http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
  28. #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
  29. gpgcheck=1
  30. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  31.  
  32. #released updates
  33. [updates]
  34. name=CentOS-$releasever - Updates - mirrors.aliyun.com
  35. failovermethod=priority
  36. baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
  37. http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
  38. #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
  39. gpgcheck=1
  40. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  41.  
  42. #additional packages that may be useful
  43. [extras]
  44. name=CentOS-$releasever - Extras - mirrors.aliyun.com
  45. failovermethod=priority
  46. baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
  47. http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
  48. #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
  49. gpgcheck=1
  50. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  51.  
  52. #additional packages that extend functionality of existing packages
  53. [centosplus]
  54. name=CentOS-$releasever - Plus - mirrors.aliyun.com
  55. failovermethod=priority
  56. baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
  57. http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
  58. #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
  59. gpgcheck=1
  60. enabled=0
  61. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  62.  
  63. #contrib - packages by Centos Users
  64. [contrib]
  65. name=CentOS-$releasever - Contrib - mirrors.aliyun.com
  66. failovermethod=priority
  67. baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
  68. http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
  69. #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
  70. gpgcheck=1
  71. enabled=0
  72. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  已经配置好的阿里仓库
  1. yum repolist #查看阿里的可用仓库,有启用的表示已经成功了
  2.  
  3. 从阿里服务器将rpm同步到本地需要两个rpm软件createrepo yum-utils
  4.  
  5. yum install createrepo yum-utils -y #yum安装这两个包
  6. mkdir -p /mirrors/Packege #创建rpm包的存放目录
  7. reposync -r base -p /mirrors/Packege #将已经配置好的阿里仓库镜像内的rpm包拉到本地,b ase为本地已经配置好的仓库名,可以用yum repolist查看到
  8. createrepo -pdo /mirrors/ /mirrors/Packege #创建repo数据库
 
###############################同步rpm包,需要相当长一段时间################################
同步完成之后,本地的yum仓库已经基本完成了,只剩下定时更新了
  1. vim /cron/repository.sh #编写同步脚本
  2. reposync -r base -p /mirrors/Packege -d #来删除本地老旧
  3. reposync -r base -p /mirrors/Packege
  4.  
  5. crontab -e #添加定时任务
  6. 0 0 1 * * sh /cron/repository.sh #每月1日0时更新yum仓库
文档更新时间: 2020-04-07 16:59   作者:张尚