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

rsync工具介绍

1 rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。
2 rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法
3 只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。
4 
5 从字面上的意思你可以理解为remote sync (远程同步)这样可以让你理解的更深刻一些。
6 Rsync不仅可以远程同步数据(类似于scp [1]),当然还可以本地同步数据(类似于cp),
7 但不同于cp或scp的一点是,rsync不像cp/scp一样会覆盖以前的数据(如果数据已经存在),
8 它会先判断已经存在的数据和新数据有什么不同,只有不同时才会把不同的部分覆盖掉。
9 如果你的linux没有rsync命令请使用 yum install -y rsync 安装。
  

六种不同的模式

说明:src表示源文件,dest表示目的文件

常用选项
-a 归档模式,表示以递归方式传输文件,并保持所有属性,等同于-rlptgoD, -a选项后面可以跟一个 –no-OPTION 这个表示关闭-rlptgoD中的某一个例如 -a–no-l 等同于-rptgoD;

-r 对子目录以递归模式处理,主要是针对目录来说的,如果单独传一个文件不需要加-r,但是传输的是目录必须加-r选项;

-t 保持文件的时间属性;

-p 保持文件的权限属性;

-l 保留软链接;

-L 同步软链接的同时同步其源文件;

-g 保存文件数组;

-o 保持文件的属主;

-D 保存设备文件信息;

-v =visual,可视化;

-P 显示同步过程,比v更详细;

-u =update,加上该选项,如果DEST中文件比SRC中的新,则不同步;

-z =zip,传输时压缩;

–delete 删除DEST中SRC没有的文件;

–exclude 过滤指定文件,不同步;

–progress 在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、同步的文件传输速度等等;

-e 指定端口;

  

特性
可以镜像保存整个目录树和文件系统。

可以很容易做到保持原来文件的权限、时间、软硬链接等等。

无须特殊权限即可安装。

快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。

安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。

支持匿名传输,以方便进行网站镜象。

rsync实战


通过ssh传输方式同步本地文件(两台机器上都必须要rsync才能成功同步)

[root@dl-001 tmp]# rsync -av /etc/passwd root@192.168.111.127:/tmp/1.txt //dl-001机器
The authenticity of host '192.168.111.127 (192.168.111.127)' can't be established.
ECDSA key fingerprint is 44:ee:39:82:07:79:25:36:18:54:04:f2:2f:a6:33:fe.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.111.127' (ECDSA) to the list of known hosts.
root@192.168.111.127's password:
sending incremental file list
passwd

sent 1167 bytes received 37 bytes 17.32 bytes/sec
total size is 1090 speedup is 0.91

[root@dl-002 ~]# cat /tmp/1.txt //dl-002机器
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

  

说明:
如果同步一个有软链接的文件,可以加上-L选项,这样软链接连同源文件一起同步。
可以把远程机器上的文件同步到本地机器,同步时只需要调换位置。

使用ssh远程连接

[root@dl-001 ~]# ssh -p 22 192.168.111.127
root@192.168.111.127's password: //需要输入密码
Last login: Wed Dec 6 21:50:58 2017 from 192.168.111.128
[root@dl-002 ~]# //已经进入dl-002
[root@dl-002 ~]
# exit //退出远程
登出
Connection to 192.168.111.127 closed.


在同步的同时指定远程机器的端口号

# rsync -av -e "ssh -p 22" /etc/passwd root@192.168.111.127:/tmp/1.txt

使用–delete实现在被同步的文件中删除同步文件中没有的文件。

[root@dl-001 ~]# ls //dl-001机器上
1_heard.txt 1.txt 2.txt dd root@192.168.111.127
1_sorft.txt 222 anaconda-ks.cfg dir
[root@dl-001 ~]# ls dir
test1 test6
[root@dl-001 ~]# rsync -av --delete dir/ root@192.168.111.127:/tmp/dir
root@192.168.111.127's password:
sending incremental file list
./
deleting 567.txt
test1
test6

sent 162 bytes received 53 bytes 33.08 bytes/sec
total size is 9 speedup is 0.04

[root@dl-002 ~]# cd /tmp //dl-002机器上
[root@dl-002 tmp]# ls
1.txt dir systemd-private-4c1b8f19ef2744319ab12a31cee97108-vmtoolsd.service-S1U6Lk
[root@dl-002 tmp]# ls dir //没有同步之前
567.txt

[root@dl-002 tmp]# ls dir //同步文件之后,567.txt被删除了
test1 test6

使用–exclude实现过滤(不同步指定文件)

[root@dl-001 ~]# cd dir //dl-001机器
[root@dl-001 dir]# ls
2.txt 43443.txt test1 test6
[root@dl-001 dir]# rsync -av --exclude "*.txt" dir/ root@192.168.111.127:/d
root@192.168.111.127's password:
sending incremental file list
rsync: change_dir "/root/dir//dir" failed: No such file or directory (2)

sent 12 bytes received 12 bytes 5.33 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]

[root@dl-002 ~]# cd /d //dl-002机器
[root@dl-002 d]# ls //可见.txt结尾的文件已经过滤了。
test1 test6
**************************************************************************************
当你的才华还撑不起你的野心的时候,你就应该静下心来学习;当你的能力还驾驭不了你的目标时,就应该沉下心来,历练;梦想,不是浮躁,而是沉淀和积累,只有拼出来的美丽,没有等出来的辉煌,机会永远是留给最渴望的那个人,学会与内心深处的你对话,问问自己,想 要怎样的人生,静心学习,耐心沉淀,送给自己,共勉。
**************************************************************************************
原文地址:https://www.cnblogs.com/macoffee/p/14785852.html