我知道我可以像:
rsync -zaP uploads/ myserver:/path/to/
如果我理解正确,那将会压缩该目录中的每个文件,并逐一同步到服务器。 但是,如果它是成千上万的文件,那就需要一些时间。 压缩整个目录并上传速度要快得多。
那么,有没有一种方法可以让我用rsync来做到这一点?
我写了一个小小的bash函数来压缩和移动rsync的整个目录。 你将如何简化或改善?
function zipsync() { # Arguments local source=$1 local target=$2 # Get the host and path from the $target string IFS=':' read -a array <<< "$target" local host=${array[0]} local remote_path=${array[1]} # The archive file locations local remote_archive=${remote_path}${source}.tar.gz local local_archive=${source}.tar.gz # Colors cya='\033[0;36m'; gre='\033[0;32m'; rcol='\033[0m' echo -e "$cya Compressing files $rcol" tar -zcvf $local_archive $source echo -e "$cya Syncing files $rcol" rsync -avP $local_archive $target echo -e "$cya Extracting file in remote $remote_archive $rcol" ssh $host "cd ${remote_path}; tar zxvf ${remote_archive}" echo -e "$cya Removing the archives $rcol" ssh $host "rm $remote_archive" rm $local_archive echo -e "$gre All done :) $rcol" }
句法:
zipsync source target
例:
$ zipsync uploads my_server:/var/www/example.com/public_html
这个function的问题:
zipsync uploads -p 5555 [email protected]:/path/导致-p被读为第二个参数。 我的目标是制作一个非常简单的命令,用于重新组装rsync 。
如果我理解正确,
rsync -zaP uploads/ myserver:/path/to/将压缩该目录中的每个文件并逐一同步到服务器。
这是不正确的。 rsync命令查看本地文件,将它们与远程文件(如果有)进行比较,然后将差异同步到服务器。 如果没有匹配的远程文件,则不会有速度增加。 但是,对于只有部分文件被更改的后续上传,速度的提高会变得非常激烈。 -z标志尝试将压缩应用于通过链接传输的数据。
但是,如果它是成千上万的文件,那就需要一些时间。 压缩整个目录并上传速度要快得多。 那么,有没有一种方法可以让我用rsync来做到这一点?
你的理解是有缺陷的,所以我觉得这个问题是没有意义的。 您的post的其余部分似乎不是一个问题,所以我不知道你所期待的是什么答案。 如果我弄错了,请更新这个问题。
如果你只想运行这个目标是空的一次,那么它可以更快是的。
但是你的function过于复杂。
你可以运行:
tar zcvf - /source | ssh destination.example.com "cd /destination; tar xvzf -"
如果要运行同步来同步更改,请参阅roaima的答案。