通过网络优化SCP

时间:2016-11-28 19:08:55

标签: linux bash shell scp

我有一个shell脚本,它通过网络使用scp发送文件。该脚本以事件驱动的方式工作,即每次修改文件时,都使用scp通过网络发送。我想优化脚本,以便每次发送文件时,我只发送“新附加/新写入数据”并将其附加到另一端点的文件

脚本如下

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

1 个答案:

答案 0 :(得分:0)

我建议您查看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会尽量减少传输的数据量。 目标文件已存在时 它只会转移必要的差异。