10.29/10.30 rsync经常使用选项

Linux文件同步工具-rsync

  • rsync经常使用选项
    • -a 包含-rtplgoD参数选项
    • -r 同步目录时要加上,相似cp时的-r选项
    • -v 同步时显示一些信息,让咱们知道同步的过程
    • -l 保留软链接
      • 如果拷贝的原目录里面有一个软连接文件,那这个软连接文件指向到了另一个目录下
      • 在加上-l,它会把软连接文件自己拷贝到目标目录里面去
    • -L 加上该选项后,同步软连接时会把源文件给同步
    • -p 保持文件的权限属性
    • -o 保持文件的属主
    • -g 保持文件的属组
    • -D 保持设备文件信息
      • /dev/sdb1 这样的设备文件有它的特殊性,若是不加-D 可能拷贝过去就是一个很是普通的文件,不能当设备来用
    • -t 保持文件的时间属性
    • --delete 删除DEST中SRC没有的文件
    • --exclude 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不一样步
    • -P 显示同步过程,好比速率,比-v更加详细
    • -u 加上该选项后,若是DEST中的文件比SRC新,则不一样步
      • update
    • -z 传输时压缩

rsync命令,同步目录

  • rsync -av /root/111 /tmp/111_dest //同步一个目录
    • 在同步目录的时候,在目录的最后面加一个斜杠/
[root@hf-01 ~]# ls 111
a.txt  haha  hanfeng
[root@hf-01 ~]# ls /tmp/
[root@hf-01 ~]# rsync -av /root/111/ /tmp/111_dest/    //同步一个目录
sending incremental file list
created directory /tmp/111_dest
./
a.txt
haha/
hanfeng/

sent 256312 bytes  received 42 bytes  512708.00 bytes/sec
total size is 256144  speedup is 1.00
[root@hf-01 ~]#
  • 在加入参数 -L 后,会把参数 -l 的含义给覆盖掉
    • -L会把软连接所指向的源文件给拷贝过去
[root@hf-01 ~]# rsync -avL /root/111/ /tmp/111_dest/
sending incremental file list

sent 88 bytes  received 14 bytes  204.00 bytes/sec
total size is 256144  speedup is 2511.22
[root@hf-01 ~]#

rsync命令,删除目标中源文件中没有的内容

  • rsync -avL --delete /root/111/ /tmp/111_dest/
    • --delete会把多余的文件除去
[root@hf-01 ~]# ls 111/
a.txt  haha  hanfeng
[root@hf-01 ~]# ls /tmp/111_dest/
a.txt  haha  hanfeng
[root@hf-01 ~]# touch /tmp/111_dest/new.txt
[root@hf-01 ~]# rsync -avL --delete /root/111/ /tmp/111_dest/
sending incremental file list
./
deleting new.txt

sent 91 bytes  received 17 bytes  216.00 bytes/sec
total size is 256144  speedup is 2371.70
[root@hf-01 ~]# ls /tmp/111_dest/
a.txt  haha  hanfeng
[root@hf-01 ~]#

rsync命令,过滤全部txt文件

[root@hf-01 ~]# rsync -avL --exclude "*.txt" /root/111/ /tmp/111_dest/
sending incremental file list
./
2.txt.swp
4913
haha/
hanfeng/

sent 184 bytes  received 61 bytes  490.00 bytes/sec
total size is 0  speedup is 0.00
[root@hf-01 ~]#
  • 可屡次过滤文件
[root@hf-01 ~]# !rm
rm -rf /tmp/111_dest/*
[root@hf-01 ~]# rsync -avL --exclude "*.txt" --exclude="2*" /root/111/ /tmp/111_dest/
sending incremental file list
./
4913
haha/
hanfeng/

sent 131 bytes  received 42 bytes  346.00 bytes/sec
total size is 0  speedup is 0.00
[root@hf-01 ~]#
  • 在添加文件后,再次同步,会只同步里面不相同的文件,而相同的文件则不会再次同步
[root@hf-01 ~]# cd 111
[root@hf-01 111]# touch 6.dest 123
[root@hf-01 111]# rsync -avL --exclude "*.txt" --exclude="2*" /root/111/ /tmp/111_dest/
sending incremental file list
./
123
6.dest

sent 187 bytes  received 55 bytes  484.00 bytes/sec
total size is 0  speedup is 0.00
[root@hf-01 111]#

rsync命令,参数-P

  • rsync -avP /root/111/ /tmp/111_dest/
    • 在传输过程当中,会告诉你传输了多少,传输的速度是多少
[root@hf-01 ~]# !rm
rm -rf /tmp/111_dest/*
[root@hf-01 ~]# rsync -avP /root/111/ /tmp/111_dest/
sending incremental file list
./
123
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=6/8)
2.txt.swp
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=5/8)
4913
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=4/8)
6.dest
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=3/8)
a.txt
      256144 100%   53.26MB/s    0:00:00 (xfer#5, to-check=2/8)
haha/
hanfeng/

sent 256522 bytes  received 118 bytes  513280.00 bytes/sec
total size is 256144  speedup is 1.00
[root@hf-01 ~]#

rsync命令,参数-u

[root@hf-01 ~]# cd /tmp/111_dest/
[root@hf-01 111_dest]# ls
123  2.txt.swp  4913  6.dest  a.txt  haha  hanfeng
[root@hf-01 111_dest]# vim 4913
在4913中添加内容

[root@hf-01 111_dest]# rsync -avPu /root/111/ /tmp/111_dest/
sending incremental file list
./

sent 145 bytes  received 17 bytes  324.00 bytes/sec
total size is 256144  speedup is 1581.14
[root@hf-01 111_dest]# cat 4913
dsgsdfascs
dsafszcdrw
etfbcgrhc
cbcvbtyegvdgdh
gxdgdfhch
[root@hf-01 111_dest]# cat /root/111/4913
[root@hf-01 111_dest]#

rsync命令,参数-z

  • 在远程传输不少文件的时候,加上-z 参数,能够节省带宽,增长速度的
[root@hf-01 111_dest]# rsync -avPz /root/111/ /tmp/111_dest/
sending incremental file list
4913
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=4/8)

sent 178 bytes  received 33 bytes  422.00 bytes/sec
total size is 256144  speedup is 1213.95
[root@hf-01 111_dest]#
相关文章
相关标签/搜索