架设rsync服务器

  

什么是rsync
rsync,remote synchronize顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息。 rsync是用 “rsync 算法”提供了一个客户机和远程文件服务器的文件同步的快速方法,而且可以通过ssh方式来传输文件,这样其保密性也非常好,另外它还是免费的软件。

一、rsync的安装

yum install -y rsync

二、配置rsync服务器端

 rsync的主要有以下三个配置文件rsyncd.conf(主配置文件)、rsyncd.secrets(密码文件)、rsyncd.motd(rysnc服务器信息)

具体步骤如下

touch /etc/rsyncd.conf  #创建rsyncd.conf,这是rsync服务器的配置文件。
touch /etc/rsync.passwd  #创建rsync.passwd ,这是用户密码文件。
chmod 600 /etc/rsyncd/rsync.passwd  #将rsync.passwd这个密码文件的文件属性设为root拥有, 且权限要设为600, 否则无法备份成功!

下一步就是我们修改rsyncd.conf和rsyncd.secrets和rsyncd.motd文件的时候了。

1、创建rsync服务器配置文件/etc/rsyncd.conf

[root@test160 ~]# vim /etc/rsyncd.conf
uid = root
gid = root
port = 873                               #  指定运行端口,默认是873,您可以自己指定
hosts allow = 192.168.0.204, 192.168.1.205          # 允许访问的客户机
#hosts deny = 0.0.0.0/32                      #拒绝访问的
use chroot = no                                # //不使用chroot
max connections =                              #最大连接数为
timeout=

## 下面这些绿色文件是安装完RSYNC服务后自动生成的文件,当然也可以手动配置到指定路径
pid file = /var/run/rsyncd.pid      ##pid文件的存放
lock file = /var/run/rsync.lock     ##锁文件的存放位置
log file = /var/log/rsyncd.log      ##日志记录文件的存放
#motd file = /etc/rsyncd.motd      #欢迎


## 上面这段是全局配置,下面的模块可以有
[test]                    ## 模块名字,自己命名
path = /data/shell               #指定文件目录所在位置,这是必须指定 
comment = rsync files                         ## 注释
exclude = easy/ samba/                     ##在/data/shell/中想把easy和samba目录排除在外
ignore errors                                 ##可以忽略一些无关的IO错误
read only = yes                                ## 只读,不让客户端上传文件到服务器上
list = no                                     ## 是否把rsync 服务器上提供同步数据的目录显示
## 下面这一行,同步验证时用的账号,如果没有这项就是匿名同步,client同步时不用用户名也能同步。
auth users = www             #此用户与系统无关
secrets file = /etc/rsyncd.passwd             ## 指定认证文件

2、配置rsync密码(在上边的配置文件中已经写好路径)

[root@test160 ~]# vim /etc/rsync.passwd
www:www123          ## 用户名:密码。注意这个不是系统用户,只是rsync用户。所以不用useradd。

(名字随便写,只要和上边配置文件里的“auth users”参数一致即可),格式(一行一个用户)
     账号:密码

把密码文件的权限改成600

[root@test160 ~]# chmod 600 /etc/rsync.passwd          ## 只能所有者可读,否则报错

3、配置文件中指定了欢迎信息

[root@test160 ~]# vim /etc/rsyncd.motd 
      Welcome the rsync services

三、启动rsync服务器

启动rsync服务器相当简单,有以下2种方法
A、--daemon参数,表示以守护进程的方式启动rsync服务

#/usr/bin/rsync --daemon  --config=/etc/rsyncd/rsyncd.conf  #--config用于指定rsyncd.conf的位置,如果在/etc下可以不写

B、xinetd方式

修改services加入如下内容

# vi /etc/services
rsync  873/tcp  # rsync 
rsync  873/udp  # rsync

这一步一般可以不做,通常都有这两行(我的RHEL4和GENTOO默认都有)。修改的目的是让系统知道873端口对应的服务名为rsync。如没有的话就自行加入。

设定 /etc/xinetd.d/rsync, 简单例子如下:

# default: off
# description: The rsync server is a good addition to am ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
     disable = yes                        ## yes表示禁用这个服务  
     socket_type     = stream             ## 表示服务的数据包类型为stream
     wait            = no                 ## 表示不需等待,即服务将以多线程的方式运行
     user            = root               ## 表示执行此服务进程的用户是root
     server          = /usr/bin/rsync     ## 启动脚本的位置
     server_args     = --daemon
     log_on_failure  += USERID            ## 表示设置失败时,UID添加到系统登记表
}

上述, 主要是要打开rsync這個daemon, 一旦有rsync client要连接時, xinetd会把它转介給 rsyncd(port 873)。然后service xinetd restart, 使上述设定生效。

四、防火墙设置

设置iptables让rsync 服务器端口873通过,客户端上也应该让通过。

#iptables -A INPUT -p tcp -m state --state NEW  -m tcp --dport 873 -j ACCEPT
#iptables -L  查看一下防火墙是不是打开了 873端口

不太懂防火墙的配置,可以先service iptables stop 将防火墙关掉。

五、启动rsync后的检查操作

[root@test160 init.d]# rsync --daemon  或者service xinetd restart
[root@test160 init.d]# ps -ef|grep rsync    #xinetd启动没有这个进程
root      5160     1  0 14:23 ?        00:00:00 rsync --daemon
root      5162  4253  0 14:23 pts/1    00:00:00 grep rsync
[root@test160 init.d]# lsof -i:3873
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rsync   5160 root    4u  IPv4  22289      0t0  TCP *:fagordnc (LISTEN)
rsync   5160 root    5u  IPv6  22290      0t0  TCP *:fagordnc (LISTEN)

六、设置rsync服务开启自启动操作

echo "/usr/bin/rsync --daemon" >>/etc/rc.local

注意:当然还可以用chkconfig rsync on 命令,但是要编写适合chkconfig操作的脚本。

七、重启rsync的命令

pkill rsync    #---关闭rsync服务
rsync --daemon #---开启rsync服务重启rsync的命令
kill和killall -9 这个两个命令杀进程,如果杀了,有可能这个服务就起不来了
[root@test160 init.d]# /usr/bin/rsync --daemon

遇到服务起不来,报错:failed to create pid file /var/run/rsyncd.pid: File exist
解决:
rm -f /var/run/rsyncd.pid
rsync --daemon
八、完成

搭建完成,就可以通过rsync客户端执行一些示例来测试同步数据。

 

posted @ 2017-12-06 11:34  Gringer  阅读(158)  评论(0)    收藏  举报