10

I have a file in which i wanted to zip up and at the same time transfer over to another host using scp.

I tried to do the following command but failed. I do not mind zipping up and scp over later, but i just want to know where did I got it wrong

Am i wrong to use a pipe | over here ?

-bash-3.2$ gzip -c aum.dmp | scp [email protected]:/export/home/oracle/aum.dmp.gz
Usage: scp [-pqrvBC46] [-F config] [-S program] [-P port]
           [-c cipher] [-i identity] [-o option]
       [[user@]host1:]file1 [...] [[user@]host2:]file2

Regards, Noob

1
  • if you specifically know the file name beforehand (and do not need to automate this), then you better skip the pipe and make sure to wait for gzip to (successfully) finish: gzip -c file.dmp && scp file.dmp.gy user@host:/path/to/file. Note that your command is missing the source file for the whole copying action.
    – FelixJN
    Commented Jul 28, 2015 at 8:23

3 Answers 3

11

gzip will write to STDOUT, and scp can't handle it.

try

gzip -c aum.dmp | ssh -l  oracle 192.168.0.191 'cat > /export/home/oracle/aum.dmp.gz'

instead.

where

  • gzip -c aum.dmp | will gzip aum.dmp, and send result to stdout
  • ssh -l oracle 192.168.0.191 will connect to user oracle on 192.168.0.191
  • 'cat > /export/home/oracle/aum.dmp.gz' will execute this command

'cat > /export/home/oracle/aum.dmp.gz'

  • cat will capture stdin (stdout from command before | )
  • > /export/home/oracle/aum.dmp.gz will write to this /export/home/oracle/aum.dmp.gz

the whole purpose of cat part, executed n remote site is to capture gzip result.

2
  • sorry for the late reply, i do not understand the last portion of the whole command , what does the ' ' meant ? so instead of scp, are we using ssh instead then ?
    – Noob
    Commented Jul 30, 2015 at 13:59
  • yes, scp can't handle STDIN/STDOUT but ssh can.
    – Archemar
    Commented Aug 29, 2016 at 7:17
11

You can use the -C flag to enable compression in scp transfer. This should be enough, although you can check man scp for more details on compression.

2
  • this will compress data on network, saving bandwith, it will not compress file.
    – Archemar
    Commented Jul 30, 2015 at 14:15
  • Using -C flag will make the process slower than normal - serverfault.com/q/697363/151023 Commented Dec 9, 2022 at 6:55
10

In case you need to get the files/directories from a remote server, into a local archive, you can use tar + gzip inside ssh, and redirect to a local file. For example:

ssh user@server "sudo tar cvzf - /var/log/containers/**/*.log" > containers_logs.tgz

Where:

  • c - create archive file.
  • v - show the progress.
  • z - compress with gzip.
  • f - filename of archive.
1
  • I think this is the best approach. Commented Jan 17 at 11:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .