yum 仓库, update脚本

createrepo 命令用于创建yum源(软件仓库),即为存放于本地特定位置的众多rpm包建立索引,描述各包所需依赖信息,并形成元数据,然后,yum通过ftp/http或URI访问着这些目录。

yum -y install createrepo

语法:

createrepo [option] <directory>

参数说明:

-u  --baseurl <url>
    指定Base URL的地址

-o --outputdir <url>
    指定元数据的输出位置

-x --excludes <packages>
    指定在形成元数据时需要排除的包

-i --pkglist <filename>
    指定一个文件,该文件内的包信息将被包含在即将生成的元数据中,格式为每个包信息独占一行,不含通配符、正则,以及范围表达式。

-n --includepkg
    通过命令行指定要纳入本地库中的包信息,需要提供URL或本地路径。

-q --quiet
    安静模式执行操作,不输出任何信息。

-g --groupfile <groupfile>
    指定本地软件仓库的组划分,范例如下:
createrepo -g comps.xml /path/to/rpms
    注意:组文件需要和rpm包放置于同一路径下。

-v --verbose
    输出详细信息。

-c --cachedir <path>
    指定一个目录,用作存放软件仓库中软件包的校验和信息。
    当createrepo在未发生明显改变的相同仓库文件上持续多次运行时,指定cachedir会明显提高其性能。

--update
    如果元数据已经存在,且软件仓库中只有部分软件发生了改变或增减,
    则可用update参数直接对原有元数据进行升级,效率比重新分析rpm包依赖并生成新的元数据要高很多。

-p --pretty
    以整洁的格式输出xml文件。

-d --database
    该选项指定使用SQLite来存储生成的元数据,默认项。

安装使用事例:


#建立yun仓库包存储目录
mkdir /home/ads/yumrepo/data/rpm -p    

#建立ftp上传目录,如有包更新,需上传到该目录下
mkdir /home/ads/ftp_root/custom/x86_64/

#建立软连接,用于update
ln -s /home/ads/yumrepo/data/rpm/ /home/ads/yumrepo/repo/packages

#建立软连接,用于http显示包列表
ln -s /home/ads/yumrepo/data/rpm/ /home/ads/yumrepo/http_root/packages

#建立仓库,并将文件输出到repo
createrepo -pdo /home/ads/yumrepo/repo  /home/ads/yumrepo/repo/packages

ll
lrwxrwxrwx 1 root root   27 Aug 27 10:30 packages -> /home/ads/yumrepo/data/rpm/
drwxr-xr-x 2 root root 4096 Aug 29 11:41 repodata
ll repodata/
-rw-r--r-- 1 root root  20250 Aug 29 11:41 09cf0ad09da66e4223ca3ee391167a862e09118dcbca08e726c033ce15909c16-other.sqlite.bz2
-rw-r--r-- 1 root root 725037 Aug 29 11:41 6a9ae61d640da09d011ef1d6f86e5f02c910771ca432fa231acebe5494c08227-primary.sqlite.bz2
-rw-r--r-- 1 root root 693211 Aug 29 11:41 774b0e04428eb99b9258d143e2dad1124c94edde1e1c35a75aaa345d05275ed5-filelists.sqlite.bz2
-rw-r--r-- 1 root root  13308 Aug 29 11:41 7842299665124dbd8a5417d5d0d13245beebfe7ea652978aadaaa18cfc48dfd9-other.xml.gz
-rw-r--r-- 1 root root 765766 Aug 29 11:41 b029ecb66376c1268ea039f445bd984752d66c9b2d319314b404a08b2c728834-filelists.xml.gz
-rw-r--r-- 1 root root 296697 Aug 29 11:41 e0bfe6f75d4bc1323ebfc9bf30165486fdfc00b4c7612326c0326709d0e4b118-primary.xml.gz
-rw-r--r-- 1 root root   2995 Aug 29 11:41 repomd.xml

关于这几个文件的描述是:
repomd.xml this is the file that describes the other metadata files. It is like an index file to point to the other files. It contains timestamps and checksums for the other files. This lets a client download this one, small file and know if anything else has changed. This also means that cryptographically (ex: gpg) signing this one file can ensure repository integrity.
primary.xml.[gz] this file stores the primary metadata information. This includes information such as:
name, epoch, version, release, architecture
file size, file location, description, summary, format, checksums header byte-ranges, etc.
dependencies, provides, conflicts, obsoletes, suggests, recommends
file lists for the package for CERTAIN files - specifically files matching: /etc*, *bin/*, /usr/lib/sendmail [1]
filelists.xml.[gz] this file stores the complete file and directory listings for the packages. The package is identified by: name, epoch, version, release, architecture and package checksum id.
other.xml.[gz] this file currently only stores the changelog data from packages. However, this file could be used for any other additional metadata that could be useful for clients.
groups.xml.[gz] this file is tentatively described. The intention is for a common package-groups specification as well. There is still some sections for this format that need to be fleshed out.
其实我们需要记住的就是primary.xml.gz存储的是包列表,而filelist.xml存储的是所有包的目录和文件列表。

也就是说如果createrepo成功创建索引后,包的信息和每个包的文件信息都会存储在这两个文件中。

当然,如果你在执行createrepo时添加了-d 参数的话,createrepo会对

filelist.xml.gz,primary.xml.gz 和other.xml.gz各自生成一个sqlite的文件(或者压缩文件)。

其中用sqlite格式存储了对应xml文件中的信息。

然后当你在客户端执行 yum clean all ,yum list 之后,这些索引文件就会下载到客户端。

建立时间戳目录,用于yum update后文件更新
date +%s
1535339065
mkdir /home/ads/yumrepo/http_root/repodata/1535339065 -p
cp /home/ads/yumrepo/repo/repodata/* /home/ads/yumrepo/http_root/repodata/1535339065/
vim /home/ads/yumrepo/http_root/repodata/1535339065/repomd.xml
将所有location路径添加时间戳 
<location href="repodata/1535339065/3e827db59588c4acf3203c1c9128197d1dad4229475eca0902dfcffd3045c8ad-filelists.xml.gz"/>
复制文件到上一级,使yum能够发现
cp /home/ads/yumrepo/http_root/repodata/1535339065/repomd.xml /home/ads/yumrepo/http_root/repodata/

apache配置

yum -y install http php mysql

ln -s /home/ads/yumrepo/http_root /var/www/html/yum

客户端yum配置添加此yum仓库

vim /etc/yum.repos.d/test.repo
[test]
name=test
baseurl=http://10.0.13.165/yum/
gpgcheck=0

更新yum缓存
yum makecache

测试新yum
yum list |grep  test
显示已经添加到仓库的包,说明成功。
cloudera-manager-agent.x86_64            5.13.0-1.cm5130.p0.55.el7      test    
cloudera-manager-daemons.x86_64          5.13.0-1.cm5130.p0.55.el7      test    
cloudera-manager-server.x86_64           5.13.0-1.cm5130.p0.55.el7      test    
cloudera-manager-server-db-2.x86_64      5.13.0-1.cm5130.p0.55.el7      test    

每次更新手动创建还是比较麻烦,自己写了个shell脚本,计划任务执行,发现有更新包,使用update命令。

[root@yum-update conf]# cat /root/zxl_repo_updater.sh 
#!/bin/bash
LOG_PATH=/home/ads/yumrepo/update.log
num_r=`ps -ef|grep zxl_repo_updater.sh |grep -v grep |wc -l`

if [ $num_r -eq 2 ]; then
FTP_PATH=/home/ads/ftp_root/custom/x86_64
BAK_PATH=/home/ads/ftpbak/
REPO_FTP_PATH=/home/ads/yumrepo/data/ftp/
RPM_PATH="/home/ads/yumrepo/data/rpm"
REPO_PATH="/home/ads/yumrepo/repo"
HTTP_ROOT_PATH=/home/ads/yumrepo/http_root

log() {
  msg="$1"
  echo "[$(date '+%Y%m%d %H:%M:%S')] ${msg}" >> $LOG_PATH
}

update_repo() {
  package_list="$(ls $REPO_FTP_PATH)"
  # Move file to package folder
  for f in $package_list; do
    if [ ! -f "${REPO_FTP_PATH}/${f}" ]; then
      continue
    fi
    if [ -f "${RPM_PATH}/$f" ]; then
      log "backup rpm: ${f}"
      mv "${RPM_PATH}/${f}" "${BAK_PATH}/${f}.bak.$(date +%s)"
    fi
    log "adding rpm: ${f}"
    mv "${REPO_FTP_PATH}/${f}" "${RPM_PATH}/${f}"
    chown nobody:nobody "${RPM_PATH}/${f}"
    chmod 644 "${RPM_PATH}/${f}"
  done
}

NUM=`ls -n $FTP_PATH |wc -l`
if [ $NUM -gt 1 ]; then
	rsync -avz $FTP_PATH/* $REPO_FTP_PATH
	rm -rf $FTP_PATH/*

	update_repo 

	createrepo $REPO_PATH --update
	
	timestamp=$(date +%s)

	cp -r "$REPO_PATH/repodata" "${HTTP_ROOT_PATH}/repodata/${timestamp}"
	log "repodata allocated: ${HTTP_ROOT_PATH}/repodata/${timestamp}"

	# Render repomd.xml
	sed -i "s#repodata#repodata/${timestamp}#" "${HTTP_ROOT_PATH}/repodata/${timestamp}/repomd.xml"
	# Overwrite main repomd.xml
	cp "${HTTP_ROOT_PATH}/repodata/${timestamp}/repomd.xml" "${HTTP_ROOT_PATH}/repodata/repomd.xml"
fi
fi

查看日志,显示自动更新。

[root@yum-update conf]# tail -f /home/ads/yumrepo/update.log
[20180828 20:55:01] adding rpm: y-engine-switchindex-scripts-2.4.0-5629.x86_64.rpm
[20180828 20:55:01] adding rpm: y-engine-switchindex-scripts-debuginfo-2.4.0-5629.x86_64.rpm
[20180828 20:55:06] repodata allocated: /home/ads/yumrepo/http_root/repodata/1535460906
[20180829 10:28:02] adding rpm: y-engine-venus-sand-2.4.0-5630.x86_64.rpm
[20180829 10:28:02] adding rpm: y-engine-venus-sand-debuginfo-2.4.0-5630.x86_64.rpm
[20180829 10:28:07] repodata allocated: /home/ads/yumrepo/http_root/repodata/1535509687
[20180829 11:40:01] adding rpm: y-engine-hera-sand-2.4.0-5630.x86_64.rpm

猜你喜欢

转载自blog.csdn.net/zhangxueleishamo/article/details/82182676