rsync工具

10.28 rsync工具介绍

rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。

语法

rsync有六种不同的工作模式:

注: src表示源文件,dest表示目的文件

10.29 Options

  • -a:包含-rtplgoD
    • -r:同步目录时加上表示对子目录进行递归处理
    • -t:保持文件的时间属性
    • -p:保持文件的权限属性
    • -l:保留软链接
    • -g:保存文件数组
    • -o:保持文件的属主
    • -D:保存设备文件信息
  • -v:=visual,可视化
  • -L:同步软链接的同时同步其源文件
  • -P:显示同步过程,比v更详细
  • -u:=update,加上该选项,如果DEST中文件比SRC中的新,则不同步
  • -z:=zip,传输时压缩
  • --delete:删除DEST中SRC没有的文件
  • --exclude:过滤指定文件,不同步

10.30 语法应用:

  • 同步本地文件
[root@1 ~]# rsync -av /etc/passwd /tmp/11.txt
sending incremental file list
passwd

sent 1222 bytes  received 31 bytes  2506.00 bytes/sec
total size is 1148  speedup is 0.92
  • 将本地机器的内容拷贝到远程机器
[root@1 ~]# rsync -av /etc/passwd [email protected]:/tmp/11.txt
[email protected]'s password: 
sending incremental file list
passwd

sent 1222 bytes  received 31 bytes  358.00 bytes/sec
total size is 1148  speedup is 0.92

注: 进行远程同步的时候,远程机和本地机必须都安装有rsync工具才可以。

  • 将本地机器的内容拷贝到远程机器,当端口不一致时:
[root@1 ~]# rsync -av -e "ssh -p 22" /etc/passwd [email protected]:/tmp/11.txt
[email protected]'s password: 
sending incremental file list

sent 31 bytes  received 12 bytes  7.82 bytes/sec
total size is 1148  speedup is 26.70
  • 将远程机器的内容拷贝到本地机器。
[root@1 ~]# rsync -av [email protected]:/etc/passwd /tmp/22.txt 
[email protected]'s password: 
receiving incremental file list
passwd

sent 42 bytes  received 83 bytes  27.78 bytes/sec
total size is 1149  speedup is 9.19

10.31选项应用

  • rsync -avP 显示详尽的同步过程
[root@1 ~]# rsync -avP /root/grep/ /tmp/grep_dest/
sending incremental file list
created directory /tmp/grep_dest
./
717.sh
          65 100%    0.00kB/s    0:00:00 (xfer#1, to-check=20/22)
aaa.sed.shell
          47 100%    3.53kB/s    0:00:00 (xfer#2, to-check=19/22)
…………
  • rsync -avL 同步软链接的同时同步其源文件
[root@1 ~]# rsync -avL /root/grep/ /tmp/grep_dest/
sending incremental file list
adailink

sent 1530 bytes  received 32 bytes  3124.00 bytes/sec
total size is 14275  speedup is 9.14
  • rsync -av --delete 删除目标文件中和源文件中不一样的文件
[root@1 ~]# touch /tmp/grep_dest/new2.txt

[root@1 ~]# rsync -av --delete /root/grep/ /tmp/grep_dest/
sending incremental file list
./
deleting new2.txt  ##删除文件new2.txt
adailink -> /root/awk/test.txt

sent 405 bytes  received 19 bytes  848.00 bytes/sec
total size is 13178  speedup is 31.08
  • rsync -av --exclude 传输时过滤掉指定文件
[root@1 ~]# rsync -av --exclude "*.txt" --exclude "*.shell" /root/grep/ /tmp/grep_dest/
sending incremental file list
./
717.sh
adailink -> /root/awk/test.txt
boot.log.bak

注: 可以同时过滤多种文件。

猜你喜欢

转载自my.oschina.net/u/3791387/blog/1812399