50

Note that I can't first store the file locally -- it's too big.

This (obnoxious) page (scroll all the way to the bottom) seems to give an answer but I'm having trouble disentangling the part that's specific to tape drives:

http://webcache.googleusercontent.com/search?q=cache:lhmh960w2KQJ:www.experts-exchange.com/OS/Unix/SCO_Unix/Q_24249634.html+scp+redirect&cd=3&hl=en&ct=clnk&gl=us

To make this more concrete, here's how you would think it might work:

On local machine:

% echo "pretend this string is a huge amt of data" | scp - remote.com:big.txt

(That's using the convention -- which scp does not in fact support -- of substituting a dash for the source file to tell it to get it from stdin instead.)

migrated from stackoverflow.com Jun 2 '11 at 0:39

This question came from our site for professional and enthusiast programmers.

  • Can you post the url of your google result? Experts Exchange only shows the answer at the bottom if your referrer is google... – Jon Jun 21 '10 at 19:52
76

You can pipe into ssh and run a remote command. In this case, the remote command is cat > big.txt which will copy stdin into the big.txt file.

echo "Lots of data" | ssh user@example.com 'cat > big.txt'

It's easy and straightforward, as long as you can use ssh to connect to the remote end.

You can also use nc (NetCat) to transfer the data. On the receiving machine (e.g., host.example.com):

nc -l 1234 > big.txt

This will set up nc to listen to port 1234 and copy anything sent to that port to the big.txt file. Then, on the sending machine:

echo "Lots of data" | nc host.example.com 1234

This command will tell nc on the sending side to connect to port 1234 on the receiver and copy the data from stdin across the network.

However, the nc solution has a few downsides:

  • There's no authentication; anyone could connect to port 1234 and send data to the file.
  • The data is not encrypted, as it would be with ssh.
  • If either machine is behind a firewall, the chosen port would have to be open to allow the connection through and routed properly, especially on the receiving end.
  • Both ends have to be set up independently and simultaneously. With the ssh solution, you can initiate the transfer from just one of the endpoints.
  • If it's any consolation, I'm inclined to mark yours accepted since it explains nicely what's actually going on. (If you wanted to really clinch it, you could include the FIFO pipe and netcat solutions with some guidance on why you might prefer one or the other! :) – dreeves Jun 21 '10 at 20:06
  • Done. Netcat is a handy utility. :) – Barry Brown Jun 21 '10 at 20:37
  • As in the other comment, if you are piping through tar, you can use a process substitution: tar -cvzf >(ssh destination 'cat > file') huge_directory_tree – Taywee Jul 6 '13 at 0:49
  • 1
    SSH is indeed the way to go. Compared to nc it also offers encryption and compression of your data by default and more importantly: error-detection. I had situations where I used nc with a faulty network driver and corrupted data got transmitted undetected. SSH will fail in that situation, because it can't decrypt/decompress faulty data. – jlh Jul 18 '16 at 11:10
15

Using ssh:

echo "pretend this is a huge amt of data" | ssh user@remote.com 'cat > big.txt'
  • Ah, beautiful, thank you! Any reason to prefer this or FIFO pipe solution? – dreeves Jun 21 '10 at 19:58
  • I think the mknod approach accomplishes the task in exactly the same way except for the named pipe. – bpf Jun 21 '10 at 20:03
4

Use nc (Net Cat), which doesn't need to save the file locally.

  • Ah, thanks! Want to include the equivalent of my "echo .. | scp .." example? And any reasons you know of to prefer this to the other answers? – dreeves Jun 21 '10 at 20:09
  • 2
    I strongly recommend against using nc for this. I once dumped a raw disk image from one machine to another only to find out much later that my network driver was faulty and transferred faulty bits. Use scp, ssh or anything else that will tell you when there's a transmission error. – jlh Oct 10 '16 at 19:07
2

Use a FIFO pipe:

mknod mypipe p
scp mypipe destination &
ls > mypipe
  • I couldn't get this to work on a Linux system. scp complained that mypipe was not a regular file. – Barry Brown Jun 21 '10 at 20:05
  • 1
    Didn't work on a Mac, either, for the same reason. (I had to use mkfifo to create the pipe, though.) – Barry Brown Jun 21 '10 at 20:24
  • 1
    Didn't work here either. But if it does work for you, and you have bash or zsh, you could better achieve this with a process substitution, like for this example: scp <(ls) destination – Taywee Jul 6 '13 at 0:48
1

Thanx Denis Scherbakov!

When I tried your script on the Hetzner cloud I got

debug1: Sending command: scp -v -t backup-20180420120524.tar.xz.enc
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
Transferred: sent 4168, received 2968 bytes, in 0.0 seconds
Bytes per second: sent 346786.6, received 246944.0

But only a file with no content got created. As the actual content is already encrypted with openssl, we actually don't need scp. The linux builtin ftp also has great piping capabilities. So here's my (still quite manual) solution:

#!/bin/bash

function join_e
{
  for word in $*; do
    echo -n "--exclude=$word "
  done
}


# Directory and file inclusion list
ILIST=(
  /home
)

# Directory and file exclusion list
ELIST=(
  var/lib/postgresql
)



export OPASS=fileencryptionpassword

nice -n 19 bash -c \
   "\
   tar $(join_e ${ELIST[@]}) -cpvf - -C / ${ILIST[*]} \
   | xz -c9e -T8 \
   | openssl enc -aes-256-cbc -pass env:OPASS \
   "

# decrypt with:
# cat backup.tar.xz.enc | openssl  aes-256-cbc -d  -pass env:OPASS | xz -dc | tar xv

# invocation procedure for ftp:
# $ ftp -np
# ftp> open storage.com
# ftp> user  storageuser storagepass
# ftp> put "| bash ~/backup.sh" backup.tar.xz.enc
1

Here is an alternative solution:

All of the examples above suggesting ssh+cat assume that "cat" is available on the destination system.

In my case the system (Hetzner backup) had a very restrictive set of tools offering sftp, but not a complete shell. So using ssh+cat was not possible. I came up with a solution that uses undocumented "scp -t" flag. The complete script may be found below.

#!/bin/bash

function join_e
{
  for word in $*; do
    echo -n "--exclude=$word "
  done
}

CDATE=`date +%Y%m%d%H%M%S`

# Make password available to all programs that are started by this shell.
export OPASS=YourSecretPasswrodForOpenSslEncryption

#-----------------------------------------------

# Directory and file inclusion list
ILIST=(
  var/lib
)

# Directory and file exclusion list
ELIST=(
  var/lib/postgresql
)

# 1. tar: combine all files into a single tar archive
#      a. Store files and directories in ILIST only.
#      b. Exclude files and directories from ELIST.
# 2. xz: compress as much as you can utilizing 8 threads (-T8)
# 3. openssl: encrypt contents using a password stored in OPASS local environment variable
# 4. cat: concatenate stream with SCP control message, which has to be sent before data
#      a. C0600 - create a file with 600 permissions
#      b. 107374182400 - maximum file size
#         Must be higher or equal to the actual file size.
#         Since we are dealing with STDIN, we have to make an educated guess.
#         I've set this value to 100x times my backups are.
#      c. stdin - dummy filename (unused)
# 5. ssh: connect to the server
#      a. call SCP in stdin (-t) mode.
#      b. specify destination filename

nice -n 19 bash -c \
   "\
   tar $(join_e ${ELIST[@]}) -cpf - -C / ${ILIST[*]} \
   | xz -c9e -T8 \
   | openssl enc -aes-256-cbc -pass env:OPASS \
   | cat <(echo 'C0600 107374182400 stdin') - \
   | ssh username@server.your-backup.de "\'"scp -t backup-${CDATE}.tar.xz.enc"\'"\
   "

Update 2019.05.08:

As per request, below is a much simpler and shorter version.

#!/bin/sh

# WORKS ON LARGE FILES ONLY

cat filename.ext \
| cat <(echo 'C0600 107374182400 stdin') - \
| ssh user@host.dom 'scp -t filename.ext'
  • Can you improve this by removing all the unnecessary things and just pare this down to the bare minimum example of how to use scp -t? Right now you have a complete script that's highly custom/localized to your environment. A good thing for the Hetzner wiki, but not for Super User where most people are just looking for how to pipe input through scp. – allquixotic May 7 at 2:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.