1. 程式人生 > >yum 倉庫, update指令碼

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命令。

[[email protected] 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

檢視日誌,顯示自動更新。

[[email protected] 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