scp 和 compress 同时进行,无需中间保存

Rob*_*ino 74 ssh scp tar 7z

什么是规范的方式:

  • scp 一个文件到远程位置
  • 压缩传输中的文件(tar或不压缩,单个文件或整个文件夹,7za或者其他更有效的东西)
  • 在不保存中间文件的情况下执行上述操作

我熟悉这样的壳管:

tar cf - MyBackups | 7za a -si -mx=9 -ms=on MyBackups.tar.7z
Run Code Online (Sandbox Code Playgroud)

本质上:

  • 将整个文件夹滚动到一个 tar
  • 将数据传递stdoutstdin压缩程序
  • 应用积极压缩

通过ssh链接执行此操作的最佳方法是什么,文件登陆远程文件系统?


我宁愿不sshfs安装。


这行不通:

scp <(tar cvf - MyBackups | 7za a -si -mx=9 -so) localhost:/tmp/tmp.tar.7z
Run Code Online (Sandbox Code Playgroud)

因为:

/dev/fd/63: not a regular file
Run Code Online (Sandbox Code Playgroud)

ter*_*don 123

有很多方法可以做你想做的事。最简单的是使用管道:

tar zcvf -  MyBackups | ssh user@server "cat > /path/to/backup/foo.tgz"
Run Code Online (Sandbox Code Playgroud)

在这里,压缩由tar哪些调用gzipz标志)处理。您也可以使用compress( Z) 和bzip( j)。对于7z,请执行以下操作:

tar cf - MyBackups | 7za a -si -mx=9 -ms=on MyBackups.tar.7z | 
   ssh user@server "cat > /path/to/backup/foo.7z"
Run Code Online (Sandbox Code Playgroud)

最好的方式,但是,可能是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 dae?
   mon.  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 exist?
   ing files in the destination.  Rsync is widely used for backups and  mirroring  and
   as an improved copy command for everyday use.
Run Code Online (Sandbox Code Playgroud)

rsync办法太多的选择。确实值得通读它们,但它们乍一看很吓人。在这种情况下,您关心的是:

    -z, --compress              compress file data during the transfer
        --compress-level=NUM    explicitly set compression level

   -z, --compress
          With this option, rsync compresses the file data as it is sent to the desti?
          nation machine, which reduces the amount of data being transmitted --  
          something that is useful over a slow connection.

          Note  that this option typically achieves better compression ratios than can
          be achieved by using a compressing remote shell or a  compressing  transport
          because  it takes advantage of the implicit information in the matching data
          blocks that are not explicitly sent over the connection.
Run Code Online (Sandbox Code Playgroud)

所以,在你的情况下,你会想要这样的东西:

rsync -z MyBackups user@server:/path/to/backup/
Run Code Online (Sandbox Code Playgroud)

文件将在传输过程中被压缩,并在到达目的地时进行解压缩。


还有一些选择:

  • scp 本身可以压缩数据

     -C      Compression enable.  Passes the -C flag to ssh(1) to
             enable compression.
    
    $ scp -C source user@server:/path/to/backup
    
    Run Code Online (Sandbox Code Playgroud)
  • 有可能是一种方式来获得rsync7za发挥不错,但没有这样做没有意义的。这样做的好处rsync是它只会复制本地和远程文件之间发生变化的位。但是,小的本地更改可能会导致非常不同的压缩文件,因此没有必要使用rsync此方法。它只会使事情复杂化而没有任何好处。直接使用ssh如上图所示。如果你真的想这样做,你可以尝试给rsync. 在我的系统上,我无法使用7za它,因为它不允许您将压缩数据写入终端。也许你的实现是不同的。尝试类似的东西(这对我不起作用):

    rsync $(tar cf - MyBackups | 7za a -an -txz -si -so) \
      user@server:/path/to/backup
    
    Run Code Online (Sandbox Code Playgroud)
  • 还有一点是7z 不应该用于 Linux 上的备份。如7z手册页所述:

    不要在 Linux/Unix 上使用 7-zip 格式进行备份,因为:
    - 7-zip 不存储文件的所有者/组。

  • +1 为`scp -C`。远程磁盘上没有足够的空间来保存压缩文件,因此我无法在传输前进行压缩。一个小的命令行选项使我的问题消失了。 (4认同)
  • 一个很好的说明是,除非您通过通常较慢的网络进行传输,例如互联网,否则最好避免压缩,因为它只会降低传输速率。在 LAN 上,`-z` 至少慢两倍。要获得比通过 ssh 进行 rsync 更快的速度,请使用 `-W` 标志设置一个 rsync 守护进程和 rsync(复制整个文件(无 delta-xfer 算法)。 (3认同)
  • 谢谢!我将接受这个很好的答案,但请添加一个完整的独立命令行,它同时使用 `rsync` _and_ `7za`,最终输出到远程文件系统。我喜欢`-z`,但我想解耦压缩阶段,所以..在这种情况下我将如何使用`rsync`? (2认同)
  • @Robottinosino 查看更新的答案。将 `rsync` 与 `7z` 一起使用是没有意义的。它_应该_与 rsync 和 subshel​​ 一起工作,如图所示,但我不知道怎么做。 (2认同)

小智 16

我认为这个命令可以解决问题

ssh user@host "cd /path/to/data/;tar zc directory_name" | tar zx 
Run Code Online (Sandbox Code Playgroud)

现在,首先您必须从目标主机执行此命令。以及要说明的细节:

  1. ssh user@host 将打开与主机的连接,从那里传输数据。
  2. cd /path/to/data 将转到存储所需数据的目录
  3. tar zc * 将启动压缩并将其放入 STDOUT
  4. 现在 pipe(|) 会将源的 STDOUT 通过管道传输到“tar zx”正在运行的目标的 STDIN,并不断解压缩来自源的数据流。

如您所见,此命令可即时压缩并节省带宽。您也可以使用其他压缩来获得更好的结果,但请记住,压缩和解压缩需要 CPU 周期。

参考


小智 12

dkbhadeshiya 的答案的小改进:您不必这样做cd dir,只需将工作目录指定为tar

ssh user@host "tar -C /path/to/data/ -zc directory_name" | tar zx 
Run Code Online (Sandbox Code Playgroud)

你也可以用同样的方式上传目录:

tar zc directory_name/ | ssh user@host "tar zx -C /new/path/to/data/"
Run Code Online (Sandbox Code Playgroud)