rsync服务模式+客户端访问

您所在的位置:网站首页 rsync服务端和客户端区别 rsync服务模式+客户端访问

rsync服务模式+客户端访问

2023-11-12 00:50| 来源: 网络整理| 查看: 265

介绍

rsync是一个差异同步备份工具,也是一种数据发送/ 差分备份 许可协议。 可以实现把服务器上的一些文件备份到另一台电脑上,而且是差异备份,这是用它的关键,也是取代scp的关键。  网上关于这个的介绍也有很多, 就不多作介绍了。

rsync有不同的工作模式, 主要就3种。

在同一台机器上用,跟cp差不多的用法。

跟scp一样的远程shell模式

服务器模式。

我们要用的就是服务器模式。

这里要用的是由xinetd超级守护进程来控制的rsync服务。 当然也可以不用,到那一步再说啦。

    

环境:

CentOS6.6_x86_64

rsync 3.0.6

防火墙关闭, SElinux 关闭。

正文

安装程序

配置文件

启动服务

客户端连接

ssh连接访问慢:http://blog.csdn.net/yefengnidie/article/details/8186942

一、 安装程序软件。

    首先我们的服务器上需要有xinetd和rsync。如果没有yum安装就可以啦。

yum -y install xinetd yum -y install rsync

   

 还有说明一下, 服务端和客户端是同一个软件,  它是不分服务端和客户端的, 只是分启动模式,也就是启动的时候要加的选项参数啦。

    如果rsync事先就已经安装,而xinetd没有安装,有可能会发现没有/etc/xinetd.d/rsync文件,直接重新安装一下rsync就行, 一般情况下也不会这样, 我电脑系统就是自带的rsync,后装的xinetd, 有那个文件。

yum -y reinstall rsync

    我们来看一下/etc/xinetd.d/rsync这个文件,占一下篇幅。

# default: off # description: The rsync server is a good addition to an ftp server, as it \ #       allows crc checksumming etc. service rsync {         disable = yes         flags           = IPv6         socket_type     = stream         wait            = no         user            = root         server          = /usr/bin/rsync         server_args     = --daemon         log_on_failure  += USERID }

    看到server_args后面那个--daemon了吧, 那个就是以守护进程的方式来启动rsync,也就是服务器模式的启动方式啦。 还有那个disable, 一会儿启动的时候可以把yes改为no,也可以用chkconfig来修改。  还有一个比较有意思的 flages, 在我的电脑这里要改成IPv4,要不然就监听在ipv6的地址上了。

    

二、配置文件。

    

    配置/etc/rsyncd.conf文件, 可能会没有这个文件,创建一个就可以。  

    内容分为两个部: 1 全局配置,    2 数据共享目录。

    数据共享目录可以配置多个。

# Global Settings   全局配置

uid = nobody                    #工作进程用户,运行进程的用户

gid = nobody         #工作进程组, 运行进程的用户组

use chroot = no                #是否使用chroot

max connections = 10           #最大连接,注意这可是实实在在的磁盘I/O,所以连接数不要太多。

strict modes = yes               #是否是严格模式。

pid file = /var/run/rsyncd.pid       #pid文件路径

log file = /var/log/rsyncd.log       #日志文件路径

# Directory to be synced  目录

[synced_name]                          #synced共享名称

 path = /path/to/some_dir             #共享目录路径

ignore errors            #忽略I/O错误

read only = no                       #是否是只读,只可以拉取数据。pull

write only = no                       #是否是只写,只可以推送数据。push

hosts allow = white_list_ip/net/*      #白名单。

hosts deny = black_list_ip/net/*       #黑名单。

        黑白名单说明: 默认会允许访问, 如果两项规则都没有匹配,则为默认。

          只出现白名单时,匹配的允计,没有被白名单匹配的,按默认。

          只出现黑名单时,匹配的拒绝,没有被匹配的按默认。

          二者同时出现时,先检查白名单,匹配则允计。

          否则,再检查黑名单,匹配则拒绝。

list = false                        #是否允许列出文件列表。

uid = root                            #以哪个用户的身份操作数据。

gid = root                          #以哪个用户组的身份操作数据。

auth users = username     #自定义用户。

secrets file = /etc/rsyncd.passwd        #用户的密码文件。  

    大体上就这些配置,可以按自己的需要修改。还有一些如端口还有其它的,man rsyncd.conf

    再凉下我自己的单子:

#Global setting uid = nobody gid = nobody use chroot = no max connections = 5 strict modes = yes pid file=/var/run/rsyncd.pid log file=/var/log/rsyncd.log # [test] path=/var/test ignore errors read only = no write only = no #hosts allow =  #hosts deny =  list = false uid = root gid = root auth users = ursync secrets file = /etc/rsyncd.passwd

    建立密码文件,密码文件的格式是: 用户:密码。   

    注意用户是你自定义的用户,在rsyncd.conf文件中出现的那个。

    文件都是明文, 注意权限600。 

-bash-4.1# cat /etc/rsyncd.passwd ursync:iampasswd -bash-4.1# chmod 600 /etc/rsyncd.passwd

我这里测试用的账户是ursync , 密码是:iampasswd    

建立共享目录, 我这里是/var/test.。

一切搞定,启动服务。

三、启动服务( 换电脑了, 按照上面重新来了一遍,希望不会出现什么问题)

[root@localhost ~]# chkconfig --list rsync rsync           off [root@localhost ~]# chkconfig rsync on [root@localhost ~]# service xinetd start Starting xinetd:                                           [  OK  ] [root@localhost ~]#[root@localhost ~]# ss -tnl | grep 873 LISTEN     0      64                       :::873                     :::*      [root@localhost ~]#

    

    果然,监听在ipv6的地址了。  把/etc/xinetd.d/rsync文件中的IPv6改成IPv4 。 重启xinetd服务。

[root@localhost ~]# service xinetd restart Stopping xinetd:                                           [  OK  ] Starting xinetd:                                           [  OK  ] [root@localhost ~]# ss -tnl | grep 873 LISTEN     0      64                        *:873                      *:*

    

    监听于873/tcp 端口。

  服务启动完成,接下来就是测试了。

   启动服务也可以不用xinetd来启动,直接rsync --daemon 启动也可以,只不过最好 再写个服务脚本来控制启动和关闭。

四、 客户端测试

    

rsync选项:

-n : 在不确定命令执行是否正确时,来进行测试执行,并输出信息上。

-v : --verbose, 详细输出模式

-q : --quiet,  静默模式,最小的信息输出。

-c :--checksum, 开启校验功能, 强制对文件传输进行校验。

-r : --recursive, 递归复制。

-a : --archives, 归档,相当于 -rlptgoD

-p : --perms, 保留文件的原有属性。

-t : --times     保留文件的时间戳。

-l : -links      复制符号链接文件,而不是跳过。

-g : --group     保留文件的属组。

-o : --owner     保留文件的属主。

-D : --devices   保留设备文件。

-e ssh:  表示使用ssh协议作承载  如:  rsync -e ssh -r /etc [email protected]:/tmp

-z:      对文件压缩后传输。

--progress  显示文件传输进度条。 不是整体的,是每一个文件的进度。

--stats     显示压缩和传输的状态。 完成以后的统计信息。

--password-file指定密码文件

    选项挺多,不过用的时候也就 也就几个用的着。  看自己的选择了。这里做测试也就用 -a -v -e --password-file 这几个而已。 

    rsync的访问模式也有几种,man rsync 最上面就是。 我们访问服务器有2种, 也是看习惯了。 不用太介意啦。这里只帖一种。 

         Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]          Push: rsync [OPTION...] SRC... [USER@]HOST::DEST

    上面那个是从服务器到本机。 下面那个刚好是反过来。 都是固定的格式, 看那两个冒号 :: 。如果是一个冒号的话就不是连接服务器了,只是一般的shell模式。冒号后面跟的就是服务端的共享名称了。 

    多说无益,来试一下:把客户端的/etc上传到服务器。 也就是Push.

[root@localhost etc]# rsync -a /etc [email protected]::test Password:  [root@localhost etc]#

    test就是服务端的共享名称.  

    没有加-v,所以没有显示过程, 大家测试的时候可以加上看看效果。 以现在服务端的配置,上传的符号链接文件都会自动加上/rsyncd-munged/,原因是因为rsync的安全机制导致的, 在 write only = on 并且,use chroot = no 的情况下就会这样。  至于use chroot这里就不说了, 只是给大家提醒下。

  看看服务器的:

[root@localhost ~]# ls /var/test etc [root@localhost ~]#

   

    注意因为我们上边写的是/etc,所以传的是/etc这个目录和下面所有文件。 如果写的是/etc/ 那么就是/etc下面的所有文件了。  

   客户端也可以指定密码文件, 直接在文件里面写上密码就可以, 注意啦, 只有密码,没有用户名, 跟服务端的密码文件不一样。

[root@localhost etc]# echo 'iampasswd' > /etc/rsyncd.passwd [root@localhost etc]# chmod 600 /etc/rsyncd.passwd

    

    再来:Pull

[root@localhost etc]# rsync -a --password-file /etc/rsyncd.passwd [email protected]::test/etc/fstab /tmp [root@localhost etc]# ll /tmp/fstab  -rw-r--r--. 1 root root 969 Apr 12 17:53 /tmp/fstab

    

    把服务端共享里面的etc目录下面的fstab 下载到本机的/tmp下。

    重点说明一下-e选项。  -e 可以让rsync承载在别的安全机制下。  毕竟rsync是不加密传输的。 

    一般我们用ssh。  所以服务端要有ssh的守护进程,如openssh-server, 客户端要有ssh工具,如openssh-clients。

[root@localhost etc]# rsync -a --password-file /etc/rsyncd.passwd -e ssh [email protected]::test/etc/fstab /tmp ssh: connect to host 192.168.1.200 port 22: Connection refused rsync: connection unexpectedly closed (0 bytes received so far) [receiver] rsync error: error in rsync protocol data stream (code 12) at io.c(600) [receiver=3.0.6]

    

    上面指定-e ssh,  但是结果好像不怎么好, 因为我这里服务端的ssh端口不是22,而默认它是连接22端口的。 所以,这里可以用ssh的参数。  -p 指定端口,  -l 指定ssh登录用户。 

    再试试:

[root@localhost etc]# rsync -a --password-file /etc/rsyncd.passwd -e 'ssh -p 23000 -l root' [email protected]::test/etc/ /tmp [email protected]'s password:  [root@localhost etc]#

  

    OK,可以了, 但是用的竟然是root用户, 别的用户我这里是会出故障的, 暂时也不清楚是什么问题。 而且如果换成别的用户, 备份就麻烦了。所备份文件属性是不能动的,而且既然是差异备份,那就肯定有些文件在备份的时候会被覆盖掉。别的用户想覆盖不同用户的文件,除了root是不可能的。

     我了解的现在最好的方式就是用ssh公钥认证登录了。而对于这方面的这里就不多写了, 总体也就两条指令。

[root@localhost etc]# ssh-keygen -t rsa            #生成密钥对的指令。 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa):  Enter passphrase (empty for no passphrase):  Enter same passphrase again:  Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: c6:78:d2:68:20:c6:05:87:f5:ef:be:35:9e:88:4f:91 [email protected] The key's randomart p_w_picpath is: +--[ RSA 2048]----+ |  .++            | | ..o .           | |  + . .          | | . . . * .       | |      = E        | |     . = .       | |        o o      | |       + + o     | |      ..=.o      | +-----------------+

    生成密钥对的时候会让你输入保存的位置, 不用管 默认就可以。 然后会让你输入密钥的密码,这里可千万不要写, 我们是要用rsync所挂载的ssh的, 如果每一次连接使用密钥的时候都要输入密码,那不就跟前面一样了吗, 我们要的是自动。 而且对于备份服务来说,又不面对公网, 安全级别也不用太高啦。

    第二条指令:  如果服务端的ssh端口是22的话: 

ssh-copy-id user@host

    

    就可以了。 会自动把公钥传到服务端的所属用户里去。    user@host 要用root登录啊。

    如果服务端不是22端口就这样来指定端口:

ssh-copy-id "-p port user@host"

  

    我这里的指令输出:

[root@localhost etc]# ssh-copy-id "-p 23000 [email protected]" [email protected]'s password:  Now try logging into the machine, with "ssh '-p 23000 [email protected]'", and check in:   .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. [root@localhost etc]#

    好啦,终于完成了。

[root@localhost etc]# rsync -a --password-file /etc/rsyncd.passwd -e 'ssh -p 23000 -l root' [email protected]::test/etc/ /tmp [root@localhost etc]#

    

    没有任何的输出啊。  加上-v再试一个:

[root@localhost etc]# rsync -a -v --password-file /etc/rsyncd.passwd -e 'ssh -p 23000 -l root' /var/named/ [email protected]::test sending incremental file list ./ arch.star.com.zone named.ca named.empty named.localhost named.loopback data/ dynamic/ slaves/ sent 2989 bytes  received 118 bytes  60.33 bytes/sec total size is 2524  speedup is 0.81 [root@localhost etc]#

好啦, 基本的应用也就这样了。   

最后发现: 用ssh的这种方式,服务端的rsync不用启动也可以, 直接是通过ssh端口来完成传输, 只要rsync软件存在,/etc/rsyncd.conf配置文件存在 就可以。

 最后补充:rsync+inotify可以实现实时的同步, 大家有兴趣可以搞搞,以后再补上吧。 

 补上inotify:   http://fanqie.blog.51cto.com/9382669/1642073 

 rsync深入,在网上找的:http://hi.baidu.com/jiaozhenqing/item/e84ca1c16ab4ab350931c69c

自己也是正在学习linux中,有很多的东西还都不懂,  如果有错误的,还请大侠们指出, 谢谢了。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3