0

I'm uploading the file through Sftp to destination server using bash scripts.

How I can be sure that the file which is uploaded is complete upload in the case sftp will not return anything or network connection could be broken?

I see that I can get the size of the file before uploading to the server and then I can compare it with the existing size for the file on the server.

Perhaps you can mention about other better options?

Thank you.

yart
  • 7,515
  • 12
  • 37
  • 37
  • Use `scp` instead, as it provides an exit code that is useful for determining if there were errors - `sftp` doesn't. – twalberg Dec 27 '13 at 15:04
  • @twalberg, thank you but I can use only Sftp. – yart Dec 27 '13 at 15:19
  • The OpenSSH `sftp` provides an error indication in exit code too. – Martin Prikryl Dec 27 '13 at 16:13
  • @MartinPrikryl On at least one of the distros I currently use, the manual page for `sftp` doesn't mention an exit code. If it does provide one, is it based on the last command that was run (i.e. cd or ls or something) or on the last transfer, or on something else? I suppose maybe I'll have to go code-diving to figure that out. The exit conditions for `scp` are at least documented... – twalberg Dec 27 '13 at 16:36
  • You are right that it does not seem to be documented. It works only when `-b` switch is used (batch mode). In batch mode, script is aborted on any error and exit code `1` is returned. – Martin Prikryl Dec 27 '13 at 17:57

3 Answers3

0

Many sites for downloading softwares will provide both the software and its checksum.

we can use the same technique to check our uploading file.
upload the file together with its checksum, on the server side compare the file's checksum with uploaded checksum, if the two don't match, you will know

  1. The file uploaded is corrupted, or
  2. The checksum uploaded is corrupted, or
  3. Both the checksum and file uploaded are corrupted.
ray
  • 4,109
  • 1
  • 17
  • 12
0

I think getting the size is a good option. What I could imagine :

Client side : - Put the size of the file, and its md5 in a file, like ".fileinfo" - Send the fileinfo to the server - Send the (interesting) File to the server

Server side : - Check periodically files of a folder (with "watch ls" command for example) - If a ".fileinfo" exists, read it, and check if the size corresponds to an existing file of the same name (without ".filefome"). If the size corresponds, do an "md5sum" of the file, and check if it corresponds. If yes, move your file into your final destination folder, and delete the ".fileinfo" file. If not reiterate.

Neozaru
  • 1,109
  • 1
  • 10
  • 25
0

Test exit code of sftp. If it returns 0 you can be pretty sure that everything is ok (assuming you are using OpenSSH sftp). This works only when you use -b switch (what I assume you are doing).

SFTP protocol allows checksum calculation, but I suppose you are stuck with OpenSSH (or either or both sides) that does not support this.

To be 100% sure, you can download the file back and compare with original.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992