rsync

rsync工具介绍

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

语法

rsync有六种不一样的工做模式:shell

mark

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

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:过滤指定文件,不一样步

语法应用:

  • 同步本地文件
[root@3 ~]# 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@3 ~]# rsync -av /etc/passwd root@192.168.8.129:/tmp/11.txt
root@192.168.8.129'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工具才能够。ssh

  • 将本地机器的内容拷贝到远程机器,当端口不一致时:
[root@3 ~]# rsync -av -e "ssh -p 22" /etc/passwd root@192.168.8.129:/tmp/11.txt
root@192.168.8.129'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@3 ~]# rsync -av root@192.168.8.129:/etc/passwd /tmp/22.txt 
root@192.168.8.129'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

选项应用

  • rsync -avP 显示详尽的同步过程
[root@3 ~]# 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@3 ~]# 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@3 ~]# touch /tmp/grep_dest/new2.txt

[root@adai003 ~]# 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@3 ~]# 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

注: 能够同时过滤多种文件。工具

相关文章
相关标签/搜索