记录一下rsync命令的使用。
rsync意为remote sync,即远程同步的意思,它可以同步本地或远程机器上的文件或目录,其突出的特点为“增量同步”,这意味着它把变化的内容从源头同步到目的。
rsync语法:
同步方式 | 语法 |
---|---|
本地同步 | rsync {options} {source} {destination} |
将远程内容同步至本地 | rsync {options} {User_name>@{Remote-Host}:{Source-File-Dir} {destination} |
将本地内容同步至远程 | rsync {options} {source-file-dir} {User_name}@{Remote-Host}:{destination} |
rsync后跟的一些常用选项
选项 | 含义 |
---|---|
-v, -verbose | 输出详细的信息 |
-q, -quiet | 抑制消息输出 |
-r, -recursive | 以递归的形式同步文件或目录 |
-z, -compress | 传输过程中压缩数据 |
rsync使用实例
1)、在本机同步文件
将当前目录下的a目录中的所有内容同步到当前目录的b目录下,使用如下的命令即可:
rsync -zvh a/* b/
a.txt
sent 68 bytes received 31 bytes 198.00 bytes/sec
total size is 2 speedup is 0.02
2)、本机递归同步
在递归同步时,可以使用-a或-r参数来实现,其中-a还能保留文件的元信息(修改时间、权限),因此命令如下:
[root@hadoop102 ~]# rsync -zavh a/ainner b
sending incremental file list
ainner/
ainner/a.txt
sent 127 bytes received 39 bytes 332.00 bytes/sec
total size is 5 speedup is 0.03
或者
[root@hadoop102 ~]# rsync -zrvh a/ainner b
sending incremental file list
ainner/
ainner/a.txt
sent 125 bytes received 39 bytes 328.00 bytes/sec
total size is 5 speedup is 0.03
3)、将文件或目录从本地同步到远程机器
[root@hadoop102 ~]# rsync -zavh a/ainner root@192.168.1.17:/root/b
The authenticity of host '192.168.1.17 (192.168.1.17)' can't be established.
ECDSA key fingerprint is SHA256:19DdNuhGPyPdcLx8za4M/g+cZ6SFrSk6WHpAuA5S8/0.
ECDSA key fingerprint is MD5:c9:c3:ee:2d:80:01:2d:d3:90:20:e4:31:af:22:20:d9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.17' (ECDSA) to the list of known hosts.
root@192.168.1.17's password:
sending incremental file list
ainner/
ainner/a.txt
sent 127 bytes received 39 bytes 30.18 bytes/sec
total size is 5 speedup is 0.03
4)、将文件或目录从远程同步到本机
[root@hadoop102 ~]# rsync -zavh root@192.168.1.17:/root/b b/
root@192.168.1.17's password:
receiving incremental file list
b/
b/ainner/
b/ainner/a.txt
sent 55 bytes received 152 bytes 37.64 bytes/sec
total size is 5 speedup is 0.02