用rsync同步yum源到本地服务器
唠叨
大家都知道,使用yum来安装或者更新现有的包是最方便不过的了,但是有的时候服务器出于安全方面考虑,一般只许进不许出,那怎么办?那有同学就说了,这个简单啊,搞个本地源不就好了,其实这个方法不是不行,只不过ISO自带的包一般都比较旧,那我想更新咋办?所以还是不能满足需求。
既然服务器不让我通过互联网下载需要的包,那我直接把网上的源搬到我本地服务器不就好了,我局域网安装或者更新也快,前提是,你得准备一台放置yum源的服务器,磁盘空间要够大,500G以上,最最重要的是,能访问互联网!
准备
- yum install -y httpd rsync
- iptables开放80 873端口,关闭selinux
配置
首先打开httpd.conf
- vi /etc/httpd/conf/httpd.conf
- 把 #ServerName www.example.com:80
- 改成 ServerName localhost:80
- 然后把欢迎页删除掉
- mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.bak
- 新建一个centos目录放置从互联网上同步下来的rpm包
- mkdir /var/www/html/centos
脚本
把以下同步脚本放置到合适的目录下即可,别忘了添加执行权限
- #!/bin/bash
- YUM_SITE="rsync://mirrors.kernel.org/centos/"
- #存放路径,我这里直接用的是httpd默认根目录
- LOCAL_PATH="/var/www/html/centos/"
- #需要同步哪些目录
- LOCAL_VER='./'
- BW_limit=2048
- LOCK_FILE="/var/log/yum_server.pid"
- RSYNC_PATH=""
- # check update yum server pid
- MY_PID=$$
- if [ -f $LOCK_FILE ]; then
- get_pid=`/bin/cat $LOCK_FILE`
- get_system_pid=`/bin/ps -ef|grep -v grep|grep $get_pid|wc -l`
- if [ $get_system_pid -eq 0 ] ; then
- echo $MY_PID>$LOCK_FILE
- else
- echo "Have update yum server now!"
- exit 1
- fi
- else
- echo $MY_PID>$LOCK_FILE
- fi
- # check rsync tool
- if [ -z $RSYNC_PATH ]; then
- RSYNC_PATH=`/usr/bin/whereis rsync|awk ' ''{print $2}'`
- if [ -z $RSYNC_PATH ]; then
- echo 'Not find rsync tool.'
- echo 'use comm: yum install -y rsync'
- fi
- fi
- # sync yum source
- for VER in $LOCAL_VER;
- do
- # Check whether there are local directory
- if [ ! -d "$LOCAL_PATH$VER" ] ; then
- echo "Create dir $LOCAL_PATH$VER"
- `/bin/mkdir -p $LOCAL_PATH$VER`
- fi
- # sync yum source
- echo "Start sync $LOCAL_PATH$VER"
- $RSYNC_PATH -avrtH --delete --bwlimit=$BW_limit --exclude "isos" $YUM_SITE$VER $LOCAL_PATH
- done
- # clean lock file
- `/bin/rm -f $LOCK_FILE`
- echo "rsync end $(date +%Y-%m-%d_%k:%M:%S)" >> /var/www/html/centos/centos_rsync_is_end.txt
- exit 1
同步的过程可能会很久,整个yum源有400多G,所以脚本会在全部同步完以后,生成一个时间文件在/var/www/html/centos_rsync_is_end.txt 假设你已经全部同步完,就启动httpd服务
- service httpd start
然后把以下repo文件放置到你的yum源客户端的/etc/yum.repo.d/下,后缀名必须是repo,我这边yum源服务端IP是192.168.1.69
- [base]
- name=CentOS - base
- baseurl=http://192.168.1.69/centos/$releasever/os/$basearch/
- gpgcheck=1
- gpgkey=http://192.168.1.69/centos/RPM-GPG-KEY-CentOS-6
- [updates]
- name=CentOS - updates
- baseurl=http://192.168.1.69/centos/$releasever/updates/$basearch/
- gpgcheck=1
- gpgkey=http://192.168.1.69/centos/RPM-GPG-KEY-CentOS-6
- [extras]
- name=CentOS - extras
- baseurl=http://192.168.1.69/centos/$releasever/extras/$basearch/
- gpgcheck=1
- gpgkey=http://192.168.1.69/centos/RPM-GPG-KEY-CentOS-6
到这里你就基本上完成了一个本地yum源服务器的搭建,现在还少一个定时任务,让rsync每天凌晨3点去同步一次互联网上的yum源
- crontab -e
- 添加一条定时任务
- * 3 * * * /bin/bash /etc/yum.repos.d/centos.yum.sh > /dev/null &
附上opensuse源的脚本
方法请参照上面的yum源,脚本内容就改动了一些地方,内容基本相同
- #!/bin/bash
- YUM_SITE="rsync://mirrors.ustc.edu.cn/opensuse/"
- LOCAL_PATH="/var/www/html/opensuse/"
- LOCAL_VER='distribution factory-tested factory update'
- BW_limit=2048
- LOCK_FILE="/var/log/opensuse_server.pid"
- RSYNC_PATH=""
- # check update yum server pid
- MY_PID=$$
- if [ -f $LOCK_FILE ]; then
- get_pid=`/bin/cat $LOCK_FILE`
- get_system_pid=`/bin/ps -ef|grep -v grep|grep $get_pid|wc -l`
- if [ $get_system_pid -eq 0 ] ; then
- echo $MY_PID>$LOCK_FILE
- else
- echo "Have update yum server now!"
- exit 1
- fi
- else
- echo $MY_PID>$LOCK_FILE
- fi
- # check rsync tool
- if [ -z $RSYNC_PATH ]; then
- RSYNC_PATH=`/usr/bin/whereis rsync|awk ' ''{print $2}'`
- if [ -z $RSYNC_PATH ]; then
- echo 'Not find rsync tool.'
- echo 'use comm: yum install -y rsync'
- fi
- fi
- # sync yum source
- for VER in $LOCAL_VER;
- do
- # Check whether there are local directory
- if [ ! -d "$LOCAL_PATH$VER" ] ; then
- echo "Create dir $LOCAL_PATH$VER"
- `/bin/mkdir -p $LOCAL_PATH$VER`
- fi
- # sync yum source
- echo "Start sync $LOCAL_PATH$VER"
- $RSYNC_PATH -avrtH --delete --bwlimit=$BW_limit --exclude "iso" $YUM_SITE$VER $LOCAL_PATH
- done
- # clean lock file
- `/bin/rm -f $LOCK_FILE`
- echo "rsync end $(date +%Y-%m-%d_%k:%M:%S)" >> /var/www/html/opensuse/opensuse_rsync_is_end.txt
- exit 1
转载请注明:
闲来无事 »
用rsync同步yum源到本地服务器