使用 rsync 压缩整个目录并上传到远程

jam*_*unt 2 rsync

我知道我可以这样:

rsync -zaP uploads/ myserver:/path/to/
Run Code Online (Sandbox Code Playgroud)

如果我理解正确,那将压缩该目录中的每个文件并一一同步到服务器。但如果是数千个文件,则需要一些时间。压缩整个目录并上传要快得多。

那么,有没有一种瓷器方式可以让我做到这一点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"
}
Run Code Online (Sandbox Code Playgroud)

句法:

zipsync source target
Run Code Online (Sandbox Code Playgroud)

例子:

$ zipsync uploads my_server:/var/www/example.com/public_html
Run Code Online (Sandbox Code Playgroud)

该函数的问题:

  1. 无法在本地计算机中完成选项卡。
  2. 无法在远程服务器中完成选项卡。
  3. 无法在目标路径中指定端口,这将不起作用:zipsync uploads -p 5555 bob@xmpl.com:/path/因为-p被读取为第二个参数。

我的目标是制作一个非常易于使用和重新组装的命令rsync

roa*_*ima 9

如果我理解正确,rsync -zaP uploads/ myserver:/path/to/将压缩该目录中的每个文件并一一同步到服务器。

这是不正确的。该rsync命令查看本地文件,将它们与远程文件(如果有)进行比较,然后将差异同步到服务器。如果没有匹配的远程文件,则速度不会增加。但是,对于仅更改了部分文件的后续上传,速度提升可能会非常显着。该-z标志尝试对通过链接传输的数据应用压缩。

但如果是数千个文件,则需要一些时间。压缩整个目录并上传要快得多。那么,有没有一种方法可以让我用 rsync 做到这一点?

你的理解有缺陷,所以我认为这个问题没有实际意义。你帖子的其余部分似乎不是一个问题,所以我不确定你期待什么答案。如果我弄错了,请更新问题。


fak*_*ker 7

如果您只想在目标为空的情况下仅运行一次,那么它会更快,是的。
但是你的函数过于复杂了。
你可以运行:

 tar zcvf - /source | ssh destination.example.com "cd /destination; tar xvzf -"
Run Code Online (Sandbox Code Playgroud)

如果要运行同步以同步更改,请参阅 roaima 的答案。