10.31 rsync通过ssh同步

10.31 rsync通过ssh同步

  • rsync通过ssh方式同步
  • rsync -av test1/ 192.168.133.132:/tmp/test2/
  • rsync -av -e "ssh -p 22" test1/ 192.168.133.132:/tmp/test2/
##rsync通过ssh方式同步:
[root@Dasoncheng ~]# rsync -av ./1.txt 192.168.60.12:/tmp/2.txt
root@192.168.60.12's password:   ##这里需要输入远程机器密码,相当于ssh登录之后再执行rsync;
sending incremental file list
1.txt

sent 136 bytes  received 31 bytes  47.71 bytes/sec
total size is 63  speedup is 0.38
[root@Dasoncheng2 ~]# ls /tmp
g2.sh
mongodb-27017.sock
systemd-private-20588b1fbaaf484980b8b102f16b40b7-vmtoolsd.service-SK9Q8q
[root@Dasoncheng2 ~]# ls /tmp
2.txt  mongodb-27017.sock  ##这里1.txt已经过来了,并修改为2.txt了;
g2.sh  systemd-private-20588b1fbaaf484980b8b102f16b40b7-vmtoolsd.service-SK9Q8q
[root@Dasoncheng2 ~]# 
##ssh登录远程机器:
[root@Dasoncheng ~]# ssh -p 22 192.168.60.12
root@192.168.60.12's password: 
Last login: Wed Dec 13 09:35:05 2017 from 192.168.60.1
[root@Dasoncheng2 ~]#    ##登录到了Dasoncheng2机器;
[root@Dasoncheng2 ~]# exit
logout
Connection to 192.168.60.12 closed.
##rsync通过ssh方式同步(并指定端口):
[root@Dasoncheng ~]# rsync -av -e "ssh -p 22" ./1.txt 192.168.60.12:/tmp/3.txt
root@192.168.60.12's password: 
sending incremental file list
1.txt

sent 136 bytes  received 31 bytes  47.71 bytes/sec
total size is 63  speedup is 0.38
  • rsync 通过服务的方式同步
  • 要编辑配置文件/etc/rsyncd.conf (如果配置文件不在/etc目录下,则启动服务时 需要加上--config=/customdir指定配置文件路径)
  • 启动服务rsync --daemon
  • 格式:rsync -av test1/ 192.168.133.130::module/dir/
rsyncd.conf样例
[root@Dasoncheng2 ~]# vim /etc/rsyncd.conf
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.133.130
[test]
path=/tmp/rsync/  ##注意权限问题;
use chroot=true
max connections=4
read only=no
list=true
uid=root
gid=root
auth users=test
secrets file=/etc/rsyncd.passwd
hosts allow=192.168.133.132 1.1.1.1 2.2.2.2  192.168.133.0/24
rsyncd.conf配置文件详解
  • port:指定在哪个端口启动rsyncd服务,默认是873端口。
  • log file:指定日志文件。
  • pid file:指定pid文件,这个文件的作用涉及服务的启动、停止等进程管理操作。
  • address:指定启动rsyncd服务的IP。假如你的机器有多个IP,就可以指定由其中一个启动rsyncd服务,如果不指定该参数,默认是在全部IP上启动。
  • []:指定模块名,里面内容自定义。
  • path:指定数据存放的路径。
  • use chroot true|false:表示在传输文件前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true,如果你的数据当中有软连接文件,阿铭建议你设置成false。(即将所有操作限制在目录/tmp/rsync里面,会影响软链接等的操作如 -L获取软链接源文件时)
  • max connections:指定最大的连接数,默认是0,即没有限制。
  • read only true|false:如果为true,则不能上传到该模块指定的路径下。
  • list:表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,false则隐藏。(用法:rsync的::后面不接任何模块,即列出所有可用模块名--安全选项)
  • uid/gid:指定传输文件时以哪个用户/组的身份传输。
  • auth users:指定传输时要使用的用户名。
  • secrets file:指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。注意该密码文件的权限一定要是600。格式:用户名:密码
  • hosts allow:表示被允许连接该模块的主机,可以是IP或者网段,如果是多个,中间用空格隔开。(当设置了auth users和secrets file后,客户端连服务端也需要用用户名密码了,若想在命令行中带上密码,可以设定一个密码文件)
  • rsync -avL test@192.168.133.130::test/test1/ /tmp/test8/ --password-file=/etc/pass
  • 其中/etc/pass内容就是一个密码,权限要改为600

10.32 rsync通过服务同步

##编辑服务端/etc/rsyncd.conf添加如下内容(如果配置文件不在/etc目录下,则启动需要指定文件目录);
[root@Dasoncheng2 ~]# cat /etc/rsyncd.conf
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.60.12
[test]
path=/tmp/rsync
use chroot=yes
max connections=4
read only=no
list=true
uid=root
gid=root
#auth users=test  这里暂时将用户认证关闭(除了修改端口外的配置文件都不需要重启服务);测试
#secrets file=/etc/rsyncd.passwd
hosts allow=192.168.133.132 1.1.1.1 2.2.2.2  192.168.60.0/24
[root@Dasoncheng2 ~]# ls -ld /tmp/rsync/  
drwxrwxrwx 2 root root 43 Dec 13 10:59 /tmp/rsync/   ##给予777权限,方便测试(其他细节 请参考上面配置文件详情);

Dasoncheng1(客户端):

[root@Dasoncheng ~]# ls
1.txt  anaconda-ks.cfg  redis2.conf  sbin.tar.gz  test2     wc
aaa    data.txt         rpmbuild     test1        test.txt
[root@Dasoncheng ~]# rsync -avP aaa 192.168.60.12::test/bbb
sending incremental file list
aaa
        1319 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 1386 bytes  received 27 bytes  2826.00 bytes/sec
total size is 1319  speedup is 0.93

Dasoncheng2(服务端):

[root@Dasoncheng2 ~]# rm -f /tmp/rsync/*
##同步之后:
[root@Dasoncheng2 ~]# ls /tmp/rsync/
bbb
##客户端也可以 拉取服务端文件;
设置密码文件登录;
##/etc/rsyncd.conf取消掉这两行的#号  启用用户密码验证;
[root@Dasoncheng2 ~]# vim /etc/rsyncd.conf
auth users=test
secrets file=/etc/rsyncd.passwd
[root@Dasoncheng2 ~]# cat /etc/rsyncd.passwd   ##创建密码文件;
test:p@ssw0rd

客户端同步:

[root@Dasoncheng ~]# rsync -avP aaa test@192.168.60.12::test/ccc
Password: 
@ERROR: auth failed on module test
rsync error: error starting client-server protocol (code 5) at main.c(1516) [sender=3.0.9]
##同步失败,报错  咱回服务器查看日志

服务器日志:

[root@Dasoncheng2 ~]# cat /var/log/rsync.log |tail
2017/12/25 11:05:03 [41394] connect from client (192.168.60.11)
2017/12/25 11:05:06 [41394] secrets file must not be other-accessible (see strict modes option)    ##这里提示密码文件 不能被其他人访问;
2017/12/25 11:05:06 [41394] continuing without secrets file
2017/12/25 11:05:06 [41394] auth failed on module test from client (192.168.60.11): missing secret for user "test"
2017/12/25 11:05:34 [41510] params.c:Parameter() - Ignoring badly formed line in configuration file: q
##接下来设置密码文件权限:
[root@Dasoncheng2 ~]# chmod 770 /etc/rsyncd.passwd
##客户端同步:
[root@Dasoncheng ~]# rsync -avP aaa test@192.168.60.12::test/ccc
Password: 
sending incremental file list
aaa
        1319 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 1386 bytes  received 27 bytes  403.71 bytes/sec
total size is 1319  speedup is 0.93  ##同步成功;
##服务端查看同步文件:
[root@Dasoncheng2 ~]# ls /tmp/rsync/
bbb  ccc

问题:如果我要用脚本呢?
法1、关闭用户密码验证;
法2、客户端保存一个密码文件(里面只有密码即可);
[root@Dasoncheng ~]# vim /etc/rsync_passwd.txt
[root@Dasoncheng ~]# cat /etc/rsync_passwd.txt
p@ssw0rd
[root@Dasoncheng ~]# chmod 600 /etc/rsync_passwd.txt
[root@Dasoncheng ~]# rsync -avP --port 8730 wc test@192.168.60.12::test/eee --password-file=/etc/rsync_passwd.txt
借助--password-file=/etc/rsync_passwd.txt传送;

修改端口;
[root@Dasoncheng2 ~]# vim /etc/rsyncd.conf
[root@Dasoncheng2 ~]# cat /etc/rsyncd.conf |grep port=
port=8730
[root@Dasoncheng2 ~]# killall rsync
-bash: killall: command not found
[root@Dasoncheng2 ~]# yum provides killall
[root@Dasoncheng2 ~]# yum install -y psmisc
[root@Dasoncheng2 ~]# killall rsync
[root@Dasoncheng2 ~]# !p
ps aux |grep rsync
root      63183  0.0  0.2 112660   968 pts/1    S+   15:21   0:00 grep --color=auto rsyn
[root@Dasoncheng2 ~]# rsync --daemon
[root@Dasoncheng2 ~]# ps aux |grep rsync
root      63350  0.0  0.1 114656   524 ?        Ss   15:21   0:00 rsync --daemon
root      63359  0.0  0.2 112660   968 pts/1    S+   15:22   0:00 grep --color=auto rsyn
[root@Dasoncheng2 ~]# netstat -lntp |grep rsync
tcp        0      0 192.168.60.12:8730      0.0.0.0:*               LISTEN      63350/rsync  

客户端同步:

[root@Dasoncheng ~]# rsync -avP wc test@192.168.60.12::test/ddd
rsync: failed to connect to 192.168.60.12 (192.168.60.12): Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(122) [sender=3.0.9]
[root@Dasoncheng ~]# rsync -avP --port 8730 wc test@192.168.60.12::test/ddd
Password: 
sending incremental file list
wc
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 62 bytes  received 27 bytes  25.43 bytes/sec
total size is 0  speedup is 0.00

服务端验证:

[root@Dasoncheng2 ~]# ls /tmp/rsync/  ##成功;
bbb  ccc  ddd

端口总结:
ssh是:-e "ssh -p 22"
[root@Dasoncheng ~]# rsync -av -e "ssh -p 22" ./1.txt 192.168.60.12:/tmp/3.txt
服务是:--port 8730
[root@Dasoncheng ~]# rsync -avP --port 8730 wc test@192.168.60.12::test/ddd

转载于:https://my.oschina.net/u/3651233/blog/1588871

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值