<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IGI&#039;s Blog &#187; mirror</title>
	<atom:link href="http://www.igigo.net/archives/tag/mirror/feed" rel="self" type="application/rss+xml" />
	<link>http://www.igigo.net</link>
	<description></description>
	<lastBuildDate>Fri, 09 Dec 2011 04:19:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
	
<!-- Start Of Script Generated By WP-PostViews Plus -->
<script type='text/javascript' src='http://www.igigo.net/wp-includes/js/jquery/jquery.js?ver=1.4.2'></script>
<script type="text/javascript">
/* <![CDATA[ */
/* ]]> */
</script>
<!-- End Of Script Generated By WP-PostViews Plus -->
	<item>
		<title>搭建linux mirror镜像</title>
		<link>http://www.igigo.net/archives/65</link>
		<comments>http://www.igigo.net/archives/65#comments</comments>
		<pubDate>Sun, 21 Feb 2010 16:29:49 +0000</pubDate>
		<dc:creator>igi</dc:creator>
				<category><![CDATA[开源镜像]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mirror]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.igigo.net/?p=65</guid>
		<description><![CDATA[linux mirror]]></description>
			<content:encoded><![CDATA[<p>在学校搞过ubuntu的镜像源，没想到工作后继续维护镜像源，可谓有缘，也是很是开心能为有需要的人尽点绵薄之力。在这个过程中，自己也学到很多</p>
<p>记得一开始的学校那个服务器跑的是win 2003，用xlight提供ftp服务，为了便于管理，给系统装上了cygwin。在一开始摸索中，尝试过lftp脚本去同步镜像源，但lftp必须所有文件都扫描一次，长时间的空闲连接就被服务器踢掉了。后来才知道普遍的做法是用rsync去同步，在debian网站上提供了一个写的比较完善的<a href="http://www.debian.org/mirror/anonftpsync">rsync脚本</a>。 该脚本提供了文件锁,进程锁等完善的处理，稍微修改就非常合适用来同步各镜像源</p>
<h3>空间需求</h3>
<p>对于镜像服务器，基本上是IO密集型， 对磁盘空间需求很大，如果只需要提供常用的i386,amd64两种架构的软件包及常用版本</p>
<pre class="brush: plain; title: ;">
fedora需要300G左右
ubuntu需要250G左右
debian需要250G左右
gentoo需要100G左右
archlinux需要50G左右
centos需要150G左右
cygwin需要10G左右
</pre>
<p>除此之外,还需要预留一定空间以保证不被新发布的版本或者大软件包塞爆硬盘。</p>
<h3>同步选择</h3>
<p>一般来说，除非你服务器的空间几乎无限，不然就得考虑排除没必要的架构和版本。以debian为例<br />
debian现有alpha、amd64、arm、armel、hppa、hurd-i386、i386、ia64、m68k、mipsel、mips、powerpc、s390、sh and sparc这么多架构的软件包，还有source代码包<br />
debian现有oldstable，stable，testing，unstable，还有experimental版本的<br />
如此多的架构和版本，我们要根据需要进行排除，rsync的exclude选项可以很好的完成这个任务，有时遇到服务器端有问题的文件或者目录,也可以通过exclude选项跳过去。</p>
<p>确定好需要同步的架构及版本后，就需要找寻合适的同步对象，一个发行版一般都有许多个镜像源，但只有部分提供rsync服务，在各自的官方网站一般都可以找到这些列表或者搭建镜像的帮助。镜像源之间是有一定的等级的，一般会有一个总的最高级别的服务器(简称根服务器)，所有新的软件包会现出现在它上面,然后它下级的镜像源再来它这里同步，然后还有下下级的镜像源来同步。如果能直接同步根服务器，那是最好的，因为你能最快获得新的软件包，但由于各种原因，根服务器不一定对所有用户都开放rsync同步服务，而是只允许特定ip列表才能访问其rsync服务(大部分发行版是这么作的)。一般来说，主要是根据访问速度及对方服务器的软件包的滞后程度来选择同步对象，国内开放rsync的开源镜像不多，周边地区速度不错的有台湾和日本的服务器，其他地方的速度就一般</p>
<h3>同步策略</h3>
<p>根据需要写好rsync脚本后，就需要添加crontab，以保持自动更新，推荐做法是一天更新几次，部分小源可以一天更新一次，如果只想一天更新一次，可以把时间放在深夜，一般规律都是凌晨5,6点访问量最小，在这段时间附近开始更新可以避开负载高峰。</p>
<h3>其他</h3>
<p>debian系的源，源目录都有pool目录，它存放所有软件包，如果使用了debian的同步脚本，或者在rsync同步时分两步,先同步这个目录,同步完成后再同步其他目录，这样就可以有效的避免服务器同步时其他人访问会有一定几率出错的问题，还有rsync的delete-after及delay-update选项，都可以避免未完成的更新对用户造成影响</p>
<p>贴下我稍微修改及注释的debian那个同步脚本</p>
<pre class="brush: bash; title: ;">
#! /bin/sh
set -e

# 此脚本由 http://www.debian.org/mirror/anonftpsync 修改而来
# 需要原脚本可以自行下载
# CVS: cvs.debian.org:/cvs/webwml - webwml/english/mirror/anonftpsync
# Version: $Id: anonftpsync,v 1.43 2008-06-15 18:16:04 spaillar Exp $

#增加mirror名变量，方便修改log名、lock名、mail信息
MIRROR=&quot;opensuse&quot;

RSYNC=&quot;/usr/bin/rsync&quot;
TO=&quot;/home/mirror/opensuse&quot;
RSYNC_HOST=&quot;rsync.opensuse.org&quot;
RSYNC_DIR=&quot;opensuse-hotstuff-160gb&quot;
LOGDIR=&quot;/home/mirror.scripts/log&quot;

# ARCH_EXCLUDE 控制需要排除的架构的包
# 对于Debian,以下是包含的架构
# alpha, amd64, arm, armel, hppa, hurd-i386, i386, ia64, m68k, mipsel, mips, powerpc, s390, sh and sparc
# 一个比较特殊的值: source
# 它将排除源代码包
# 例子
# ARCH_EXCLUDE=&quot;alpha arm armel hppa hurd-i386 ia64 m68k mipsel mips s390 sparc&quot;
#对于opensuse:
#这里应该是: x86_64 i586 i686 noarch source
ARCH_EXCLUDE=

# EXCLUDE变量将排除更多需要排除的包,非得以不建议使用这个变量
# The following example would exclude mostly everything:
#EXCLUDE=&quot;\
#  --exclude stable/ --exclude testing/ --exclude unstable/ \
#  --exclude source/ \
#  --exclude *.orig.tar.gz --exclude *.diff.gz --exclude *.dsc \
#  --exclude /contrib/ --exclude /non-free/ \
# &quot;
EXCLUDE=

#mail地址
#同步完成后把log发到指定邮箱,需要修改exim配置
MAILTO=

# LOCK_TIMEOUT是一个时间锁,以分钟为单位,默认锁6小时,即360分钟.
# 脚本运行时将创建lock文件,以保证同时间内只有一个rsync再运行
# 不同同步脚本的lock不能互相影响,以不同文件名区分.
LOCK_TIMEOUT=360

# RSYNC代理设置,一般不需要设置
# RSYNC_PROXY=&quot;IP:PORT&quot;
# export RSYNC_PROXY=$RSYNC_PROXY

# 帐号密码设置
# . ftpsync.conf
# export RSYNC_PASSWORD
# RSYNC_HOST=$RSYNC_USER@$RSYNC_HOST

# 检查各个重要变量是否为空
if [ -z &quot;$TO&quot; ] || [ -z &quot;$RSYNC_HOST&quot; ] || [ -z &quot;$RSYNC_DIR&quot; ] || [ -z &quot;$LOGDIR&quot; ] || [ -z &quot;$RSYNC&quot; ]; then
    echo &quot;One of the following variables seems to be empty:&quot;
    echo &quot;TO, RSYNC_HOST, RSYNC_DIR or LOGDIR&quot;
    exit 2
fi

#hostname变量,也可以手工指定
HOSTNAME=hostname -f
# HOSTNAME=mirror.domain.tld

#LOCK文件,绝对路径,建议放在统一目录,便于管理
LOCK=&quot;/home/mirror.scripts/lock/$MIRROR-Archive-Update-in-Progress-${HOSTNAME}&quot;

# 临时目录,由rsync --delay-updates 参数决定
# 必须保留,以避免错误,同步时所有新下载的都自动存放在临时目录
TMP_EXCLUDE=&quot;--exclude .~tmp~/&quot;

# 架构排除变量的展开语句
#以下是原脚本的,只适用debian系的部分发行版
#for ARCH in $ARCH_EXCLUDE; do
#   EXCLUDE=$EXCLUDE&quot;\
#       --exclude binary-$ARCH/ \
#       --exclude disks-$ARCH/ \
#       --exclude installer-$ARCH/ \
#       --exclude Contents-$ARCH.gz \
#       --exclude Contents-$ARCH.diff/ \
#       --exclude arch-$ARCH.files \
#       --exclude arch-$ARCH.list.gz \
#       --exclude *_$ARCH.deb \
#       --exclude *_$ARCH.udeb &quot;
#   if [ &quot;$ARCH&quot; = &quot;source&quot; ]; then
#       SOURCE_EXCLUDE=&quot;\
#       --exclude source/ \
#       --exclude *.tar.gz \
#       --exclude *.diff.gz \
#       --exclude *.dsc &quot;
#   fi
#done
#以下是为opensuse专门修改的
for ARCH in $ARCH_EXCLUDE; do
    EXCLUDE=$EXCLUDE&quot;\
        --exclude *.$ARCH.rpm&quot;
    if [ &quot;$ARCH&quot; = &quot;source&quot; ]; then
        SOURCE_EXCLUDE=&quot;\
        --exclude source/&quot;
    fi
done

#日志文件
LOGFILE=$LOGDIR/$MIRROR-mirror.log
# 可以使用下面的命名方式
# LOGFILE=$LOGDIR/$(echo $RSYNC_DIR | tr / _)-mirror.log
# LOGFILE=$LOGDIR/${RSYNC_DIR/\//_}-mirror.log

cd $HOME
umask 002

# 在第一次运行时创建trace文件,记录每次同步的时间记录
# 只在Debian的镜像中发现有此文件,其他发行版一般不需要
#if [ ! -d &quot;${TO}/project/trace/&quot; ]; then
#  mkdir -p ${TO}/project/trace
#fi

# 判断是否有同一脚本的rsync在运行,可以避免上一同步还没完而起多一个同步进程
if [ -f &quot;$LOCK&quot; ]; then
# Note: this requires the findutils find; for other finds, adjust as necessary
  if [ &quot;find $LOCK -maxdepth 1 -cmin -$LOCK_TIMEOUT&quot; = &quot;&quot; ]; then
# Note: this requires the procps ps; for other ps', adjust as necessary
    if ps ax | grep '[r]'sync | grep -q $RSYNC_HOST; then
      echo &quot;stale lock found, but a rsync is still running, aiee!&quot;
      exit 1
    else
      echo &quot;stale lock found (not accessed in the last $LOCK_TIMEOUT minutes), forcing update!&quot;
      rm -f $LOCK
    fi
  else
    echo &quot;current lock file exists, unable to start rsync!&quot;
    exit 1
  fi
fi

#生成lock
touch $LOCK

# 在部分非debian系统,需要用0代替exit
# 捕捉退出信号,以删除lock
# 脚本结尾也有一句同样效果的,这里起保证异常退出时能删除lock的作用
# 单rsync错误，还是顺序执行到最后的rm,然后再触发这里的trap
# 保证的是父进程的异常退出
trap &quot;rm -f $LOCK&quot; exit

# 我根据需要加的,可在手工运行脚本时,捕捉ctrl+c
# 这样能再按下ctrl+c后继续保存log及删除lock文件
trap &quot;&quot; 2

set +e

# 我根据需要加的,写个时间进log,方便查
date +[&quot;Start &quot;%F&quot; &quot;%T] &gt;&gt; $LOGFILE

# debian的原脚本把rsync分两步,先同步poor的内容
# 其他发行版不需要这么做
# timeout参数能在出现io错误时自动结速脚本,而不卡住
# delay-updates参数:先把下载的数据放tmp目录,同步完再移到正确位置
# 必须加此参数,可以避免未同步完的不完整包被用户下载导致错误
# 对于第一次同步，建议增加--size-only及--ignore-existing，以增加同步速度(在经常断开的情况下)
# 对于想删除不需要的exclude文件情况，可以增加--delete-excluded及--force(force用来强制删除空的不必要目录)
$RSYNC --recursive -p --links --hard-links --times \
     --progress \
     --verbose \
     --delay-updates --delete-after \
     --timeout=3600 \
     $TMP_EXCLUDE $EXCLUDE $SOURCE_EXCLUDE \
     $RSYNC_HOST::$RSYNC_DIR $TO/ &gt;&gt; $LOGFILE 2&gt;&amp;1

#chtime.sh用来记录源正常完成更新的时间
if [ $? -eq 0 ]
then
    /home/mirror.scripts/bin/chtime.sh opensuse &gt;&gt; $LOGFILE 2&gt;&amp;1
fi

#写时间进trace文件,非debian系统不需要
#LANG=C date -u &gt; &quot;${TO}/project/trace/${HOSTNAME}&quot;

#写结束时间进log
date +[&quot;End &quot;%F&quot; &quot;%T] &gt;&gt; $LOGFILE

#寄送邮件
if [ -n &quot;$MAILTO&quot; ]; then
    mail -s &quot;$MIRROR archive synced&quot; $MAILTO &lt; $LOGFILE
fi

#保存log
savelog $LOGFILE &gt;/dev/null

#最后删除lock文件
rm $LOCK
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.igigo.net/archives/65/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

