简体   繁体   English

通过 SSH 远程创建压缩 tar 文件,仅包含文件子集

[英]Creating compressed tar file, with only subset of files, remotely over SSH

I've successfully managed to transfer a tar file over SSH on stdout from a remote system, creating a compressed file locally, by doing something like this:我已经成功地从远程系统的标准输出上通过 SSH 传输了一个 tar 文件,通过执行以下操作在本地创建一个压缩文件:

read -s sudopass
ssh me@remote "echo $sudopass | sudo -S tar cf - '/dir'" 2>/dev/null | XZ_OPT='-6 -T0 -v' xz > dir.tar.xz

As expected this gets me a dir.tar.xz locally which is all of the remote /dir compressed.正如预期的那样,这让我在本地获得了一个dir.tar.xz ,它是所有远程/dir压缩的。

I've also managed to figure out how to locally only compress a subset of files, by passing a filelist to tar with -T on STDIN:我还设法弄清楚如何在本地仅压缩文件的子集,方法是在 STDIN 上使用-T将文件列表传递给tar

find '/dir' -name '*.log' | XZ_OPT='-6 -T0 -v' tar cJvf /root/logs.txz -T -

My main question is: how would I go about doing the first thing (transfer plain tar remotly, then compress locally) while at the same time telling tar that I only want to do it on a specific subset of files?我的主要问题是:我将如何 go 做第一件事(远程传输普通 tar,然后在本地压缩),同时告诉tar我只想对特定的文件子集执行此操作?

When I try combining the two:当我尝试将两者结合时:

ssh me@remote "echo $sudopass | sudo -S find '/dir' -name '*.log' | tar cf
-T -" | XZ_OPT='-6 -T0 -v' xz > cypress_logs.tar.xz

I get errors like:我收到如下错误:

tar: -: Cannot stat: No such file or directory

I feel like tar isn't liking the fact that I'm both passing it something on STDIN as well as expecting it to output to STDOUT.我觉得tar不喜欢这样一个事实,即我既在 STDIN 上传递了一些东西,又期望它到 output 到 STDOUT。 Adding another - didn't seem to help either.添加另一个-似乎也没有帮助。

Also, as a bonus question, if anyone has a better idea on how to pass $sudopass above that would be great, since this method -- while avoiding having the password in the bash history -- makes the sudo password show up in the process list while it's running.另外,作为一个额外的问题,如果有人对如何通过$sudopass以上有更好的想法,那将是很好的,因为这种方法 - 同时避免在 bash 历史记录中使用密码 - 使 sudo 密码显示在过程中运行时列出。

Remember that the f option requires an argument, so when you write cf -T - , I suspect that the -T is getting consumed as the argument to f , which throws off the rest of the command line.请记住, f选项需要一个参数,因此当您编写cf -T -时,我怀疑-T被用作f的参数,这会抛出命令行的 rest。

This works for me:这对我有用:

ssh me@remote "echo $password | sudo -S find /tmp/dir -name '*.log' | tar -cf- -T-"

You could also write it like this:你也可以这样写:

ssh me@remote "echo $password | sudo -S find /tmp/dir -name '*.log' | tar cf - -T-"

But I prefer to always use - for options, rather than legacy tar's weird options without any prefix.但我更喜欢总是使用-作为选项,而不是没有任何前缀的遗留 tar 的奇怪选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM