1. 拷贝本地文件;当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。
2.使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。
3.使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。
4. 从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。
5. 从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。
6. 列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。
-a 以archive模式操作、复制目录、符号连接 相当于-rlptgoD
注:前面是rsync所提供的数据源,也就是我们在rsyncd.conf中所写的[rhel4home]模块。而“This is RHEL 4 data”是由[rhel4home]模块中的 comment = This is RHEL 4 data 提供的;为什么没有把rhel4opt数据源列出来呢?因为我们在[rhel4opt]中已经把list=no了。
注: 这个命令的意思就是说,用root用户登录到服务器上,把rhel4home数据,同步到本地当前目录rhel4home上。当然本地的目录是可以你自己 定义的。如果当你在客户端上当前操作的目录下没有rhel4home这个目录时,系统会自动为你创建一个;当存在rhel4home这个目录中,你要注意 它的写权限。
这回我们引入一个--delete 选项,表示客户端上的数据要与服务器端完全一致,如果 linuxsirhome目录中有服务器上不存在的文件,则删除。最终目的是让linuxsirhome目录上的数据完全与服务器上保持一致;用的时候要 小心点,最好不要把已经有重要数所据的目录,当做本地更新目录,否则会把你的数据全部删除;
这次我们加了一个选项 --password-file=/etc/rsyncd.secrets,这是当我们以root用户登录rsync服务器同步数据时,密码将读取/etc/rsyncd.secrets这个文件。这个文件内容只是root用户的密码。我们要如下做;
服务器是重量级应用,所以数据的网络备份还是极为重要的。我们可以在生产型服务器上配置好rsync 服务器。我们可以把一台装有rysnc机器当做是备份服务器。让这台备份服务器,每天在早上4点开始同步服务器上的数据;并且每个备份都是完整备份。有时 硬盘坏掉,或者服务器数据被删除,完整备份还是相当重要的。这种备份相当于每天为服务器的数据做一个镜像,当生产型服务器发生事故时,我们可以轻松恢复数 据,能把数据损失降到最低;是不是这么回事??
注: 我们在 /etc/cron.daily/中创建了两个文件rhel4home.sh和rhel4opt.sh ,并且是权限是755的。创建了两个密码文件root用户用的是rsyncrhel4root.secrets ,easylife用户用的是 rsyncrhel4easylife.secrets,权限是600;
然后我们再/home目录下创建rhel4homebak 和rhel4optbak两个目录,意思是服务器端的rhel4home数据同步到备份服务器上的/home/rhel4homebak 下,rhel4opt数据同步到 /home/rhel4optbak/目录下。并按年月日归档创建目录;每天备份都存档;
有个Solaris部署文档,参考:http://www.blogjava.net/lishengliang/archive/2006/04/26/43309.html
1. 通过ssh-keygen在server A上建立SSH keys,不要指定密码,你会在~/.ssh下看到identity和identity.pub文件
2. 在server B上的home目录建立子目录.ssh
3. 将A的identity.pub拷贝到server B上
4. 将identity.pub加到~[user b]/.ssh/authorized_keys
5. 于是server A上的A用户,可通过下面命令以用户B ssh到server B上了。e.g. ssh -l userB serverB。这样就使server A上的用户A就可以ssh以用户B的身份无需密码登陆到server B上了。
这通常有两种情况,一种是服务器在防火墙内,一种是服务器在防火墙外。无论哪种情况,通常还是使用ssh,这时最好新建一个备份用户,并且配置sshd 仅允许这个用户通过RSA认证方式进入。如果服务器在防火墙内,则最好限定客户端的IP地址,拒绝其它所有连接。如果客户机在防火墙内,则可以简单允许防 火墙打开TCP端口22的ssh外发连接就ok了。
A:当然可 以。你可以使用如:rsync -other -options -backupdir = ./backup-2000-2-13 ...这样的命令来实现。这样如果源文件:/path/to/some/file.c改变了,那么旧的文件就会被移到./backup- 2000-2-13/path/to/some/file.c,这里这个目录需要自己手工建立起来
A:rsync使用时默认是用uid=nobody;gid=nobody来运行的,如果你的系统不存在nobody组的话,就会出现这样的错误,可以试试gid = ogroup或者其它
A: 这通常是您的 rsyncd.conf 中的 path 路径所设的那个目录并不存在所致.请先用 mkdir开设好备份目录.
*************************************************************************************************
错误1:
在最后带上--password-file=/cygdrive/f/mysql5.5.16/cwRsync/pwd.secrets时,提示:
password file must not be other-accessible
continuing without password file
Password:
网上答案是:WINDOW出现这种情况,是因为pwd.secrets的权限不对,或存放的位置不是NTFS分区。但二者均检验过,仍无效。
尝试了:
A、用/cygdrive/f...
B、路径改成f:/和f:\,
C、把--password-file选项放前面或后面
D、把pwd.secrets的属性安全只保留WINDOWS帐户或cwRsync,且只读。
E、把linux其它客户端上的rsyncd.pwd拖下来给window也不行。
四种情况均无效。有待进一步尝试。
在linux上出现这种错误则容易解决。原因是这份客户端的密码文件必需设成调用者只读属性“600”才行。
错误2:
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(794) [receive
r=3.0.2]
解决:很大可能是服务器端没有开启 rsync 服务。开启服务。
错误3:
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1495) [receiver=3.0.2]
解决:服务器端同步目录没有权限,cwrsync 默认用户是Svcwrsync。为同步
目录添加用户Svcwrsync 权限。
错误4:
rsync: failed to connect to 210.28.93.4: No route to host (113)
是server端口的防火墙端口没开。
错误5:
@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
是服务器上忘记新建目录了…
错误6:
rsync warning: some files vanished before they could be transferred (code 24) at main.c(1039) [sender=3.0.6]
本地文件在同步过程中被删除了。
错误7:
rsync error: some files could not be transferred (code 23) at main.c(1385) [generator=2.6.9]
这是因为在服务器端的备份文件名中有非法符号,可能造成Windows同步数据时无法识别文件名。
错误8:
@ERROR: failed to open lock file
rsync error: error starting client-server protocol (code 5) at main.c(1495) [rec
eiver=3.0.2]
解决:配置文件 rsync.conf 中添加 lock file = rsyncd.lock 即可解决。
错误9:
@ERROR: auth failed on module XXX
出现这种情况,先检查你的用户名、密码、主机IP地址是否正确,
如果都正确,有一个可能是原因是:远程rsync服务器的帐户密码文件的权限必须为600,例如,你在rsyncd.conf中设置了secrets file =
/etc/rsyncd/rsync_pwd
那么你就必须确保rsync_pwd的访问权限为600:
chmod 600 /etc/rsyncd/rsync_pwd
然后你的问题可能就解决了。
常见错误:
错误一
rsync: failed to connect to 96.44.169.178 (*inet_ntop failed*): Connection timed
out (116)
1 [main] rsync 3468 exception::handle: Exception: STATUS_ACCESS_VIOLATION
740 [main] rsync 3468 open_stackdumpfile: Dumping stack trace to rsync.exe.s
tackdump
1,防火墙问题
2,端口不对
错误二
@ERROR: auth failed on module www
rsync error: error starting client-server protocol (code 5) at main.c(1516) [Rec
eiver=3.0.8]
密码不对
错误三
@ERROR: invalid uid nobody
rsync error: error starting client-server protocol (code 5) at main.c(1516) [Rec
eiver=3.0.8]
UID不对,默认是nobody
解决方法:
uid = 0
gid = 0
错误四
receiving incremental file list
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(769) [receive
r=3.0.8]
rsync: connection unexpectedly closed (60 bytes received so far) [generator]
rsync error: error in rsync protocol data stream (code 12) at io.c(610) [generat
or=3.0.8]
可能原因:
1,磁盘挂载是用异步的(async)
然后,检查了/etc/fstab ,去掉async参数。
2,我在服务器上查看日志,看到有这么一行:
rsync: unable to open configuration file "/etc/rsyncd.conf": No such file or directory
于是我:
ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf
3,同步文件数较多的目录出错
有个子目录中文件较多,也就8000来个吧,总是同步一半便退出。在批处理中加上-v参数,看到错误信息如下:
rsync: writefd_unbuffered failed to write 4 bytes to socket [sender]: Connection reset by peer (104)
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(768) [sender=
3.0.6]
这个问题有点头疼,在www.itefix.no网站论坛上也没能查出个所以然。
不过最终还是在samba.org上找到解决方案:
在客户端命令行中加上--no-iconv参数就可以了。
原以为是文件太多,缓冲区不够引起,但看这个解决方案,似乎是转换编码方面的bug了。
在rsync的文档中描述如下:
http://rsync.samba.org/ftp/rsync/rsync.html
--iconv=CONVERT_SPEC
Rsync can convert filenames between character sets using this option. Using a CONVERT_SPEC of "." tells rsync to look up the default character-set via the locale setting. Alternately, you can fully specify what conversion to do by giving a local and a remote charset separated by a comma in the order --iconv=LOCAL,REMOTE, e.g. --iconv=utf8,iso88591. This order ensures that the option will stay the same whether you're pushing or pulling files. Finally, you can specify either --no-iconv or a CONVERT_SPEC of "-" to turn off any conversion. The default setting of this option is site-specific, and can also be affected via the RSYNC_ICONV environment variable.
五、问题排除及注意事项
问题排除:
1、问题1
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(614) [receiver=2.6.8]
解决:在服务器上查看日志,看到有这么一行:
rsync: unable to open configuration file “/etc/rsyncd.conf”: No such file or directory
于是执行如下命令:
[root@bogon ~]# ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf
但是继续报如下错误:
sending incremental file list
ERROR: module is read only
rsync error: syntax or usage error (code 1) at main.c(879) [Receiver=3.0.8]
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(760) [sender=3.0.8]
到备份服务器上修改主配置文件
[root@bogon ~]# vim /etc/rsyncd/rsyncd.conf
把
read only=yes
改为
read only=no
问题解决,实现单向同步
2、问题2
[root@www backup]# rsync -vzrtopg –progress –delete promise@172.16.10.189::promise /home/bak/
Password:
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1516) [Receiver=3.0.8]
解决:目录设置错误
确定配置文件里的目录正确
3、问题3
在Linux下使用rsync同步时,出现以下错误:
@ERROR: auth failed on module bakup
其中,bakup是rsync服务模块名称。
出现这种情况,先检查了用户名和密码正确性,经检查无误,排除了这个可能性,另外可能的原因是:rsync服务器的帐户密码文件的权限没有设置为600,修改/etc/rsyncd/rsyncd.secrets文件:
[root@bogon ~]#
chmod 600 /etc/rsyncd/rsyncd.secrets
问题解决。
4、问题4
在备份路径后面一定要加“/”,否则在双向同步时会一直循环同步。如:
src=/var/www/ //后面一定要加“/”
5、问题5
同步mysql数据时需要注意:
本地源主机(H1)是一台web服务器,把/var/lib/mysql备份到备份服务器(H2),当web服务器上的数据受损时需要从备份服务器把数据同步到web服务器
执行脚本文件:/usr/local/sbin/rsync.sh
同步备份服务器上的/var/lib/mysql到web服务器后仍然无法访问网站,此时需要重启mysql
#/etc/init.d/mysqld restart
问题解决。
网上相关资料:
资料一:
最近在配置rsync服务器镜像备份。中间遇到@ERROR: auth failed on module 这个问题,网上找了好多资料,也具体对照测试了。
1、
[root@bo bin]# /usr/local/rsync/bin/rsync -vazu -progress –delete backup@192.168.1.238::www /tmp –password-file=/etc/backserver.pas
rsync: Failed to exec ss: No such file or directory (2)
rsync error: error in IPC code (code 14) at pipe.c(86) [receiver=2.6.9]
rsync: writefd_unbuffered failed to write 12 bytes [receiver]: Broken pipe (32)
rsync error: error in rsync protocol data stream (code 12) at io.c(1122) [receiver=2.6.9]
[root@ht-store bin]#
以上错误可能是上面使用的参数前面的符号不正确。换成下面的就没有上面的错误了
[root@bo /]# usr/local/rsync/bin/rsync -vzrtopgu –progress –delete –password-file=/etc/backserver.pas backup@192.168.1.238:www /tmp
2、
[root@ht-store /]# usr/local/rsync/bin/rsync -vzrtopgu –progress –delete backup@192.168.1.238:www /tmp/ –password-file=/etc/backserver.pas
backup@192.168.1.238′s password:
Permission denied, please try again.
backup@192.168.1.238′s password:
Permission denied, please try again.
backup@192.168.1.238′s password:
Permission denied (publickey,gssapi-with-mic,password).
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: unexplained error (code 255) at io.c(453) [receiver=2.6.9]
[root@ht-store /]#
更改成
[root@ht-store /]# usr/local/rsync/bin/rsync -vzrtopgu –progress –delete –password-file=/etc/backserver.pas
backup@192.168.1.238::www /tmp
@ERROR: auth failed on module www
rsync error: error starting client-server protocol (code 5) at main.c(1383) [receiver=2.6.9]
[root@ht-store /]#
3、
[root@ht-store /]# /usr/local/rsync/bin/rsync -vazu –progress –delete –password-file=/etc/backserver.pas
backserver@192.168.1.238::www /tmp
@ERROR: auth failed on module www
rsync error: error starting client-server protocol (code 5) at main.c(1383) [receiver=2.6.9]
检查一下用户名是否错误backserver 应该是backup
4、
[root@ht-store /]# /usr/local/rsync/bin/rsync -vzar –progress –delete –password-file=/etc/backserver.pas backup@192.168.1.238:www /tmp
backup@192.168.1.238′s password:
Permission denied, please try again.
backup@192.168.1.238′s password:
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(244) [receiver=2.6.9]
是双引号“::”backup@192.168.1.238::www 而不是单引号“:”backup@192.168.1.238:www
5、
[root@ht-store /]# /usr/local/rsync/bin/rsync -vzrtopgu –progress –delete –password-file=/etc/backserver.pas backup@192.168.1.238::www /tmp
@ERROR: Unknown module ‘www’
rsync error: error starting client-server protocol (code 5) at main.c(1383) [receiver=2.6.9]
这是因为服务器端hosts allow 没有允许该主机访问
资料二:
1. Q: 出現以下這個訊息, 是怎麼一回事?
@ERROR: auth failed on module xxxxx
rsync: connection unexpectedly closed (90 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
A: 這是因為密碼設錯了, 無法登入成功, 請再檢查一下 rsyncd.pwd ,rsyncd.sec中的密碼設定, 二端是否一致?有时服务器
端没有起动服务也会出现这种情况
2. Q: 出現以下這個訊息, 是怎麼一回事?
password file must not be other-accessible
continuing without password file
Password:
A: 這表示 rsyncd.pwd rsyncd.sec 的檔案權限屬性不對, 應設為 600
請下 chmod 600 rsyncd.pwd rsyncd.sec
3. Q: 出現以下這個訊息, 是怎麼一回事?
@ERROR: chroot failed
rsync: connection unexpectedly closed (75 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
A: 這通常是您的 rsyncd.conf 中
的 path 路徑所設的那個目錄並不存在所致.
請先用 mkdir 開設好備份目錄.
4.Q:出现一下信息是怎么回事
rsync: failed to connect to 218.107.243.2: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9]
所以情況會是:
1. 對方主機沒開機
2. 對方有 firewall 阻擋
3. 所通過的網路上有 firewall 阻擋
shell>setup #关闭防火墙,其实最重要的也就是把tcp udp 的873端口打开。
5.Q:出现一下信息是怎么回事
当
/server/rsync/bin/rsync -vzrutogp –progress –password-file=/etc/rsyncclien
t.pwd backup@60.173.7.35:backup /var/www/backup
出现
backup@60.173.7.6′s password
Permission denied, please try again.
最有可能的情况是,系统没有认出来,你现在的通信是rsync,所以请仔细检查一下上面加红的那句话中ip地址后面是否为两个:也就是说::为rsync通信,:为ssh通信
6.Q:
当/server/rsync/bin/rsync rsync://backup@61.156.238.119/backup时,出现如下
rsync: link_stat “/.” (in backup) failed: Permission denied (13)
rsync error: some files could not be transferred (code 23) at main.c(1385) [receiver=2.6.9]
解决方法:更改你的rsyncd.conf中的
uid=nobody
gid=nobody
为
uid=root
gid=root
解决方法2
出现这样问题的原因是我要同步的这个root目录在掩位的最后一位
不是r-x或以上的权限,也就是说root这个目录一定要等于或大于drwxr-xr-x才能同步。
附录一:源主机和备份服务器的配置文档
源主机配置文件
见附件<rsync.sh>、<rsyncd.pass>
备份服务器配置文件
见附件<rsyncd.conf>、<rsyncd.motd>、<rsyncd.secrets>
附录二:Rsync的INSTALL文档
见附件<INSTALL_Rsync>
附录三:Inotify的INSTLL文档
见附件<INSTALL_Inotify>
注意,有时候安装时会出点末知原因,例如,不能程序本身没有注册服务,可以通过好动方式:
C:\ICW\bin>cygrunsrv.exe -I "Rsync" -p "/cygdrive/C/ICW/bin/rsync.exe" -a "--config=/cygdrive/C/ICW/rsyncd.conf --daemon --no-detach" -f "Rsync"
*************************************************************************************************
5、常用选项:
*************************************************************************************************
-vzrtopg --delete是常用选项。
【-u选项可以保障当从机有更新过同名文件时,不会在同步时被主机更改。】
【--delete如果想保持客户端和服务端真正同步,应使用此选项,这样服务端删掉某文件时,客户端也会删掉】
【-e ssh如果想用ssh传输,则应加此选项】
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-v, --verbose 详细模式输出
-q, --quiet 精简输出模式
-c, --checksum 打开校验开关,强制对文件传输进行校验
-r, --recursive 对子目录以递归模式处理
-R, --relative 使用相对路径信息
-b, --backup 创建备份,即对目的已经存在有同名文件时,将老的文件重新命名为~filename。可以使用--suffix 选项来指定不同的备份文件前缀
--backup-dir 将备份文件(如~filename)存放在在目录下。
-suffix=SUFFIX 定义备份文件前缀
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)【重要】
-l, --links 保留软链结
-L, --copy-links 想对待常规文件一样处理软链结
--copy-unsafe-links 仅仅拷贝指向SRC 路径目录树以外的链结
--safe-links 忽略指向SRC 路径目录树以外的链结
-H, --hard-links 保留硬链结
-p, --perms 保持文件权限
-o, --owner 保持文件属主信息
-g, --group 保持文件属组信息
-D, --devices 保持设备文件信息
-t, --times 保持文件时间信息
-S, --sparse 对稀疏文件进行特殊处理以节省DST 的空间
-n, --dry-run 现实哪些文件将被传输
-W, --whole-file 拷贝文件,不进行增量检测
-x, --one-file-system 不要跨越文件系统边界
-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700 字节
--password-file=/password/path/file指定密码文件,这样可以在脚本中使用而无需交互式地输入验证密码了,需要注意的是这份密码文件权限属性要设得只有属主可读。chmod 600 /etc/rsyncd.pwd
-e, --rsh=COMMAND 指定使用rsh、ssh 方式进行数据同步
--rsync-path=PATH 指定远程服务器上的rsync 命令所在路径信息
-C, --cvs-exclude 使用和CVS 一样的方法自动忽略文件,用来排除那些不希望
传输的文件
--existing 仅仅更新那些已经存在于DST 的文件,而不备份那些新创建的文件
--delete-excluded 同样删除接收端那些被该选项指定排除的文件
--delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致。【慎用】
--delete-after 传输结束以后再删除
--ignore-errors 及时出现IO 错误也进行删除
--max-delete=NUM 最多删除NUM 个文件
--partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
--force 强制删除目录,即使不为空
--numeric-ids 不将数字的用户和组ID 匹配为用户名和组名
--timeout=TIME IP 超时时间,单位为秒
-I, --ignore-times 不跳过那些有同样的时间和长度的文件
--size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
--modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
-T --temp-dir=DIR 在DIR 中创建临时文件
--compare-dest=DIR 同样比较DIR 中的文件来决定是否需要备份
-P 等同于 --partial
--progress 显示备份过程
-z, --compress 对备份的文件在传输时进行压缩处理
--exclude=PATTERN 指定排除不需要传输的文件模式
--include=PATTERN 指定不排除而需要传输的文件模式
--exclude-from=FILE 排除FILE 中指定模式的文件
--include-from=FILE 不排除FILE 指定模式匹配的文件
--version 打印版本信息
--address 绑定到特定的地址
--config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf 文件
--port=PORT 指定其他的rsync 服务端口
--blocking-io 对远程shell 使用阻塞IO
--stats 给出某些文件的传输状态
--progress 在传输时现实传输过程
--log-format=formAT 指定日志文件格式
下面是我自己用的一段小代码
cd "C:\Program Files\ICW\bin\"
c:
rsync.exe -ravz --progress Balance@158.161.58.13::CRM61 --port 18910 /cygdrive/D/CRM61/Library/ <D:\CRM61\a.txt
rsync.exe -ravz --progress Balance@158.161.58.26::CustomPages --port 18910 /cygdrive/D/Sage/CRM/MetenCRM/WWWRoot/CustomPages/ <D:\CRM61\a.txt