1

I have some program that generates a lot of data, to be specific encrypting tarballs. I want to upload result on a remote ftp server.

Files are quite big (about 60GB), so I don't want to waste hdd space for tmp dir and time.

Is it possible? I checked ncftput util, but there is not option to read from a standard input.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
galadog
  • 960
  • 2
  • 10
  • 20

4 Answers4

3

curl can upload while reading from stdin:

-T, --upload-file

[...]

Use the file name "-" (a single dash) to use stdin instead of a given file. Alternately, the file name "." (a single period) may be specified instead of "-" to use stdin in non-blocking mode to allow reading server output while stdin is being uploaded.

[...]

Anders Lindahl
  • 41,582
  • 9
  • 89
  • 93
  • `cat oldname.txt | curl -T - ftp.example.com/newname.txt --user myuser`. In my case, this prompts me for password, and it uploads the file as `newname.txt`. – Grav Jul 21 '16 at 21:42
1

I guess you could do that with any upload program using named pipe, but I foresee problems if some part of the upload goes wrong and you have to restart your upload: the data is gone and you cannot start back your upload, even if you only lost 1 byte. This also applied to a read from stdin strategy.

My strategy would be the following:

  1. Create a named pipe using mkfifo.
  2. Start the encryption process writing to that named pipe in the background. Soon, the pipe buffer will be full and the encryption process will be blocked trying to write data to the pipe. It should unblock when we will read data from the pipe later.
  3. Read a certain amount of data from the named pipe (let say 1 GB) and put this in a file. The utility dd could be used for that.
  4. Upload that file though ftp doing it the standard way. You then can deal with retries and network errors. Once the upload is completed, delete the file.
  5. Go back to step 3 until you get a EOF from the pipe. This will mean that the encryption process is done writing to the pipe.
  6. On the server, append the files in order to an empty file, deleting the files one by one once it has been appended. Using touch next_file; for f in ordered_list_of_files; do cat $f >> next_file; rm $f; done or some variant should do it.

You can of course prepare the next file while you upload the previous file to use concurrency at its maximum. The bottleneck will be either your encryption algorithm (CPU), you network bandwidth, or your disk bandwidth.

This method will waste you 2 GB of disk space on the client side (or less or more depending the size of the files), and 1 GB of disk space on the server side. But you can be sure that you will not have to do it again if your upload hang near the end.

If you want to be double sure about the result of the transfer, you could compute hash of you files while writing them to disk on the client side, and only delete the client file once you have verify the hash on the server side. The hash can be computed on the client side at the same time you are writing the file to disk using dd ... | tee local_file | sha1sum. On the server side, you would have to compute the hash before doing the cat, and avoid doing the cat if the hash is not good, so I cannot see how to do it without reading the file twice (once for the hash, and once for the cat).

jfg956
  • 16,077
  • 4
  • 26
  • 34
0

This is a sample of uploading to ftp site by curl

wget -O- http://www.example.com/test.zip | curl -T - ftp://user:password@ftp.example.com:2021/upload/test.zip
0

You can write to a remote file using ssh:

program | ssh -l userid host 'cd /some/remote/directory && cat - > filename'
glenn jackman
  • 238,783
  • 38
  • 220
  • 352