使用rsync快速同步远程文件目录

0x00 场景

有个项目代码有几十G,只能在远程服务器编译,打算拖到本地look look,怎么才能同步过来呢?

-------------2019.1.18 下午14:54 更新-------------
我擦-擦-擦,场景信息不全,可能会导致误解。

提速的前提,是本地也有一些这个项目的老代码,rsync会先diff再能传输。

如果是本地是空目录,不会diff的。

0x01 使用rsync

命令如下:

rsync -arvP --delete counsellor@192.168.1.100:/path/to/big_project .

选项含义:

        -v, --verbose               increase verbosity(显示中间信息)
            --info=FLAGS            fine-grained informational verbosity
            --debug=FLAGS           fine-grained debug verbosity
            --msgs2stderr           special output handling for debugging
        -q, --quiet                 suppress non-error messages
            --no-motd               suppress daemon-mode MOTD (see caveat)
        -c, --checksum              skip based on checksum, not mod-time & size (跳过基于checksum,修改时间和大小的对比)
        -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X) (文件模式,一堆选项的组合)
            --no-OPTION             turn off an implied OPTION (e.g. --no-D)
        -r, --recursive             recurse into directories (遍历子目录)
        -D                          same as --devices --specials 
        -t, --times                 preserve modification times (保留修改时间)
            --delete                delete extraneous files from dest dirs(目标目录如果多了,就删掉)
        -W, --whole-file            copy files whole (w/o delta-xfer algorithm)(全文件拷贝,不使用diff算法,w/o是without的缩写)
        -h, --human-readable        output numbers in a human-readable format
            --progress              show progress during transfer
        -P                          same as --partial --progress (断点续传,进度显示)
        	--partial               keep partially transferred file

tips

w/owithout的缩写,切记切记。

0x02 传的慢

因为默认sync是要先diff文件,比较文件时间戳和大小几个特征再决定要不要传文件,所以传输很慢,我这里是1K/s不到的速度。

如何解决这个问题?

答案很简单,去掉不必要的校验,比如diff,大小比较可以保留。

这个时候就需要我大W选项,只去掉diff。

修改后,命令如下:

rsync -arvPW --delete counsellor@192.168.1.100:/path/to/big_project .

0x03 守护进程模式

rsync是可以作为守护进程一直同步文件的,这样就可以实时保证文件的及时更新。

当然首先要写一个配置文件 [ rsyncd.conf ]

######################################################################################################
#                      ******进程相关全局配置******
######################################################################################################
# = 后面的值可根据自己的实际情况更改
#    pid file 守护进程pid文件
#    port 守护进程监听端口,可更改,由xinetd允许rsyncd时忽略此参数
#    address 守护进程监听ip,由xinetd允许rsyncd时忽略此参数
pid file = /usr/local/var/run/rsyncd.pid
port = 873
address = 192.168.1.2
#rsyncd 守护进程运行系统用户全局配置,也可在具体的块中独立配置,
uid = root
gid = root
#允许 chroot,提升安全性,客户端连接模块,首先chroot到模块path参数指定的目录下
#chroot为yes时必须使用root权限,且不能备份path路径外的链接文件
use chroot = yes
#只读
read only = no
#只写
write only = no
#允许访问rsyncd服务的ip,ip端或者单独ip之间使用空格隔开
hosts allow = 192.168.0.1/255.255.255.0 198.162.145.1 10.0.1.0/255.255.255.0
#不允许访问rsyncd服务的ip,*是全部(不涵盖在hosts allow中声明的ip,注意和hosts allow的先后顺序)
hosts deny = *
#客户端最大连接数
max connections = 5
#欢迎文件路径,可选的
#motd file = /etc/rsyncd/rsyncd.motd
#日志相关
#    log file 指定rsync发送消息日志文件,而不是发送给syslog,如果不填这个参数默认发送给syslog
#    transfer logging 是否记录传输文件日志
#    log format 日志文件格式,格式参数请google
#    syslog facility rsync发送消息给syslog时的消息级别,
#    timeout连接超时时间
log file = /usr/local/logs/rsyncd.log
transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
######################################################################################################
#                      ******模块配置(多个)******
######################################################################################################
#模块 模块名称必须使用[]环绕,比如要访问data1,则地址应该是data1user@192.168.1.2::data1
[data1]
#模块根目录,必须指定
path=/home/username
#是否允许列出模块里的内容
list=yes
#忽略错误
#ignore errors
#模块验证用户名称,可使用空格或者逗号隔开多个用户名
auth users = data1user
#模块验证密码文件 可放在全局配置里
secrets file=/etc/rsyncd/rsyncd.secrets
#注释
comment = some description about this moudle
#排除目录,多个之间使用空格隔开
exclude = test1/ test2/

启动命令如下

rsync --daemon --config=/etc/rsync/rsyncd.conf

参考文献

https://juejin.im/entry/59152932da2f60005dd07d5d
https://www.jianshu.com/p/bd3ae9d8069c

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值