平时可以利用ssh客户端密钥建立信任关系来使rsync同步,但可能存在用户权限过大的问题. 这里以daemon或服务的方式运行可以提高安全性.
一.rsync的安装 rsync现在系统一般都自带 a.yum install rsync
b.http://rsync.samba.org/ 使用rpm或源码等方式安装
二.配置rsync rsync的主要有以下三个配置文件rsyncd.conf(主配置文件)、rsyncd.secrets(密码文件)、rsyncd.motd(rysnc服务器信息)
1.服务器配置文件(/etc/rsyncd.conf),该文件默认不存在,请创建它。 注:关于auth users是必须在服务器上存在的真实的系统用户,如果你想用多个用户以,号隔开,比如auth users = www,root 定义一个discuz的模块 vi /etc/rsyncd.conf
pid file = /var/run/rsyncd.pid port = 873 address = 192.168.0.24 #uid = nobody #gid = nobody uid = www gid = website use chroot = yes read only = no #limit access to private LANs hosts allow=192.168.0.0/255.255.255.0 hosts deny=* max connections = 5 motd file = /etc/rsyncd.motd #This will give you a separate log file #log file = /var/log/rsync.log #This will log every file transferred – up to 85,000+ per user, per sync #transfer logging = yes log format = %t %a %m %f %b syslog facility = local3 timeout = 300 [discuz] path = /opt/htdocs/bbs list=no ignore errors auth users = www secrets file = /etc/rsyncd.secrets comment = This is bbs data #exclude =2.设定密码文件 密码文件格式很简单,rsyncd.secrets的内容格式为: 用户名:密码 我们在例子中rsyncd.secrets的内容如下类似的;在文档中说,有些系统不支持长密码,自己尝试着设置一下吧。 vi /etc/rsyncd.secrets
www:c1gstudiopasschown root:root /etc/rsyncd.secrets #修改属主 chmod 600 /etc/rsyncd.secrets #修改权限
注:1、将rsyncd.secrets这个密码文件的文件属性设为root拥有, 且权限要设为600, 否则无法备份成功! 出于安全目的,文件的属性必需是只有属主可读。 2、这里的密码值得注意,为了安全你不能把系统用户的密码写在这里。比如你的系统用户www密码是000000,为了安全你可以让rsync中的www为c1gstudiopass。这和samba的用户认证的密码原理是差不多的。
3.设定rsyncd.motd 文件; 它是定义rysnc服务器信息的,也就是用户登录信息。比如让用户知道这个服务器是谁提供的等;类似ftp服务器登录时,我们所看到的 c1gstudio.com ftp ……。 当然这在全局定义变量时,并不是必须的,你可以用#号注掉,或删除;我在这里写了一个 rsyncd.motd的内容为: vi /etc/rsyncd.motd
++++++++++++++++++++++++++++++++++++++++++++++ rsync services! staff use only ++++++++++++++++++++++++++++++++++++++++++++++三.运行 A. xinetd方式 省略
B. –daemon参数方式,是让rsync以服务器模式运行
/usr/bin/rsync –daemon –config=/etc/rsyncd.conf #–config用于指定rsyncd.conf的位置,如果在/etc下可以不写添加到/etc/rc.local,启动后自动运行
echo ‘/usr/bin/rsync –daemon –config=/etc/rsyncd.conf’ >> /etc/rc.local防火墙 授权192.168.0.0 C类
iptables -A INPUT -s 192.168.0.0/24 -p tcp -m tcp –dport 873 -j ACCEPT授权单个IP192.168.0.21
iptables -A INPUT -s 192.168.0.21 -p tcp -m state –state NEW -m tcp –dport 873 -j ACCEPT/etc/init.d/iptables save
四,调试 192.168.0.24为rsynce daemon端,接收数据 192.168.0.21为源数据推送端,发送数据
推送端调试列表 rsync –list-only [email protected]:: rsync: failed to connect to 192.168.0.24: Connection timed out (110) 检查防火墙,rsyncd里用户权限 传送本地/opt/htdocs/bbs/data/cache 到远端discuz模块下的data目录
rsync -av /opt/htdocs/bbs/data/cache [email protected]::discuz:/data/创建密码文件方便免登录,密码和服务器端保持一致
echo ‘c1gstudiopass’ > /etc/rsyncd.passwd chown root:root /etc/rsyncd.passwd chmod 600 /etc/rsyncd.passwd以密码文件全部同步一次
rsync -av –delete –password-file=/etc/rsyncd.passwd /opt/htdocs/bbs/ [email protected]::discuz最后再以脚本方式配合inotify和crontab就可以实现自动同步
参考:http://www.cnblogs.com/itech/archive/2009/08/10/1542945.html
我们公司用内网vpn做的.