<?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; linux</title>
	<atom:link href="http://www.igigo.net/archives/tag/linux/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>bash重定向详解</title>
		<link>http://www.igigo.net/archives/79</link>
		<comments>http://www.igigo.net/archives/79#comments</comments>
		<pubDate>Wed, 23 Jun 2010 05:11:49 +0000</pubDate>
		<dc:creator>igi</dc:creator>
				<category><![CDATA[shell]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[Redirections]]></category>

		<guid isPermaLink="false">http://www.igigo.net/?p=79</guid>
		<description><![CDATA[首先我们先回顾下bash现有的重定向符号 1.重定向输入输出，目标是文件word [n]&#60;word 默认n为0 [n]&#62;word 默认n为1 [n]&#62;&#124;word 默认n为1 noclobber选项有关，直接例子就明白它的用处了 [n]&#62;&#62;word 默认n为1 igi@igi-debian:~$ rm -f testfile igi@igi-debian:~$ touch testfile igi@igi-debian:~$ cat testfile igi@igi-debian:~$ set -o noclobber igi@igi-debian:~$ echo 2 &#62;testfile bash: testfile: cannot overwrite existing file igi@igi-debian:~$ echo 2 &#62;&#124; testfile igi@igi-debian:~$ cat testfile 2 2.重定向标准错误和标准输出到指定文件描述符 &#38;&#62;word 更通用 &#62;&#38;word &#62;word 2&#62;&#38;1 追加输出 &#38;&#62;&#62;word 没有&#62;&#62;&#38;word的表达方法 &#62;&#62;word 2&#62;&#38;1 [...]]]></description>
			<content:encoded><![CDATA[<p>首先我们先回顾下bash现有的重定向符号</p>
<h3>1.重定向输入输出，目标是文件word</h3>
<pre class="brush: plain; gutter: false; title: ;">
[n]&lt;word    默认n为0
[n]&gt;word    默认n为1
[n]&gt;|word   默认n为1    noclobber选项有关，直接例子就明白它的用处了
[n]&gt;&gt;word   默认n为1
</pre>
<pre class="brush: bash; title: ;">
igi@igi-debian:~$ rm -f testfile
igi@igi-debian:~$ touch testfile
igi@igi-debian:~$ cat testfile
igi@igi-debian:~$ set -o noclobber
igi@igi-debian:~$ echo 2 &gt;testfile
bash: testfile: cannot overwrite existing file
igi@igi-debian:~$ echo 2 &gt;| testfile
igi@igi-debian:~$ cat testfile
2
</pre>
<h3>2.重定向标准错误和标准输出到指定文件描述符</h3>
<pre class="brush: plain; gutter: false; title: ;">
&amp;&gt;word      更通用
&gt;&amp;word
&gt;word 2&gt;&amp;1
</pre>
<p>追加输出</p>
<pre class="brush: plain; gutter: false; title: ;">
&amp;&gt;&gt;word     没有&gt;&gt;&amp;word的表达方法
&gt;&gt;word 2&gt;&amp;1
</pre>
<h3>3.Here Documents</h3>
<pre class="brush: plain; gutter: false; title: ;">
 &lt;&lt;[-]word
   here-document
delimiter
-符号将删除所有行开头的tab符
</pre>
<h3>4.Here Strings</h3>
<pre class="brush: plain; gutter: false; title: ;">
&lt;&lt;&lt;word
</pre>
<h3>5.复制文件描述符</h3>
<pre class="brush: plain; gutter: false; title: ;">
[n]&lt;&amp;word   默认n为0，如果为数字，必须得为打开的文件描述符
[n]&lt;&amp;-      关闭文件描述符

[n]&gt;&amp;word   默认n为1，如果为数字，必须得为打开的文件描述符
[n]&gt;&amp;-      关闭文件描述符
</pre>
<h3>6.移动文件描述符</h3>
<pre class="brush: plain; gutter: false; title: ;">
[n]&lt;&amp;digit- 默认n为0
[n]&gt;&amp;digit- 默认n为1
</pre>
<h3>7.以读写方式打开文件描述符</h3>
<pre class="brush: plain; gutter: false; title: ;">
[n]&lt;&gt;word   文件不在时会被创建
</pre>
<p>如果要深刻理解重定向，先要明白以下2点<br />
1.shell(bash或者csh等)负责重定向，对于程序或者函数，这一切都是透明的，它只管输入输出，至于从哪输入哪输出，是shell解释器负责<br />
2.shell命令解析过程中，在实际执行命令前，IO重定向先设置好</p>
<p>我们来看以下的例子</p>
<h4>1.&#8217;echo 1 a1 >a2&#8242; 与 &#8216;echo 1 >a2 a1&#8242;</h4>
<pre class="brush: bash; title: ;">
igi@igi-debian:~$ echo 1 a1 &gt;a2
igi@igi-debian:~$ cat a2
1 a1
igi@igi-debian:~$ rm a2
igi@igi-debian:~$ echo 1 &gt;a2 a1
igi@igi-debian:~$ cat a2
1 a1
</pre>
<p>IO重定向是在命令执行前设置好，所以上面两种情况，最后的效果一样，bash先把输出重定向到a2文件，再执行&#8217;echo 1 a1&#8242;</p>
<h4>2.&#8217;ls nothisfile >res 2>&#038;1&#8242; 与 &#8216;ls nothisfile 2>&#038;1 >res&#8217;</h4>
<pre class="brush: bash; title: ;">
igi@igi-debian:~/rtest$ ls nothisfile
ls: cannot access nothisfile: No such file or directory
igi@igi-debian:~/rtest$ ls nothisfile &gt;res 2&gt;&amp;1
igi@igi-debian:~/rtest$ cat res
ls: cannot access nothisfile: No such file or directory
igi@igi-debian:~/rtest$ ls nothisfile 2&gt;&amp;1 &gt;res
ls: cannot access nothisfile: No such file or directory
igi@igi-debian:~/rtest$ cat res
igi@igi-debian:~/rtest$ ls -1
a
b
c
res
igi@igi-debian:~/rtest$ ls -1 2&gt;&amp;1 &gt;res
igi@igi-debian:~/rtest$ cat res
a
b
c
res
</pre>
<p>
&#8216;ls nothisfile >res 2>&#038;1&#8242;，文件描述符1被重定向到文件res(本来是标准输出)，然后再把文件描述符2重定向到文件描述符1(此时是文件描述符1指向文件res)，最后执行&#8221;ls nothisfile&#8221;，产生错误，被送往文件描述符2，最后流向文件res。<br />
&#8216;ls nothisfile 2>&#038;1 >res&#8217;，文件描述符2被重定向到文件描述符1(即标准输出：屏幕)，然后再把文件描述符1重定向到文件res，结果是文件描述符2被重定向到标准输出，文件描述符1被重定向到文件res，最后执行&#8221;ls nothisfile&#8221;产生的错误就被送往屏幕。
</p>
<h4>3.&#8217;ls nothisfile a >&#038;word&#8217; 与 &#8216;ls nothisfile a >&#038;123456&#8242;</h4>
<pre class="brush: bash; title: ;">
igi@igi-debian:~/test/shell$ ls -1
a
igi@igi-debian:~/test/shell$ cat a
this is a
igi@igi-debian:~/test/shell$ ls nothisfile a &gt;&amp;word
igi@igi-debian:~/test/shell$ cat word
ls: cannot access nothisfile: No such file or directory
a
igi@igi-debian:~/test/shell$ ls nothisfile a &gt;&amp;123456
-bash: 123456: Bad file descriptor
igi@igi-debian:~/test/shell$ cat 123456
cat: 123456: No such file or directory
</pre>
<p>
>&#038;这个重定向符号，在前面有提到，“重定向标准错误和标准输出到指定文件描述符“ 和 “复制文件描述符“ 都有这个符号, 实际上“重定向标准错误和标准输出到指定文件描述符“ 是 “复制文件描述符“ 的一种特别情况， 即当 [n]>&#038;word 的n省略 且  word不是数字时， 会重定向标准错误和标准输出 到指定文件。<br />
&#8220;ls nothisfile a >&#038;word&#8221; , 由于word不是纯数字， bash解析成 ”重定向标准错误和标准输出到指定文件描述符“， 效果相当于 &#8221; ls nothisfile a >word 2>&#038;1&#8243;<br />
&#8220;ls nothisfile a >&#038;123456&#8243;, 由于123456是纯数字， bash解析成 &#8220;复制文件描述符&#8221;, 相当于 &#8221; ls nothisfile a 1>&#038;123456&#8243; , 但由于 “ 复制文件描述符 “规定　“如果为数字，必须得为打开的文件描述符”<br />
所以发生了错误。
</p>
<h4>4.&#8217;ls a 1>&#038;-&#8217; 与 &#8216; ls a >&#038;1- &#8216;</h4>
<pre class="brush: bash; title: ;">
igi@igi-debian:~/test/shell$ ls
a
igi@igi-debian:~/test/shell$ cat a
this is a
igi@igi-debian:~/test/shell$ ls a &gt;&amp;1-
a
igi@igi-debian:~/test/shell$ ls a 1&gt;&amp;-
ls: write error: Bad file descriptor
</pre>
<p>
“  ls a >&#038;1- “，　>&#038;1-属于“移动文件描述符” 提到的 &#8220;[n]>&#038;digit-&#8221;, 用文件描述符digit替换掉文件描述符n, n描述符被关闭.n默认为1.  &#8220;ls a >&#038;1-&#8221;,  相当与 &#8221; ls a 1>&#038;1- &#8220;,  把文件描述符1替换掉原<br />
文件描述符1, 然后关闭原文件描述符1, 没发生变化, 输出依然被送到屏幕<br />
&#8221; ls a 1>&#038;- &#8220;,  >&#038;- 属于 &#8220;复制文件描述符&#8221; 提到的&#8221;关闭文件描述符&#8221;,  &#8221; ls a 1>&#038;- &#8220;, 关闭了文件描述符1, 在运行&#8221; ls a&#8221;, 由于输出默认都送到文件描述符1, 而它被关闭, 报&#8221;错误的文件描述符&#8221;
</p>
<h4>5.&#8217;ls a nothisfile 1>&#038;2-&#8217; 与 &#8216; ls a nothisfile 1<&#038;2- '</h4>
<pre class="brush: bash; title: ;">
igi@igi-debian:~/test/shell$ ls -1
a
igi@igi-debian:~/test/shell$ ls a nothisfile 1&lt;&amp;2-
a
igi@igi-debian:~/test/shell$ ls a nothisfile 1&gt;&amp;2-
a
igi@igi-debian:~/test/shell$ exec 3&lt;&gt;test
igi@igi-debian:~/test/shell$ ls a nothisfile 1&gt;&amp;3-
ls: cannot access nothisfile: No such file or directory
igi@igi-debian:~/test/shell$ cat test
a
igi@igi-debian:~/test/shell$
</pre>
<p>
&#8216; 1>&#038;2- &#8216; 与 &#8216; 1<&#038;2- ' 是一个效果的, [n]>&#038;digit- 和 [n]<&#038;digit-  这两个移动文件描述符的操作, 都是移动digit 到 n , 区别在n没有指定时, <&#038;digit- 等于 0<&#038;digit-,  而 >&#038;digit- 等于 1>&#038;digit-<br />
移动文件描述符, 就是把描诉符digit的指向给描述符n, 然后关了digit.  例子中的exec很好的解释移动文件描述符的行为, 文件描述符3被定向到文件test, 然后1>&#038;3-, 会使得1也定向到3所定向的文件test, 然后文件描诉符3被关闭, 效果就是标准输出被定向到test文件.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.igigo.net/archives/79/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ssh无法通过公钥登录的问题</title>
		<link>http://www.igigo.net/archives/67</link>
		<comments>http://www.igigo.net/archives/67#comments</comments>
		<pubDate>Thu, 25 Mar 2010 10:30:36 +0000</pubDate>
		<dc:creator>igi</dc:creator>
				<category><![CDATA[小砸碎]]></category>
		<category><![CDATA[key]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://www.igigo.net/?p=67</guid>
		<description><![CDATA[帮一师弟解决一个无法通过公钥登录ssh服务器问题, 顺便总结下 现象: test帐号使用key无法登录某ssh服务器, 而同机器下的test2帐号却可以登录, 两个帐号都可以通过密码登录 在排查了所有配置错误的可能后, 初步怀疑test帐号的权限设置出问题 test@client:~$ ls -l ~/.ssh/ -rw------- 1 test test 1675 2010-03-25 15:15 id_rsa test@server:~$ ls -l ~/.ssh/ -rw-r--r-- 1 test test 396 2010-03-25 15:15 authorized_keys 查看了客户端及服务器端的.ssh目录下的公钥与私钥权限, 可以看出, 并没有问题 私钥必须是600权限, 而公钥至少是644或者更严格的权限, 这都符合, 但依然无法登录 test@server:~$ ls -la ~ &#124; grep -w .ssh drwxr-xr-x 2 test test 4.0K 12-23 16:59 .ssh [...]]]></description>
			<content:encoded><![CDATA[<p>帮一师弟解决一个无法通过公钥登录ssh服务器问题, 顺便总结下</p>
<p>现象: test帐号使用key无法登录某ssh服务器, 而同机器下的test2帐号却可以登录, 两个帐号都可以通过密码登录</p>
<p><span style="color: #008000;">在排查了所有配置错误的可能后</span>, 初步怀疑test帐号的权限设置出问题</p>
<pre class="brush: bash; title: ;">
test@client:~$ ls -l ~/.ssh/
-rw------- 1 test test   1675 2010-03-25 15:15 id_rsa
</pre>
<pre class="brush: bash; title: ;">
test@server:~$ ls -l ~/.ssh/
-rw-r--r-- 1 test test    396 2010-03-25 15:15 authorized_keys
</pre>
<p>查看了客户端及服务器端的.ssh目录下的公钥与私钥权限, 可以看出, 并没有问题<br />
私钥必须是600权限, 而公钥至少是644或者更严格的权限, 这都符合, 但依然无法登录</p>
<pre class="brush: bash; title: ;">
test@server:~$ ls -la ~ | grep -w .ssh
drwxr-xr-x  2 test  test  4.0K 12-23 16:59 .ssh
</pre>
<p>查看了服务器端的.ssh目录权限, 是755, 也是没问题的, ssh服务器要求在使用key登录时.ssh目录的权限必须是其他用户不可写。<br />
一开始实在想不明为啥test2帐号使用key可以登录,test帐号使用key无法登录, ssh_config和sshd_config在检查了多遍后确实没有问题, 最后在服务器端对比两个帐号的不同时, 发现了可疑的地方</p>
<pre class="brush: bash; title: ;">
$ls -l /home/
drwxrwxrwx   3 test test 4096 2009-12-31 17:31 test
drwxr-xr-x   6 test2 test2 4096 2010-03-23 15:59 test2
</pre>
<p>两个帐号的home目录权限不同, test帐号是777, test2帐号是755, 会不会是这里不同导致的? 在服务器端把test目录修改成777后, 解决问题</p>
<h3>为啥home目录的权限也会影响ssh的key登录?</h3>
<p>
ssh服务器的key方式登录对权限要求严格。对于客户端: 私钥必须为600权限或者更严格权限(400), 一旦其他用户可读, 私钥就不起作用(如640), 表现为系统认为不存在私钥<br />
对于服务器端: 要求必须公钥其他用户不可写, 一旦其他用户可写(如660), 就无法用key登录, 表现为:Permission denied (publickey).<br />
同时要求.ssh目录其他用户不可写,一旦其他用户可写(如770), 就无法使用key登录, 表现为:Permission denied (publickey).</p>
<h3>但为什么home目录的可写也影响到ssh使用key登录?</h3>
<p>我们知道linux下目录也是文件, 目录其他用户可写,代表其他用户可以删除目录里的所有文件或子目录,重命名目录下的所有文件或子目录.<br />
home目录的可写,表示其他用户对.ssh子目录也有改写的权限(删除或重命令),也就导致ssh判断.ssh为其他用户可写, 拒绝使用key登录</p>
]]></content:encoded>
			<wfw:commentRss>http://www.igigo.net/archives/67/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>

