1、脚本实现的功能
可以实现本地Yum源自动同步远程RPM包文件并更新Repo,并且该脚本会自动移除同步失败的rpm包,保证yum的更新成功
2、脚本使用说明
直接执行即可,添加至crontab可实现定期更新,更新频率保持大约每周一次。
3、脚本使用注意事项
yum仓库目录的存储路径必须和脚本上的配置对应,此脚本完全是针对之前yum源搭建编写的,所以具有非常严格的限制,比如repoid中要通过“7”、“6_64”,“6_32”的字样来区分不同的系统,请自行参考修改
4、脚本内容如下
#!/bin/bash ############################################################### ###### Update rpm from remote to local yumrepo ###### Author:huangqw ###### Create_Date:2018/09/18 ###### LastModified:2018/09/18 ###### Warning:notice Yum Dir Path ############################################################### Date=`date +"%F_%T"` log_file="/var/log/yum_update.log" log_file2="/var/log/yum_createrepo.log" Yum7_Dir="/data/yum.repo/rhel/7/x86_64" Yum6_64_Dir="/data/yum.repo/rhel/6/x86_64" Yum6_32_Dir="/data/yum.repo/rhel/6/i386" yumrepo_Dir="/tmp/createrepo" if [ -d ${yumrepo_Dir} ];then mkdir -p ${yumrepo_Dir} fi echo "---- $Date Yum Update Begin ----" >>$log_file #Update rpms from remote to local yumrepo for repoid in `yum repolist |sed -n '/repo id/,$p' |sed '1d;$d'|cut -d" " -f1` do if echo ${repoid%%/*} |grep "7" >/dev/null 2>&1;then echo "---- $Date ${repoid%%/*} Update RPMs Begin ----" >>$log_file reposync -d -m -r ${repoid%%/*} -p ${Yum7_Dir} >> $log_file 2>&1 echo "---- $Date ${repoid%%/*} Update RPMs End ----" >>$log_file echo "---- $Date ${repoid%%/*} Update Repo Begin ----" >>$log_file createrepo --update -g ${Yum7_Dir}/${repoid%%/*}/comps.xml ${Yum7_Dir}/${repoid%%/*} >> $log_file 2>$log_file2 if [ $? -ne 0 ];then gawk '{print $10}' $log_file2 |gawk -F: '{print $1}' |xargs -I {} mv -f {} ${yumrepo_Dir} createrepo --update -g ${Yum7_Dir}/${repoid%%/*}/comps.xml ${Yum7_Dir}/${repoid%%/*} >> $log_file 2>&1 echo "---- $Date ${repoid%%/*} Update Repo End ----" >>$log_file else echo "---- $Date ${repoid%%/*} Update Repo End ----" >>$log_file fi elif echo ${repoid%%/*} |grep "6_64" >/dev/null 2>&1;then echo "---- $Date ${repoid%%/*} Update RPMs Begin ----" >>$log_file reposync -d -m -r ${repoid%%/*} -p ${Yum6_64_Dir} >> $log_file 2>&1 echo "---- $Date ${repoid%%/*} Update RPMs End ----" >>$log_file echo "---- $Date ${repoid%%/*} Update Repo Begin ----" >>$log_file createrepo --update -g ${Yum6_64_Dir}/${repoid%%/*}/comps.xml ${Yum6_64_Dir}/${repoid%%/*} >> $log_file 2>$log_file2 if [ $? -ne 0 ];then gawk '{print $10}' $log_file2 |gawk -F: '{print $1}' |xargs -I {} mv -f {} ${yumrepo_Dir} createrepo --update -g ${Yum6_64_Dir}/${repoid%%/*}/comps.xml ${Yum6_64_Dir}/${repoid%%/*} >> $log_file 2>&1 echo "---- $Date ${repoid%%/*} Update Repo End ----" >>$log_file else echo "---- $Date ${repoid%%/*} Update Repo End ----" >>$log_file fi else echo "---- $Date ${repoid%%/*} Update RPMs Begin ----" >>$log_file reposync -d -m -r ${repoid%%/*} -p ${Yum6_32_Dir} >> $log_file 2>&1 echo "---- $Date ${repoid%%/*} Update RPMs End ----" >>$log_file echo "---- $Date ${repoid%%/*} Update Repo Begin ----" >>$log_file createrepo --update -g ${Yum6_32_Dir}/${repoid%%/*}/comps.xml ${Yum6_32_Dir}/${repoid%%/*} >> $log_file 2>$log_file2 if [ $? -ne 0 ];then gawk '{print $10}' $log_file2 |gawk -F: '{print $1}' |xargs -I {} mv -f {} ${yumrepo_Dir} createrepo --update -g ${Yum6_32_Dir}/${repoid%%/*}/comps.xml ${Yum6_32_Dir}/${repoid%%/*} >> $log_file 2>&1 echo "---- $Date ${repoid%%/*} Update Repo End ----" >>$log_file else echo "---- $Date ${repoid%%/*} Update Repo End ----" >>$log_file fi fi done echo "---- $Date Yum Update End ----" >>$log_file