Rsync同步工具

10.28 rsync工具介绍ssh

10.29/10.30 rsync经常使用选项工具

10.31 rsync经过ssh同步code

10.28 -10 34 rsync

10.28 rsync工具介绍

  • rsync是同步工具,增量更新
    • cp只能覆盖,浪费磁盘IO。使用rsync增量同步
yum install -y rsync
  • 常见用法
rsync  -av /etc/passwd /tmp/1.txt
rsync  -av /etc/passwd root@192.168.83.22:/tmp/1.txt
  • rsync格式
rsync [OPTION] … SRC   DEST 本地同步
 rsync [OPTION] … SRC   [user@]host:DEST 本地同步远程(不写用户则是当前用户,对方没有则报错)
 rsync [OPTION] … [user@]host:SRC   DEST 远程同步本地
 rsync [OPTION] … SRC   [user@]host::DEST ?
 rsync [OPTION] … [user@]host::SRC   DEST

10.29/10.30 rsync经常使用选项

  • rsync经常使用选项rem

    • -a 包含-rtplgoD
    • -r 同步目录时要加上,相似cp时的-r选项
    • -v 可视化
    • -l 保留软链接 常配和L
    • -L 加上该选项后,对软连接的同步变成对源文件的同步,可是仍然以软链接命名
    • -p 保持文件的权限属性
    • -o 保持文件的属主
    • -g 保持文件的属组
    • -D 保持设备文件信息,ms没啥卵用
    • -t 保持文件的时间属性
    • --delte 删除DEST中SRC没有的文件,保证总体目录彻底一致!
    • --exclude 排除指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不一样步,支持通配
    • -P 显示同步过程,好比速率,比-v更加详细
    • -u 加上该选项后,若是DEST中的文件比SRC新,则不一样步 你新你有理
    • -z 传输时压缩 节省带宽
  • 示例:同步

//准备实验用的aaa目录
root@axiang01 ~]# ls -l aaa
总用量 4
-rw-r--r--. 1 root root 12 7月  19 10:13 11.txt
drwxr-xr-x. 2 root root  6 7月  19 10:12 aa
lrwxrwxrwx. 1 root root 10 7月  19 10:19 softlink1 -> /tmp/1.txt

//日思一脑残rsync av是标配
[root@axiang01 ~]# rsync -av /root/aaa/ /tmp/aaa_dest/ 建议目录后加“/”
sending incremental file list
created directory /tmp/aaa_dest
./
11.txt
softlink1 -> /tmp/1.txt
aa/

sent 161 bytes  received 41 bytes  404.00 bytes/sec
total size is 22  speedup is 0.11
[root@axiang01 ~]# ls -l /tmp/aaa_dest
总用量 4
-rw-r--r--. 1 root root 12 7月  19 10:13 11.txt
drwxr-xr-x. 2 root root  6 7月  19 10:12 aa
lrwxrwxrwx. 1 root root 10 7月  19 10:19 softlink1 -> /tmp/1.txt
[root@axiang01 ~]# 

//干掉软,靠avL
[root@axiang01 ~]# rsync -avL /root/aaa/ /tmp/aaa_dest/ 
//增长L选项能够同步软连接的源文件,不过仍是以软链接命名(为了要和同步源目录下的文件命名保持一致)
sending incremental file list
softlink1

sent 1166 bytes  received 32 bytes  2396.00 bytes/sec
total size is 1053  speedup is 0.88
[root@axiang01 ~]# ls -l /tmp/aaa_dest/
总用量 8
-rw-r--r--. 1 root root   12 7月  19 10:13 11.txt
drwxr-xr-x. 2 root root    6 7月  19 10:12 aa
-rw-r--r--. 1 root root 1041 7月  18 22:32 softlink1 此处与/tmp/1.txt同步

//同步啥是啥,别整没用的 --delete
[root@axiang01 ~]# touch /tmp/aaa_dest/new.txt 人无我有~
[root@axiang01 ~]# rsync -avL /root/aaa/ /tmp/aaa_dest/ --delete 还有么?
sending incremental file list
./
deleting new.txt

//不一样步没用的 --exclude
[root@axiang01 ~]# rm -rf /tmp/aaa_dest/* 重来
[root@axiang01 ~]# rsync -avL /root/aaa/ /tmp/aaa_dest/ --exclude "*.txt" --exclude "a*" 不一样步啥啥啥
sending incremental file list
./
softlink1

sent 1135 bytes  received 34 bytes  2338.00 bytes/sec
total size is 1041  speedup is 0.89
[root@axiang01 ~]# ls /tmp/aaa_dest/
softlink1 //exclude挺好使

//看同步大文件要多久 -P选项
[root@axiang01 ~]# !rm
rm -rf /tmp/aaa_dest/*
[root@axiang01 ~]# rsync -avP /root/aaa/ /tmp/aaa_dest/ 精确显示传输速度和%
sending incremental file list
./
11.txt
          12 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/4)
softlink1 -> /tmp/1.txt
aa/

//哎呦喂,你新你有理 -auv 不一样步mtime落后的源文件
[root@axiang01 ~]# vi /tmp/aaa_dest/11.txt 
[root@axiang01 ~]# rsync -auv /root/aaa/ /tmp/aaa_dest/
sending incremental file list
./

sent 100 bytes  received 16 bytes  232.00 bytes/sec
total size is 22  speedup is 0.19
[root@axiang01 ~]# cat /tmp/aaa_dest/11.txt 
6ofjeow
afegs
efewf:66
555
444
没变

//压缩传输 -z选项
[root@axiang01 ~]# rsync -az /root/aaa/ /tmp/aaa_dest/
//文件不大看不出效果

10.31 rsync经过ssh同步

  • rsync经过ssh方式同步
    • 远程同步的默认方式,会让你输入密码
    • rsync -av [源地址:]源目录 [目标地址:]目标目录 //目录是绝对路径
    • -e "ssh -p 22"链接对方指定的远程端口

示例:登录

//推文件 没弄过密钥须要输入登陆密码
[root@axiang01 ~]# rsync -av /etc/passwd 192.168.83.22:/tmp/axiang.txt
sending incremental file list
passwd

sent 1115 bytes  received 31 bytes  99.65 bytes/sec
total size is 1041  speedup is 0.91

//拉文件
[root@axiang01 ~]# rsync -avP 192.168.83.22:/tmp/axiang.txt /tmp/aaa.txt
receiving incremental file list
axiang.txt
        1041 100% 1016.60kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 30 bytes  received 1124 bytes  769.33 bytes/sec
total size is 1041  speedup is 0.90
相关文章
相关标签/搜索