描述
通过下载互联网公共镜像站最新CentOS7
系列rpm包完成本地yum镜像库部署
脚本
#!/bin/bash
#
###########################################
# Author : Diao Qiang
# Email : diaoqiangwork@163.com
# FileName : deploy_localMirror.sh
# Date : 2020-11-04 10:50:00
# Function : 通过下载互联网公共镜像站最新CentOS7系列rpm包完成本地yum镜像库部署
###########################################
#
#
# Usage:
# sh $0 deploy 部署本地yum镜像库
# sh $0 update 更新本地yum镜像库
# sh $0 fixgroups 修复客户机"yum groups [list|install|remove|xxx]"失效
# sh $0 fixrepo 修复CentOS7_latest.repo文件
#
#
# 更新日志
# 2020-11-04 初始脚本
# 2021-02-05 1、架构调整优化
# 2、新增组信息、repo文件修复(fixgroups fixrepo)
# 2021-02-07 1、新增检查互联网连通性(check_Inernet)
# 2、新增部署web服务deploy_web(包含检查(check_web)、安装(install_web)、启动(start_web)web服务模块)
# 3、优化创建组元数据逻辑
# 4、修复若干bug
# 2021-02-08 优化开始结束时间戳格式
# 结果判断
function printf_Fail(){
printf "%-50s %s\n" "$1" "[ Fail ]"
}
function printf_OK(){
printf "%-50s %s\n" "$1" "[ OK ]"
}
function if_Fail_exit(){
if [[ $? -ne 0 ]]; then
printf_Fail "$1"
time_tag "End:"
exit 1
else
printf_OK "$1"
fi
}
function if_Fail(){
if [[ $? -ne 0 ]]; then
printf_Fail "$1"
else
printf_OK "$1"
fi
}
# 检查互联网连通性
function check_Inernet(){
if ! ping -c2 -W2 -i0.2 "${mirrorSite}" &> /dev/null; then
if_Fail_exit "Access internet"
fi
}
# 安装创建local mirror必要软件
function install_rpm(){
rpmList=(yum-utils createrepo wget mlocate)
for rpmName in ${rpmList[*]}; do
rpm -qa | grep "${rpmName}" &>> "${logFile}"
if [[ $? -ne 0 ]]; then
yum -y install "${rpmName}" &>> "${logFile}"
fi
if_Fail_exit "install ${rpmName}"
done
# 更新mlocate.db数据库到最新
updatedb
}
# 检查是否安装nginx、apache(如已安装某web服务,请手动将本地镜像目录映射到web目录)
function check_web(){
webList=("${webNginx}" "${webApache}")
for webName in ${webList[*]}; do
rpm -qa | grep "${webName}" | grep -v tools &>> "${logFile}"
# 已通过rpm包安装
if [[ $? -eq 0 ]]; then
let webTag+=1
printf_OK "${webName} installed via rpm"
continue
fi
confStr=$(locate conf/${webName}.conf | grep -w "${webName}.conf$")
# 已通过源码包安装
if [[ "${confStr}" =~ "${webName}" ]]; then
let webTag+=1
printf_OK "${webName} installed via source"
fi
done
}
# 安装web服务(apache)
function install_web(){
yum -y install "${webApache}" &>> "${logFile}"
if_Fail_exit "install ${webApache}"
mv -f /etc/httpd/conf.d/welcome.conf{,.bak} &>> "${logFile}"
if_Fail "mv welcome.conf"
ln -sf "${localDir}" /var/www/html &>> "${logFile}"
if_Fail "link ${localDir} /var/www/html"
}
# 启动web服务(apache)
function start_web(){
systemctl enable "${webApache}" &>> "${logFile}"
systemctl start "${webApache}" &>> "${logFile}"
if_Fail_exit "start ${webApache}"
}
# 部署web服务(检查、安装、配置、启动web服务)
function deploy_web(){
webNginx="nginx"
webApache="httpd"
webTag=0
# 检查是否已安装web服务(nginx、apache)
check_web
# 未安装web服务,则安装web服务(apache)
if [[ "${webTag}" -eq 0 ]]; then
install_web
start_web
# 已安装web服务,请自行手动将yum本地仓库链接到web服务的HTML目录
else
echo "Please manually link YUM local repository directory(${localDir}) to HTML directory."
fi
}
# 备份所有repo文件
function bak_repo(){
if [[ ! -d "${repoDir}"/bak ]]; then
mkdir "${repoDir}"/bak &>> "${logFile}"
fi
for repoFile in $(ls "${repoDir}"/*.repo); do
mv -f "${repoFile}" "${repoDir}"/bak/ &>> "${logFile}"
if_Fail_exit "backup all repo"
done
}
# 创建CentOS7_latest.repo文件文件
function create_repo(){
cat > "${repoDir}"/"${latestRepo}" << EOF
[base]
name=CentOS-7 - Base
baseurl=${httpSite}/centos/7/os/x86_64/
gpgcheck=1
gpgkey=${httpSite}/centos/RPM-GPG-KEY-CentOS-7
[updates]
name=CentOS-7 - Updates
baseurl=${httpSite}/centos/7/updates/x86_64/
gpgcheck=1
gpgkey=${httpSite}/centos/RPM-GPG-KEY-CentOS-7
[extras]
name=CentOS-7 - Extras
baseurl=${httpSite}/centos/7/extras/x86_64/
gpgcheck=1
gpgkey=${httpSite}/centos/RPM-GPG-KEY-CentOS-7
[epel]
name=Extra Packages for Enterprise Linux 7
baseurl=${httpsSite}/epel/7/x86_64/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
EOF
if_Fail_exit "create ${latestRepo}"
}
# 清除并创建yum缓存
function cache_yum(){
yum clean all &>> "${logFile}" && yum makecache &>> "${logFile}"
if_Fail_exit "yum clean all && yum makecache"
}
# 是否存在local mirror目录。若不存在,则创建目录
function mkdir_localDir(){
if [[ ! -d "${localDir}" ]]; then
mkdir -p "${localDir}" &>> "${logFile}"
if_Fail_exit "create ${localDir}"
fi
}
# 从互联网公共镜像站同步CentOS 7版本rpm包
function sync_rpms(){
repoID=(
base
updates
extras
epel
)
for ID in ${repoID[*]}; do
if [[ ${ID} = "epel" ]]; then
echo "epel rpm package downloading..."
reposync -n --repoid=${ID} -p ${localDir} &>> "${logFile}"
else
echo "${ID} rpm package downloading..."
reposync -n --repoid=${ID} -p ${localDir}/centos/7 &>> "${logFile}"
fi
if_Fail_exit "reposync ${ID}"
done
}
# 更新元数据
function update_repo(){
typeDirs=(
centos/7/base
centos/7/updates
centos/7/extras
epel
)
for tDir in ${typeDirs[*]}; do
createrepo --update ${localDir}/${tDir} &>> "${logFile}"
if_Fail_exit "createrepo ${localDir}/${tDir}"
done
}
# 创建组元数据(通过该配置,客户机才能使用yum groups list)
# 下载xml文件
function download_xml(){
wget -q -e robots=off -c -r -nd -np -k -L -p -A "$1" "$2" -P "$3"
}
# 创建组元数据
function create_groups(){
# 获取下载的xml文件名称
groupXml=$(ls "$2" | grep "xml$")
# 根据xml创建组元数据
createrepo -g "$2"/"${groupXml}" "$2" &>> "${logFile}"
if_Fail "create $1 group"
# 删除下载的xml文件(避免误删,确保路径存在)
if [[ -f "$2"/"${groupXml}" ]]; then
rm -f "$2"/"${groupXml}"
fi
}
# 部署组元数据
function deploy_groups(){
download_xml "x86_64-comps.xml" "${httpSite}/centos/7/os/x86_64/repodata/" "${localDir}/centos/7/base"
if [[ $? -eq 0 ]]; then
create_groups "base" "${localDir}/centos/7/base"
fi
download_xml "comps-Everything.x86_64.xml" "${httpsSite}/epel/7/x86_64/repodata/" "${localDir}/epel"
if [[ $? -eq 0 ]]; then
create_groups "epel" "${localDir}/epel"
fi
}
# 获取执行时间
function time_tag(){
timeTag=$(date "+%Y-%m-%d %H:%M:%S")
tFlag=""
tCount=30
let flagCount=tCount+2
for (( i = 1; i <= ${tCount}; i++ )); do
tFlag=$(printf "=")${tFlag}
done
printf "\n%-${flagCount}s" "${tFlag}"
printf "%-7s" "$1"
printf "%-21s" "${timeTag}"
printf "%-${flagCount}s\n" "${tFlag}"
} &>> "${logFile}"
# 日志文件
function log_file(){
if [[ ! -f ${logFile} ]]; then
touch ${logFile}
chmod 600 ${logFile}
fi
}
# 主函数
function main(){
# 日志文件
logFile="/var/log/localmirror.log"
# repo目录
repoDir="/etc/yum.repos.d"
# repo文件
latestRepo="CentOS7_latest.repo"
# 本地镜像仓库目录
localDir="/data/localMirror"
# 互联网镜像站域名(当前为阿里镜像站)
mirrorSite="mirrors.aliyun.com"
# 互联网镜像站(http)
httpSite="http://${mirrorSite}"
# 互联网镜像站(https)
httpsSite="https://${mirrorSite}"
time_tag "Start:"
log_file
check_Inernet
case $1 in
# 通过下载互联网公共镜像站最新CentOS7系列rpm包,部署本地yum镜像库
deploy )
install_rpm
bak_repo
create_repo
cache_yum
mkdir_localDir
sync_rpms
update_repo
deploy_groups
deploy_web
;;
# 更新增量rpm包,并更新元数据
update )
sync_rpms
update_repo
;;
# 修复客户机yum groups失效(本地yum镜像库修复后,客户机再手动清除yum缓存生效)
fixgroups )
deploy_groups
;;
# 修复镜像库repo文件
fixrepo )
bak_repo
create_repo
cache_yum
;;
*)
echo "Useage: sh $0 [deploy|update|fixgroups|fixrepo]"
exit 1
;;
esac
time_tag "End:"
}
# 参数合规性检查
keyWords="$1"
if [[ $# -ne 1 ]]; then
echo "Useage: sh $0 [deploy|update|fixgroups|fixrepo]"
exit
fi
main "${keyWords}"
使用方法
- 部署本地yum镜像库
# sh deploy_localMirror.sh deploy- 更新本地yum镜像库
# sh deploy_localMirror.sh update- 修复
客户机"yum groups [list|install|remove|xxx]"
命令失效
# sh deploy_localMirror.sh fixgroups- 修复
CentOS7_latest.repo
文件
# sh deploy_localMirror.sh fixrepo
问题
- 互联网镜像站
当前脚本默认配置为阿里镜像站,可通过修改变量mirrorSite
值实现切换到其他镜像站。
注意:
通过修改变量mirrorSite
值切换到其他镜像站后,还至少需要对function create_repo()
中的URL进行确认,避免URL错误导致无法访问情况
- 首次部署下载rpm包错误
首次部署
在下载rpm包因为某个包下载异常导致脚本退出,可再次执行相同命令继续首次部署。
例:
# sh deploy_localMirror.sh deploy
…
首次部署遇到下载某rpm时导致脚本退出,再次执行相同命令即可
# sh deploy_localMirror.sh deploy
- 增量更新下载rpm包错误
在首次部署完成后,后续进行增量更新
互联网镜像站最新rpm包
,因为下载某个包异常导致脚本退出,可再次执行相同命令继续增量更新下载。
例:
# sh deploy_localMirror.sh update
…
下载某个rpm包报错退出,再次执行相同命令即可
# sh deploy_localMirror.sh update
- 客户端服务器执行"yum groups [list|install|remove|xxx] xxx"报错
在本地镜像源已部署完成,且在客户端服务器使用"yum [list|install|remove|xxx] xxx"正常
。但使用"yum groups [list|install|remove|xxx] xxx"报错
本地镜像源端服务器:
本地镜像源已部署完成
客户端服务器:
1、已正确配置指向本地yum镜像源的repo文件(/etc/yum.repos.d/xxx.repo)
2、已执行
# yum clean all && yum makecache
3、正常使用
# yum [list|install|remove|xxx] xxx
4、异常使用
# yum groups [list|install|remove|xxx] xxx
本地镜像源端服务器:
在本地镜像源端服务器进行组元数据修复。
# sh deploy_localMirror.sh fixgroups
客户端服务器:
1、执行
# yum clean all && yum makecache
2、验证
# yum groups [list|install|remove|xxx] xxx