且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

rsync工具介绍、rsync常用选项、rsync通过ssh同步

更新时间:2022-08-13 22:30:16

rsync工具介绍

 一个远程数据同步工具,可在本地及远程主机之间的同步。可用于测试及生产环境。

安装:

[root@test-7 ~]# yum install rsync -y

-a:表示以递归方式传输文件,并保持所有文件属性

-v:可视化输出

[root@test-7 ~]# rsync -av /etc/passwd /tmp/   #同一台主机之前的同步

sending incremental file list

passwd


sent 1239 bytes  received 31 bytes  2540.00 bytes/sec

total size is 1165  speedup is 0.92

[root@test-7 ~]# ls -la /etc/passwd

-rw-r--r-- 1 root root 1165 Oct 30 04:39 /etc/passwd

[root@test-7 ~]# ls -la /tmp/passwd

-rw-r--r-- 1 root root 1165 Oct 30 04:39 /tmp/passwd


2、本地同步到远程主机

[root@test-7 ~]# rsync -av /tmp/passwd 192.168.100.103:/tmp/2.txt

3、远程主机同步到本地

-z, --compress 对备份的文件在传输时进行压缩处理。

[root@test-7 tmp]# rsync -avz  192.168.100.103:/tmp/Python-2.7.10 /tmp/


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 传输时压缩


1、同步软连接的文件,到了目标目录后变为了真实的文件

rsync -avPL /root/111/ /tmp/111_desc/


[root@test-7 111]# ls -la /root/111

total 52

drwxr-xr-x  3 root    root       87 Oct 31 10:02 .

dr-xr-x---. 6 root    root     4096 Oct 31 10:01 ..

-rw-------. 1 root    root      997 Sep  3 15:36 anaconda-ks.cfg

-rw-r--r--  1 root    root     3109 Oct 30 07:01 my.ipt

drwxr-xr-x  2 root    root       24 Oct 30 05:34 shell

-rw-r--r--  1 tcpdump tcpdump 37804 Oct 29 23:06 test.log

lrwxrwxrwx  1 root    root        8 Oct 31 10:02 test_pro.log -> test.log


[root@test-7 111]# ls -la /tmp/111_desc/

total 92

drwxr-xr-x   3 root    root       87 Oct 31 10:02 .

drwxrwxrwt. 10 root    root     4096 Oct 31 10:01 ..

-rw-------   1 root    root      997 Sep  3 15:36 anaconda-ks.cfg

-rw-r--r--   1 root    root     3109 Oct 30 07:01 my.ipt

drwxr-xr-x   2 root    root       24 Oct 30 05:34 shell

-rw-r--r--   1 tcpdump tcpdump 37804 Oct 29 23:06 test.log

-rw-r--r--   1 tcpdump tcpdump 37804 Oct 29 23:06 test_pro.log #变为test.log


2、在目标文件中新增目录文件

[root@test-7 111_desc]# mkdir access tomcat java

[root@test-7 111_desc]# touch linux.log


3、--delete删除目标目录中的文件(在源目录中没有的文件及文件夹)

[root@test-7 ~]# rsync -avPL --delete /root/111/ /tmp/111_desc/

sending incremental file list

./

deleting tomcat/

deleting java/

deleting access/

deleting linux.log


sent 184 bytes  received 16 bytes  400.00 bytes/sec

total size is 80034  speedup is 400.17


4、--exlude 排除docker文件夹,不让其传输

[root@test-7 111]# rsync -avPL --exclude docker /root/111/ /tmp/111_desc/

5、-u:表示保留了目标文件中最新的文件(对比源目录来说)

[root@test-7 111_desc]# rsync -avPLu /root/111 /tmp/111_desc/

6、-z:表示压缩


rsync通过ssh同步

-e 'ssh -p 22' :当ssh不是22端口的时候选择这个选项

[root@test-7 111_desc]# rsync -avzP -e 'ssh -p 22' /tmp/passwd root@192.168.100.103:/tmp/




本文转自 jiekegz  51CTO博客,原文链接:http://blog.51cto.com/jacksoner/1979121