yum 本地仓库创建与发布脚本

阅读更多

为了在隔绝外网的环境下安装包,需要先用yum 先下载包,然后再创建出一个本地yum仓库,最后加到yum的配置中.

下载包

 yum install --downloadonly --downloaddir=path/of/local wget git 
#需要注意一点,如果包已经安装了,这步是下载不了的。

创建本地仓库

创建仓库,需要一个小工具:createrepo

rpm -ivh deltarpm-3.6-3.el7.x86_64.rpm
rpm -ivh python-deltarpm-3.6-3.el7.x86_64.rpm
rpm -ivh libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm
rpm -ivh createrepo-0.9.9-28.el7.noarch.rpm
createrepo  /path/of/localrepo

发布仓库

发布仓库,就是加一个配置文件到 /etc/yum.repos.d/下,文件内容如下

 

#注意 gpgcheck 要为0
#enable 要为1
[my-media]
name=CentOS-$releasever - myrepo
baseurl=file:///path/of/localrepo
gpgcheck=0
enabled=1

同时,还要将 /etc/yum.repos.d/CentOS.repo , /etc/yum.repos.d/epel.repo 移除,否则可能安装时找不到本地包。 

做完以上步骤,就可以愉快的使用  yum install 了。 

 

完整工具脚本

为了方便使用,制作了以下脚本,使用时

    

#下载
bash mkrepo.sh download wget git nginx
#创建仓库
bash mkrepo.sh init
#发布仓库
#把上述目录 ,打包,复制到目标机器上,开始发布
bash mkrepo.sh publish
#清除仓库
bash mkrepo.sh unpublis

 

 

 

 mkrepo.sh脚本

#!/usr/bin/env bash
basedir=$(dirname $0)
cd ${basedir}
x=file://$(pwd)
DIR="${x//\//\/}"
case "$1" in
   "download" )
      str=""
      for ((i=2;i<=$#;i++))
      do
        echo ${!i}
        str="$str ${!i}"
      done
      if [ -z "$str" ]; then
         echo "no pakages specified";
         exit 1;
      fi
      yum install --downloadonly --downloaddir=$(pwd) $str
   ;;
   "init" )
      rpm -ivh deltarpm-3.6-3.el7.x86_64.rpm
      rpm -ivh python-deltarpm-3.6-3.el7.x86_64.rpm
      rpm -ivh libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm
      rpm -ivh createrepo-0.9.9-28.el7.noarch.rpm
      createrepo  $(pwd)

      rpm -e createrepo-0.9.9-28.el7.noarch
      rpm -e libxml2-python-2.9.1-6.el7_2.3.x86_64
      rpm -e python-deltarpm-3.6-3.el7.x86_64
      rpm -e deltarpm-3.6-3.el7.x86_64
   ;;
   "publish" )
     sed -r "s/^baseurl.*$/baseurl=$DIR/" zc.repo > /etc/yum.repos.d/zc.repo
      mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
      mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.bak
    ;;
   "unpublish" )
      rm /etc/yum.repos.d/zc.repo
      mv /etc/yum.repos.d/CentOS-Base.repo.bak /etc/yum.repos.d/CentOS-Base.repo
      mv /etc/yum.repos.d/epel.repo.bak /etc/yum.repos.d/epel.repo
    ;;
    * )
      echo "Usage:"
      echo "  bash $basedir/$0 command "
      echo "commands include: "
      echo "  download  pakagenames.  names seperated by blankspace"
      echo "  init , create a local rpm repo"
      echo "  publish, add this repo to rpm repos"
      echo "  unpublist, remove this repo from rpm repos"

    ;;
esac

 

 

你可能感兴趣的:(linux,rpm,本地仓库)