1.同步yum源的脚本
#cat /data/yum_repo/rsync_yum_shell/rsync_yumrepo.sh
#!/bin/bash
#script name:rsync_yumrepo.sh
RsyncBin="/usr/bin/rsync"
RsyncPerm='-avrt --delete --no-iconv --bwlimit=100000'
Centos_7_base='/data/yum_repo/Centos-7/Base/'
Centos_7_epel='/data/yum_repo/Centos-7/Epel/'
Centos_7_extras='/data/yum_repo/Centos-7/extras/'
Centos_6_base='/data/yum_repo/Centos-6/Base/'
Centos_6_epel='/data/yum_repo/Centos-6/Epel/'
Centos_6_extras='/data/yum_repo/Centos-6/extras/'
LogFile='/data/yum_repo/rsync_yum_log'
Date=`date +%Y-%m-%d`
#check
function check {
if [ $? -eq 0 ];then
echo -e "\033[1;32mRsync is success!\033[0m" >>$LogFile/$Date.log
else
echo -e "\033[1;31mRsync is fail!\033[0m" >>$LogFile/$Date.log
fi
}
if [ ! -d "$Centos_7_base" ];then
mkdir -pv $Centos_7_base
fi
if [ ! -d "$Centos_7_epel" ];then
mkdir -pv $Centos_7_epel
fi
if [ ! -d "$Centos_7_extras" ];then
mkdir -pv $Centos_7_extras
fi
if [ ! -d "$Centos_6_base" ];then
mkdir -pv $Centos_6_base
fi
if [ ! -d "$Centos_6_epel" ];then
mkdir -pv $Centos_6_epel
fi
if [ ! -d "$Centos_6_extras" ];then
mkdir -pv $Centos_6_extras
fi
if [ ! -d "$LogFile" ];then
mkdir $LogFile
fi
#rsync centos 6 base
echo 'Now start to rsync centos 6 base!' >>$LogFile/$Date.log
$RsyncBin $RsyncPerm --exclude=i386/ rsync://mirrors.yun-idc.com/centos/6/os/ $Centos_6_base >>$LogFile/$Date.log
check
#rsync centos 6 epel
echo 'Now start to rsync centos 6 epel!' >>$LogFile/$Date.log
$RsyncBin $RsyncPerm --exclude=SRPMS/ --exclude=ppc64/ --exclude=i386/ rsync://mirrors.yun-idc.com/epel/6/ $Centos_6_epel >>$LogFile/$Date.log
check
#rsync centos 6 extras
echo 'Now start to rsync centos 6 extras!' >>$LogFile/$Date.log
$RsyncBin $RsyncPerm --exclude=i386/ rsync://mirrors.yun-idc.com/centos/6/extras/ $Centos_6_extras >>$LogFile/$Date.log
check
#rsync centos 7 base
echo 'Now start to rsync centos 7 base!' >>$LogFile/$Date.log
$RsyncBin $RsyncPerm rsync://mirrors.yun-idc.com/centos/7/os/ $Centos_7_base >>$LogFile/$Date.log
check
#rsync centos 7 epel
echo 'Now start to rsync centos 7 epel!' >>$LogFile/$Date.log
$RsyncBin $RsyncPerm --exclude=SRPMS/ --exclude=ppc64/ rsync://mirrors.yun-idc.com/epel/7/ $Centos_7_epel >>$LogFile/$Date.log
check
#rsync centos 7 extras
echo 'Now start to rsync centos 7 extras!' >>$LogFile/$Date.log
$RsyncBin $RsyncPerm rsync://mirrors.yun-idc.com/centos/7/extras/ $Centos_7_extras >>$LogFile/$Date.log
check
脚本参考大神的博客:http://dl528888.blog.51cto.com/2382721/1342653
任务计划:
周六的凌晨0点20分开始执行更新
20 00 * * 6 /bin/bash /data/yum_repo/rsync_yum_shell/rsync_yumrepo.sh
趁周末下载的,大概46GB。
2.配置nginx服务器
参考nginx编译安装http://sohudrgon.blog.51cto.com/3088108/1596485
配置虚拟主机参考:http://sohudrgon.blog.51cto.com/3088108/1596655
我的nginx配置文件:
server {
listen 8000;
server_name yum.stu15.cn;
index index.html index.htm;
root /data/yum_repo/;
location / {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
error_page 502 = /502.html;
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /var/log/nginx/access.log access;
}
3.配置repo文件
CentOS-Base-6.repo
[Base]
name=CentOS-Base6 $basearch
baseurl=http://192.168.1.15:8000/Centos-6/Base/$basearch
enable=1
gpgcheck=1
gpgkey=http://192.168.1.15:8000/Centos-6/Base/$basearch/RPM-GPG-KEY-CentOS-6
[extras]
name=CentOS-Base6 $basearch
baseurl=http://192.168.1.15:8000/Centos-6/extras/$basearch
enable=1
gpgcheck=0
[Epel]
name=CentOS-Base6 $basearch
baseurl=http://192.168.1.15:8000/Centos-6/Epel/$basearch
enable=1
gpgcheck=0
CentOS-Base-7.repo
[Base]
name=CentOS-Base7 $basearch
baseurl=http://192.168.1.15:8000/Centos-7/Base/$basearch
enable=1
gpgcheck=1
gpgkey=http://192.168.1.15:8000/Centos-7/Base/$basearch/RPM-GPG-KEY-CentOS-7
[extras]
name=CentOS-Base6 $basearch
baseurl=http://192.168.1.15:8000/Centos-7/extras/$basearch
enable=1
gpgcheck=0
[Epel]
name=CentOS-Base6 $basearch
baseurl=http://192.168.1.15:8000/Centos-7/Epel/$basearch
enable=1
gpgcheck=0
根据需要选择repo文件。
然后进行下面操作:
按照不同的系统选择不同的repo文件,放到服务器的/etc/yum.repos.d里;
删除之前/etc/yum.repos.d里的repo;
清理缓存yum clean all;
将服务器上的软件包信息先在本地缓存,以提高搜索安装软件的速度 yum makecache.
现在本地yum源就已经完成,可以控制软件源,并且由于走内网所有不浪费机房公网网络流量。
批量重命名系统已经存在的repo文件:
# ls CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo epel-aliyun.repo # for i in `ls`; do mv -f $i `echo $i".bak"`; done # ls CentOS-Base.repo.bak CentOS-Debuginfo.repo.bak CentOS-Media.repo.bak CentOS-Vault.repo.bak epel-aliyun.repo.bak
或者删除目录下面的repo文件:
#ansible all -m shell -a 'find /etc/yum.repos.d/ -name"*.bak" -exec rm -f {} \; '
4.脚本批量更新测试环境的所有服务器的yum源为本地15的:
#cat createrepo.sh #!/bin/bash cd /etc/yum.repos.d/ for i in `ls`; do mv -f $i `echo $i".bak"`; done cat <>CentOS-Base-6.repo [Base] name=CentOS-Base6 \$basearch baseurl=http://192.168.1.15:8000/Centos-6/Base/\$basearch enable=1 gpgcheck=1 gpgkey=http://192.168.1.15:8000/Centos-6/Base/\$basearch/RPM-GPG-KEY-CentOS-6 [extras] name=CentOS-Base6 \$basearch baseurl=http://192.168.1.15:8000/Centos-6/extras/\$basearch enable=1 gpgcheck=0 [Epel] name=CentOS-Base6 \$basearch baseurl=http://192.168.1.15:8000/Centos-6/Epel/\$basearch enable=1 gpgcheck=0 EOF yum clean all yum makecache find /etc/yum.repos.d/ -name "*.bak" -exec rm -f {} \;
5.在15上使用ansible批量推送脚本到服务器,然后执行脚本:
# ansible all -m copy -a 'src="./createrepo.sh" dest="/root/createrepo.sh"'
# ansible all -m shell -a 'bash /root/createrepo.sh'
测试使用本地yum源安装zsh,然后卸载:
# ansible all -m yum -a "name=zsh state=present"
# ansible all -m yum -a "name=zsh state=removed"