标题:rsync的坑 出处:Felix021 时间:Wed, 24 Aug 2011 18:41:01 +0000 作者:felix021 地址:https://www.felix021.com/blog/read.php?2034 内容: rsync是个好东西,同步文件很方便,等啥时候有空了,可以考虑把内核提供的inotify功能结合起来,搞一个实时文件同步的应用 :D 回归主题,说说这个坑,线上的机器用rsync来进行文件的同步。 $ rsync -az --delete /home/user/files/ user@remote_host:/home/user/files/ user@remote_host's password: TERM environment variable not set. protocol version mismatch -- is your shell clean? (see the rsync man page for an explanation) rsync error: protocol incompatibility (code 2) at compat.c(174) [sender=3.0.8] 从昨天到今天这个问题一直出现,不管是2.6.8还是最新的3.0.8,看了N个使用例子,最后结果都是这样。Boluor同学的建议是配置信任关系(ssh-keygen -t rsa; ssh-copy-id user@remote_host),但是这样只是不需要输入密码,并没有解决问题。 于是RTFW,Google了一下,发现在 http://ss64.com/bash/rsync.html 的DIAGNOSTIC 一节有说明, .bashrc 等启动脚本的任何输出,都会影响rsync通过ssh同步时的数据流,导致协议不匹配。 以这个命令为例: $ ssh user@remote_host /bin/true > out.txt 实际上所有的动作是 1. ssh 与 remote_host 的 sshd 交互 2. sshd fork出一个 user shell(一般是/bin/bash) 3. /bin/bash 载入 $USER/.bash_profile 4. .bash_profile一般会载入.bashrc 5. /bin/bash 执行/bin/true 并重定向stdin到out.txt 正常情况下,out.txt应该是一个空文件。如果在这个过程中有任何输出(比如我当时的.bashrc有一个echo),就会导致rsync在执行过程中的数据有额外的GARBAGE,出现问题。 OVER. Generated by Bo-blog 2.1.0