通过网络优化 SCP

Optimizing SCP over network

我有一个 shell 脚本,它使用 scp 通过网络发送文件。该脚本以事件驱动的方式工作,即每次修改文件时,都会使用 scp 通过网络发送。我想优化脚本,而不是每次发送文件,我只发送 "new appended/new write data" 并在另一个端点

附加到文件

脚本如下

while true
do
inotifywait -e close_write,moved_to,create  . |
while read -r directory events filename; do
  if [ "$filename" = "keylog.txt" ]; then
     sshpass -p "password" scp -o StrictHostKeyChecking=no keylog.txt machine@192.168.151.19:/home/machine/keylog.txt 
  fi
done
sleep 0.00001
done

我建议看一下rsync。 它正是为这种目的而设计的。 引用自 man rsync:

   Rsync is a fast and extraordinarily versatile file copying tool.   It  can
   copy  locally,  to/from  another  host over any remote shell, or to/from a
   remote rsync daemon.  It offers a large number  of  options  that  control
   every aspect of its behavior and permit very flexible specification of the
   set of files to be copied.  It is famous for its delta-transfer algorithm,
   which reduces the amount of data sent over the network by sending only the
   differences between the source files and the existing files in the  desti-
   nation.  Rsync is widely used for backups and mirroring and as an improved
   copy command for everyday use.

在支持 scp 的服务器上, 通常 rsync 也可以。 试试这个:

sshpass -p "password" rsync keylog.txt machine@192.168.151.19:/home/machine/keylog.txt 

即使不调整参数, rsync 将尽量减少传输的数据量。 当目标文件已经存在时, 它只会转移必要的差异。