例子
• rsync -av /etc/passwd /tmp/1.txt
• rsync -av /tmp/1.txt 192.168.188.128:/tmp/2.txtlinux
rsync格式
• rsync [OPTION] … SRC DEST
• rsync [OPTION] … SRC [user@]host:DEST
• rsync [OPTION] … [user@]host:SRC DEST
• rsync [OPTION] … SRC [user@]host::DEST
• rsync [OPTION] … [user@]host::SRC DESTvim
• -a 包含-rtplgoD • -r 同步目录时要加上,相似cp时的-r选项 • -v 同步时显示一些信息,让咱们知道同步的过程 • -l 保留软链接 • -L 加上该选项后,同步软连接时会把源文件给同步 • -p 保持文件的权限属性 • -o 保持文件的属主 • -g 保持文件的属组 • -D 保持设备文件信息 • -t 保持文件的时间属性 • --delete 删除DEST中SRC没有的文件 • --exclude 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不一样步 • -P 显示同步过程,好比速率,比-v更加详细 • -u 加上该选项后,若是DEST中的文件比SRC新,则不一样步 • -z 传输时压缩 • -e 指定端口安全
rsync = remote(远程) sync(同步)
使用以前须要安装rsync包bash
yum install -y rsync [root@linux-128 ~]# yum install -y rsync
需求:我要将A目录下的文件拷贝到B目录下(本机操做) 通常咱们想到的是使用命令cp来拷贝,可是另一个需求是:每一小时拷贝一次,若是文件比较大或者多,认为去操做就比较繁琐了,因此咱们使用rsync命令来作 例子:服务器
[root@linux-128 tmp]# rsync -av /etc/passwd /tmp/rsync.txt sending incremental file list passwd sent 1424 bytes received 31 bytes 2910.00 bytes/sec total size is 1350 speedup is 0.93
需求2:将A机器/tmp/rsync.txt文件拷贝到B机器192.168.88.129/tmp/目录下ssh
拷贝的时候发下错误,是由于B机器上没有安装rsync,因此须要先安装一下socket
[root@linux-128 tmp]# rsync -av /tmp/rsync.txt root@192.168.88.129:/tmp/ sending incremental file list rsync.txt sent 1427 bytes received 31 bytes 2916.00 bytes/sec total size is 1350 speedup is 0.93
备注:这里命令也能够写成
rsync -av /tmp/rsync.txt 192.168.88.129:/tmp/ \若是B机器和A机器的当前用户同样,就能够不加root@,若是A机器当前用户是user1,你命令里面没有加B机器的用户名@,它就会报错
咱们在B机器/tmp/目录下就能够看到了ui
[root@linux-129 ~]# ls /tmp/ rsync.txt systemd-private-441df47e85a6457486521b5a6973cce1-vmtoolsd.service-b8vozP
• rsync -av test1/ 192.168.133.132:/tmp/test2/ \A-B
• rsync -av 192.168.133.132:/tmp/test2/ /root/test1 \B-A命令行
若是对方机器sshd端口不是22呢,因此咱们就要指定对方机器的shhd端口了
• rsync -av -e "ssh -p 1122" test1/ 192.168.133.132:/tmp/test2/日志
• 要编辑配置文件/etc/rsyncd.conf • 启动服务rsync --daemon • 格式:rsync -av test1/ 192.168.133.130::module/dir/ 若是要用服务的方式同步,须要先启动rsync服务,而后编辑配置文件 vim /etc/rsyncd.conf 这里有个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
[root@linux-129 ~]# rsync -av /tmp/1.txt 192.168.88.129::test/test.txt rsync: failed to connect to 192.168.88.129 (192.168.88.129): Connection refused (111) rsync error: error in socket IO (code 10) at clientserver.c(122) [sender=3.0.9]
这里报错了:没有路由到远程=机器上去
[root@linux-129 ~]# ping 192.168.88.128 \\先 查看是IP是否通畅 PING 192.168.88.128 (192.168.88.128) 56(84) bytes of data. 64 bytes from 192.168.88.128: icmp_seq=1 ttl=64 time=2.66 ms 64 bytes from 192.168.88.128: icmp_seq=2 ttl=64 time=0.927 ms
[root@linux-129 ~]# telnet 192.168.88.129 837 \\用命令telnet来查看端口是否通的 -bash: telnet: 未找到命令 [root@linux-129 ~]# yum install -y telnet [root@linux-129 ~]# telnet 192.168.88.128 837 Trying 192.168.88.128... telnet: connect to address 192.168.88.128: No route to host \\说明端口不通
遇到这种问题咱们要检测一下是否是iptables的问题
[root@linux-129 ~]# systemctl stop firewalld \\关闭将服务端和客户端的防火墙先关闭 [root@linux-129 ~]# telnet 192.168.88.128 873 Trying 192.168.88.128... Connected to 192.168.88.128. \\这就表示通了 Escape character is '^]'. @RSYNCD: 30.0 ^] \\按ctrl+] 而后输入quit退出 telnet> quit Connection closed.
[root@linux-129 ~]# rsync -avP /tmp/1.txt 192.168.88.128::test/test.txt Password: @ERROR: auth failed on module test rsync error: error starting client-server protocol (code 5) at main.c(1516) [sender=3.0.9]
这里有报错了,提示密码错误;由于咱们在修改rsync配置文件的时候,有定义密码文件,咱们将它先注释掉。
[root@linux-129 ~]# rsync -avP /tmp/1.txt 192.168.88.128::test/test.txt sending incremental file list 1.txt 1372 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) sent 1441 bytes received 27 bytes 978.67 bytes/sec total size is 1372 speedup is 0.93
这是从客户端同步数据到客户端
咱们在从服务端将数据同步回来
[root@linux-129 ~]# rsync -avP 192.168.88.128::test/test.txt /tmp/test.txt receiving incremental file list test.txt 1372 100% 1.31MB/s 0:00:00 (xfer#1, to-check=0/1) sent 45 bytes received 1475 bytes 3040.00 bytes/sec total size is 1372 speedup is 0.90
若是服务端rsync的端口是8730,客户端是没有开放这个端口,因此咱们要指定端口 --port
[root@linux-129 ~]# rsync -avP --port 8730 192.168.88.128::test/test.txt /tmp/test.txt receiving incremental file list sent 26 bytes received 60 bytes 172.00 bytes/sec total size is 1372 speedup is 15.95
ssh服务指定端口是--e "ssh -p 1122"
• port:指定在哪一个端口启动rsyncd服务,默认是873端口。
• log file:指定日志文件。
• pid file:指定pid文件,这个文件的做用涉及服务的启动、中止等进程管理操做。
• address:指定启动rsyncd服务的IP。假如你的机器有多个IP,就能够指定由其中一个启动rsyncd服务,若是不指定该参数,默认是在所有IP上启动。
• []:指定模块名,里面内容自定义。
• path:指定同步数据存放的路径。
• use chroot true|false:表示在传输文件前首先chroot到path参数所指定的目录下。这样作的缘由是实现额外的安全防御,但缺点是须要以roots权限,而且不能备份指向外部的符号链接所指向的目录文件。好比你要同步的文件里面有软链接,软链接的源地址在其余目录下,若是use chroot的值是true,那么你在同步的时候加上-L 想要将软链接的源文件同步过去的时候,它是会报错的同步失败,不加-L,它能同步成功,只是同步过的软连接是失效的。默认状况下chroot值为true,若是你的数据当中有软链接文件,阿铭建议你设置成false。 • max connections:指定最大的链接数,默认是0,即没有限制。若是有不少客户端都在重服务器上同步数据,给服务器带来压力,咱们就能够设置这个最大链接数。
• read only ture|false:read只读,若是为true,则不能上传到该模块指定的路径下。
• list:表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,false则隐藏。这里可用模块就是咱们定义的[test];咱们在::后面不加内容,它就会列出模块来,别人就知道这个模块,他会上传一些木马窃取信息,因此为了安全,设置成false
• uid/gid:指定传输文件时以哪一个用户/组的身份传输。
• auth users:指定传输时要使用的用户名。
• secrets file:指定密码文件,该参数连同上面的参数若是不指定,则不使用密码验证。注意该密码文件的权限必定要是600。格式:用户名:密码
• hosts allow:表示被容许链接该模块的主机,能够是IP或者网段,若是是多个,中间用空格隔开。192.168.133.132 1.1.1.1 2.2.2.2 192.168.133.0/24
• 当设置了auth users和secrets file后,咱们须要在服务端的/etc/rsyncd.passwd 文件写入用户名和密码格式(test:123123)客户端连服务端也须要用用户名密码了,
[root@linux-128 ~]# vim /etc/rsyncd.passwd [root@linux-128 ~]# chmod 600 !$ \\设置权限,必定要设置权限 chmod 600 /etc/rsyncd.passwd [root@linux-129 ~]# rsync -avP --port 8730 test@192.168.88.128::test/test.txt /tmp/test.txt Password: receiving incremental file list sent 54 bytes received 101 bytes 34.44 bytes/sec total size is 1372 speedup is 8.85
若不想在命令行中带上密码,能够在客户端上建立一个一个密码文件/etc/pass,这个密码文件格式和服务端设置的密码文件格式不同,它只须要写入和服务端密码同样就行(123123)。
[root@linux-129 ~]# vim /tmp/pass [root@linux-129 ~]# chmod 600 !$ \\设置权限,必定要设置权限 chmod 600 /tmp/pass [root@linux-129 ~]# rsync -avP --port 8730 --password-file=/tmp/pass test@192.168.88.128::test/test.txt /tmp/test.txt receiving incremental file list sent 54 bytes received 101 bytes 310.00 bytes/sec total size is 1372 speedup is 8.85
• rsync -avL test@192.168.133.130::test/test1/ /tmp/test8/ --password-file=/etc/pass
• 其中/etc/pass内容就是一个密码,权限要改成600
总结:rsync远程同步分为经过ssh同步,和服务的方式同步 ssh方式同步:一个冒号 服务方式同步:: 2个冒号