什么是规范的方式:
scp 一个文件到远程位置tar或不压缩,单个文件或整个文件夹,7za或者其他更有效的东西)我熟悉这样的壳管:
tar cf - MyBackups | 7za a -si -mx=9 -ms=on MyBackups.tar.7z
Run Code Online (Sandbox Code Playgroud)
本质上:
tarstdout给stdin压缩程序通过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哪些调用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"
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)有可能是一种方式来获得rsync和7za发挥不错,但没有这样做没有意义的。这样做的好处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 不存储文件的所有者/组。
小智 16
我认为这个命令可以解决问题
ssh user@host "cd /path/to/data/;tar zc directory_name" | tar zx
Run Code Online (Sandbox Code Playgroud)
现在,首先您必须从目标主机执行此命令。以及要说明的细节:
如您所见,此命令可即时压缩并节省带宽。您也可以使用其他压缩来获得更好的结果,但请记住,压缩和解压缩需要 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)
| 归档时间: |
|
| 查看次数: |
154017 次 |
| 最近记录: |