使用rsync来实现文件同步 部分参考 www.xfocus.net/articles/200107/214.html 先对这个dd进行简介,当你的网站访问量大了,一台服务器承受不住了 那好,这个时候,rsync来了。 rsync -»> remote synchronize 呵呵,不用解释了吧 第一步,先来下载这个免费的软件。 rsync.samba.org/ 或者 samba.anu.edu.au/rsync 解压缩后,编译很简单

./configure –prefix=/usr/local/rsync && make && make install
然后先来配置服务端吧
先来编辑服务端配置文件



#vi /etc/rsyncd.conf
uid = root
gid = root
hosts allow = testhost1
hosts deny = 192.168.0.*/24
use chroot = no  # 不使用chroot
max connections = 10     # 最大连接数为4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log  # 日志记录文件
[sync_files]     # 这里是认证的模块名,在client端需要指定
path = /root/sync_files # 需要做镜像的目录
comment = rsync 192.168.0.*
ignore errors    # 可以忽略一些无关的IO错误
read only = yes  # 只读
list = no        # 不允许列文件
auth users = rsyncuser   # 认证的用户名,如果没有这行,则表明是匿名,多个用户用,分隔
secrets file = /etc/rsyncd.pas  # 认证文件名
pid file = /var/run/rsyncd.pid
#motd file = /etc/rsyncd.motd
#log file = /var/log/rsyncd.log
#lock file = /var/run/rsync.lock

然后编辑你上面指定的认证文件,我这里是: /etc/rsyncd.pas

#vi /etc/rsyncd.pas
格式为: username:password
rsyncuser:123456
安全起见,更改认证文件属性
#chmod 0600 /etc/rsyncd.pas
ok,现在启动下试试看吧。
#/usr/local/rsync/bin/rsync -daemon
你也可以指定rsync运行的端口
#/usr/local/rsync/bin/rsync -daemon -port=873

如果要在启动时把服务起来,有几种不同的方法,比如:

加入inetd.conf
编辑/etc/services,加入rsync 873/tcp,指定rsync的服务端口是873
编加/etc/inetd.conf,加入rsync stream tcp nowait root /bin/rsync rsync –daemon

加入rc.local 在各种操作系统中,rc文件存放位置不尽相同,可以修改使系统启动时rsync –daemon加载进去。 我这里是直接加入 /etc/rc.local

/usr/local/rsync/bin/rsync -daemon -port=873

记得配置防火墙,允许你rsync端口的tcp和udp协议。 下面开始配置客户端 下面这个命令行中-vzrtopg里的v是verbose,z是压缩,r是recursive,topg都是保持文件原有属性如属主、时间的参数。–progress是指显示出详细的进度情况,–delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致。后面的inburst@ip中, inburst是指定密码文件中的用户名,之后的::inburst这一inburst是模块名,也就是在/etc/rsyncd.conf中自定义的名称。最后的/tmp是备份 到本地的目录名。 在这里面,还可以用-e ssh的参数建立起加密的连接。可以用–password-file=/password/path/file来指定密码文件,这样就可以在脚本中使 用而无需交互式地输入验证密码了,这里需要注意的是这份密码文件权限属性要设得只有属主可读。

#/usr/local/rsync/bin/rsync -vzrtopg -progress -delete inburst@192.168.0.52::inburst /tmp/

Password: 或者你也可以制定password文件 由于需要系统crontab执行,所以这里采用读入密码文件的方式,

#vi /etc/rsyncd.pas
加入
rsyncuser:123456
更改文件权限:
#chmod 0600 /etc/rsyncd.pas
#/usr/local/rsync/bin/rsync -vzrtopg –progress –delete –password-file=/etc/rsyncd.pas rsyncuser@192.168.0.6::/root/sync_files /tmp/
不知道为什么,总是给我报这样的错误:
@ERROR: auth failed on module sync_files
rsync error: error starting client-server protocol (code 5) at main.c(1171)
不管是加载密码文件还是输入密码,全部都报这个错误,为了节省时间,去掉了如下两行
auth users = rsyncuser   # 认证的用户名,如果没有这行,则表明是匿名,多个用户用,分隔
secrets file = /etc/rsyncd.pas  # 认证文件名

改为用ip进行限制操作。

#/usr/local/rsync/bin/rsync -vzrtopg –progress –delete –password-file=/etc/rsyncd.pas rsyncuser@192.168.0.6::/root/sync_files /tmp

这个时候就成功了。 以后有时间再研究认证问题吧,时间紧,任务急,把验证先绕过。