同步工具-rsync

1.安装软件包rsync:

[root@weix-01 ~]# yum install -y rsync
已加载插件:fastestmirror
base                                                     | 3.6 kB     00:00
epel/x86_64/metalink                                     | 4.8 kB     00:00
epel                                                     | 4.7 kB     00:00
extras                                                   | 3.4 kB     00:00
updates                                                  | 3.4 kB     00:00
(1/4): epel/x86_64/group_gz                                | 266 kB   00:07
(2/4): epel/x86_64/updateinfo                              | 878 kB   00:17
(3/4): updates/7/x86_64/primary_db                         | 6.0 MB   00:01
(4/4): epel/x86_64/primary_db                              | 6.2 MB   00:04
Determining fastest mirrors
 * epel: mirrors.tongji.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 rsync.x86_64.0.3.0.9-18.el7 将被 安装
--> 解决依赖关系完成

2.本机拷贝文件:

[root@weix-01 ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd

sent 1018 bytes  received 31 bytes  2098.00 bytes/sec
total size is 944  speedup is 0.90

3.远程同步: 用户名@IP:/路径

[root@weix-01 ~]# rsync -av /etc/passwd  root@192.168.127.130:/tmp/1.txt
The authenticity of host '192.168.127.130 (192.168.127.130)' can't be established.
ECDSA key fingerprint is SHA256:f30H1iKMhhJPRvENYRAuaLDL6WQt0+9O3PjMBiRr5YI.
ECDSA key fingerprint is MD5:ed:55:37:42:78:da:da:92:43:69:cd:89:44:63:ac:5a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.127.130' (ECDSA) to the list of known hosts.
root@192.168.127.130's password:
sending incremental file list

sent 31 bytes  received 12 bytes  1.62 bytes/sec
total size is 944  speedup is 21.95

4.sync格式:option 选项 SRC源目录 DEST 目标目录

sync  [option]    SRC    DEST
sync  [option]    SRC    [user@]host:DEST
sync  [option]    [user@]host:SRC    DEST
sync  [option]    SRC    [user@]host::DEST
sync  [option]    [user@]host::SRC    DEST

rsync常用选项

1.-av选项:

[root@weix-01 ~]# rsync -av /root/111/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_dest
./
12.txt
weix02 -> /tmp/weix01/
222/

sent 218 bytes  received 41 bytes  518.00 bytes/sec
total size is 77  speedup is 0.30
[root@weix-01 ~]# ls -l 111/
总用量 4
-rw-r--r--. 1 root root 65 12月 20 16:59 12.txt
drwxr-xr-x. 2 root root  6 12月 20 16:53 222
lrwxrwxrwx. 1 root root 12 12月 21 16:34 weix02 -> /tmp/weix01/
[root@weix-01 ~]# ls -l /tmp/111_dest/
总用量 4
-rw-r--r--. 1 root root 65 12月 20 16:59 12.txt
drwxr-xr-x. 2 root root  6 12月 20 16:53 222
lrwxrwxrwx. 1 root root 12 12月 21 16:34 weix02 -> /tmp/weix01/

2.-L选项:把软链接所指向的源文件拷贝过去

[root@weix-01 ~]# ls -l 111/
总用量 4
-rw-r--r--. 1 root root 65 12月 20 16:59 12.txt
drwxr-xr-x. 2 root root  6 12月 20 16:53 222
lrwxrwxrwx. 1 root root 12 12月 21 16:34 weix02 -> /tmp/weix01/
[root@weix-01 ~]# rsync -avL /root/111/ /tmp/111_dest/
sending incremental file list
weix02/

sent 95 bytes  received 17 bytes  224.00 bytes/sec
total size is 65  speedup is 0.58
[root@weix-01 ~]# ls -l /tmp/111_dest/
总用量 4
-rw-r--r--. 1 root root 65 12月 20 16:59 12.txt
drwxr-xr-x. 2 root root  6 12月 20 16:53 222
drwxr-xr-x. 2 root root  6 1月  30 16:50 weix02

3.--delte,删除目标中源没有的文件:

[root@weix-01 ~]# touch /tmp/111_dest/new.txt
[root@weix-01 ~]# ls -l /tmp/111_dest/
总用量 4
-rw-r--r--. 1 root root 65 12月 20 16:59 12.txt
drwxr-xr-x. 2 root root  6 12月 20 16:53 222
-rw-r--r--. 1 root root  0 1月  30 16:53 new.txt
drwxr-xr-x. 2 root root  6 1月  30 16:50 weix02
[root@weix-01 ~]# ls -l 111
总用量 4
-rw-r--r--. 1 root root 65 12月 20 16:59 12.txt
drwxr-xr-x. 2 root root  6 12月 20 16:53 222
lrwxrwxrwx. 1 root root 12 12月 21 16:34 weix02 -> /tmp/weix01/

[root@weix-01 ~]# rsync -avL --delete /root/111/ /tmp/111_dest/
sending incremental file list
./
deleting new.txt

sent 95 bytes  received 17 bytes  224.00 bytes/sec
total size is 65  speedup is 0.58

4.--exclude 过滤掉一些文件:

[root@weix-01 ~]# rsync -avL --exclude "*.txt" /root/111/ /tmp/111_dest/
sending incremental file list
./
222/
weix02/

sent 78 bytes  received 23 bytes  202.00 bytes/sec
total size is 0  speedup is 0.00
[root@weix-01 ~]# ls -l 111
总用量 4
-rw-r--r--. 1 root root 65 12月 20 16:59 12.txt
drwxr-xr-x. 2 root root  6 12月 20 16:53 222
lrwxrwxrwx. 1 root root 12 12月 21 16:34 weix02 -> /tmp/weix01/
[root@weix-01 ~]# ls -l /tmp/111_dest/
总用量 0
drwxr-xr-x. 2 root root 6 12月 20 16:53 222
drwxr-xr-x. 2 root root 6 1月  30 16:50 weix02

5.P:详细显示传输过程:

[root@weix-01 ~]# rsync -avP /root/111/ /tmp/111_dest/
sending incremental file list
./
12.txt
          65 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/4)
weix02 -> /tmp/weix01/
222/

sent 218 bytes  received 41 bytes  518.00 bytes/sec
total size is 77  speedup is 0.30

6.-u 保护目标文件中更改的文件

[root@weix-01 ~]# rsync -avPu /root/111/ /tmp/111_dest/
sending incremental file list
./

sent 104 bytes  received 16 bytes  240.00 bytes/sec
total size is 77  speedup is 0.64
[root@weix-01 ~]# cat /tmp/111_dest/12.txt
root:x:0:0:root:/root:/bin/bash
dfjakjgkjhalkjg
skjgkljakljg
b
in:x:1:1:bin:/bin:/sbin/nologin
[root@weix-01 ~]# cat /111/12.txt
cat: /111/12.txt: 没有那个文件或目录
[root@weix-01 ~]# cat /root/111/12.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

  1. -z打包传输,节省带宽

rsync通过ssh同步

1.-e "ssh -p 22"指定端口号