4

I have some gzipped files I want to transfer to a remote server and extract directly onto the target drive.

It is similar to this command

dd if=/dev/sda bs=5M conv=fsync status=progress | gzip -c -9 | ssh user@DestinationIP 'gzip -d | dd of=/dev/sda bs=5M'

From this question on Server Fault.

In my case the file is already compressed so the gzip -9 on the source will not be necessary.

It would be more along the lines of:

load-some-file /drive/image.tar.gz | ssh user@DestinationIP 'gzip -d | dd of=/dev/sda bs=5M'

What command should load-some-file be?

Will a command like cat suffice, or will dd itself do?

Can the output of dd be sent to the ssh command directly?

Giacomo1968
  • 58,727
vfclists
  • 883
  • 2
  • 11
  • 24

1 Answers1

6

What command should load-some-file be?

No command at all. Make ssh itself read the file, i.e. redirect its input:

</drive/image.tar.gz ssh …

Will a command like cat suffice?

cat /drive/image.tar.gz | ssh … or </drive/image.tar.gz cat | ssh … should work, but this is a useless use of cat.

Side note: the form sudo cat /drive/image.tar.gz | ssh … is a good idea when you need elevated access in order to open the file. Here the elevated cat will open and read the file. Redirection with < works by the shell opening the file and setting it up as stdin (i.e. passing the right descriptor) for the tool to read. In this case even </drive/image.tar.gz sudo … would not work because the unelevated shell would be denied access when trying to open the file in the first place. Compare to this answer (it's about output redirection, stdout, but the mechanics is the same).

or will dd itself do? Can the output of dd be sent to the ssh command directly?

Yes, in principle. There are many commands that can output their input as-is, see this answer. Still, if you just want ssh to get the file on its stdin then input redirection will be enough.