有很多方法可以做您想要的。最简单的方法是使用pìpe:
tar zcvf - MyBackups | ssh user@server "cat > /path/to/backup/foo.tgz"
在这里,压缩由tar
哪个调用gzip
(z
标志)处理。您也可以使用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"
在最好的方式,但是,可能是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.
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.
因此,在您的情况下,您需要这样的东西:
rsync -z MyBackups user@server:/path/to/backup/
这些文件在传输过程中将被压缩,并在目标位置解压缩。
其他一些选择:
scp
本身可以压缩数据
-C Compression enable. Passes the -C flag to ssh(1) to
enable compression.
$ scp -C source user@server:/path/to/backup
也许有一种获得rsync
和7za
发挥良好的方法,但这样做毫无意义。这样做的好处rsync
是,它将仅复制本地文件和远程文件之间已更改的位。但是,较小的本地更改可能会导致完全不同的压缩文件,因此没有必要使用rsync
此文件。它只会使事情变得复杂,没有任何好处。ssh
如上所示,直接使用即可。如果您确实要执行此操作,则可以尝试通过将subshell用作参数rsync
。在我的系统上,我无法使用7za
它,因为它不允许您将压缩数据写入终端。也许您的实现是不同的。试试类似的东西(这对我不起作用):
rsync $(tar cf - MyBackups | 7za a -an -txz -si -so) \
user@server:/path/to/backup
另一点是,7z
不应将其用于Linux上的备份。如7z
手册页所述:
在Linux / Unix上,请勿将7-zip格式用于备份目的,因为:
-7-zip不存储文件的所有者/组。
-z
速度至少要慢两倍。要获得比ssh上的rsync更快的速度,请设置rsync守护进程和使用-W
flag的rsync (复制整个文件(不包括delta-xfer算法)