rsync同步
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rsync同步相关的知识,希望对你有一定的参考价值。
rsync常用选项-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 传输时压缩
安装rsync
[[email protected] ~]# yum install -y rsync
本机文件同步
[[email protected] ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd
sent 1759 bytes received 31 bytes 3580.00 bytes/sec
total size is 1685 speedup is 0.94
从本机同步到远程 报错远程机器上没有安装rsync
[[email protected] ~]# rsync -av /etc/passwd [email protected]:/tmp/1.txt
The authenticity of host ‘192.168.130.128 (192.168.130.128)‘ can‘t be established.
ECDSA key fingerprint is SHA256:3WEYnU1l7WjaDAQHkhPIcM9Mvh7wYYFkKjG0+C0BriY.
ECDSA key fingerprint is MD5:22:a0:fb:e7:8c:44:98:c4:df:5b:64:4d:58:db:94:75.
Are you sure you want to continue connecting (yes/no)? y
Please type ‘yes‘ or ‘no‘: yes
Warning: Permanently added ‘192.168.130.128‘ (ECDSA) to the list of known hosts.
[email protected]‘s password:
bash: rsync: command not found
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: remote command not found (code 127) at io.c(605) [sender=3.0.9]
给远程机器安装rsync
[[email protected] ~]# yum install -y rsync
再次同步
[[email protected] ~]# rsync -av /etc/passwd [email protected]:/tmp/1.txt
[email protected]‘s password:
sending incremental file list
passwd
sent 1759 bytes received 31 bytes 275.38 bytes/sec
total size is 1685 speedup is 0.94
[[email protected] 111]# rsync -av /root/111/ /tmp/111_test/
sending incremental file list
created directory /tmp/111_test
./
12.txt
12_txt.swp
332 -> /tmp/333-ln.txt
333
333.txt
4913
222/
sent 365 bytes received 117 bytes 964.00 bytes/sec
total size is 15 speedup is 0.03
[[email protected] 111]# ls -l /tmp/111_test/
total 0
-rw-r--r-- 1 root root 0 Apr 17 19:17 12.txt
-rw-r--r-- 1 root root 0 Apr 17 19:17 12_txt.swp
drwxr-xr-x 2 root root 6 Apr 17 19:18 222
lrwxrwxrwx 1 root root 15 Apr 17 19:31 332 -> /tmp/333-ln.txt
-rw-r--r-- 1 root root 0 Apr 17 19:23 333
-rw-r--r-- 1 root root 0 Apr 17 19:27 333.txt
-rw-r--r-- 1 root root 0 Apr 17 19:18 4913
-L 加上该选项后,同步软链接时会把源文件给同步
[[email protected] 111]# rsync -avL /root/111/ /tmp/111_test/
sending incremental file list
332
sent 180 bytes received 32 bytes 424.00 bytes/sec
total size is 0 speedup is 0.00
[[email protected] 111]# ls -l /tmp/111_test/
total 0
-rw-r--r-- 1 root root 0 Apr 17 19:17 12.txt
-rw-r--r-- 1 root root 0 Apr 17 19:17 12_txt.swp
drwxr-xr-x 2 root root 6 Apr 17 19:18 222
-rw-r--r-- 1 root root 0 Apr 17 19:28 332
-rw-r--r-- 1 root root 0 Apr 17 19:23 333
-rw-r--r-- 1 root root 0 Apr 17 19:27 333.txt
-rw-r--r-- 1 root root 0 Apr 17 19:18 4913
--delete 删除DEST中SRC没有的文件
[[email protected] ~]# ls 111
12.txt 12_txt.swp 222 332 333 333.txt 4913
[[email protected] ~]# ls /tmp/111_test
12.txt 12_txt.swp 222 332 333 333.txt 4913
[[email protected] ~]# touch /tmp/111_test/hahaha.txt
[[email protected] ~]# ls /tmp/111_test
12.txt 12_txt.swp 222 332 333 333.txt 4913 hahaha.txt
[[email protected] ~]# rsync -avL --delete /root/111/ /tmp/111_test/
sending incremental file list
./
deleting hahaha.txt
sent 144 bytes received 16 bytes 320.00 bytes/sec
total size is 0 speedup is 0.00
[[email protected] ~]# ls /tmp/111_test
12.txt 12_txt.swp 222 332 333 333.txt 4913
--exclude 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步
[[email protected] ~]# ls 111/
12.txt 12_txt.swp 222 332 333 333.txt 4913
[[email protected] ~]# ls /tmp/111_test/
[[email protected] ~]# rsync -avL --exclude "*.txt" /root/111/ /tmp/111_test/
sending incremental file list
./
12_txt.swp
332
333
4913
222/
sent 275 bytes received 95 bytes 740.00 bytes/sec
total size is 0 speedup is 0.00
[[email protected] ~]# ls /tmp/111_test/
12_txt.swp 222 332 333 4913
-P 显示同步过程,比如速率,比-v更加详细
[[email protected] ~]# rsync -aP /root/111/ /tmp/111_test/
sending incremental file list
./
12.txt
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=6/8)
12_txt.swp
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=5/8)
332 -> /tmp/333-ln.txt
333
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=3/8)
333.txt
0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=2/8)
4913
0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=1/8)
222/
sent 365 bytes received 117 bytes 964.00 bytes/sec
total size is 15 speedup is 0.03
指定端口链接同步
[[email protected] ~]# rsync -aP -e "ssh -p 22" 192.168.130.128:/tmp/666.txt /tmp/111.txt
[email protected]‘s password:
receiving incremental file list
666.txt
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
sent 30 bytes received 76 bytes 23.56 bytes/sec
total size is 0 speedup is 0.00
以上是关于rsync同步的主要内容,如果未能解决你的问题,请参考以下文章