0

I have set of 10 files each can be of a varied size ranging from 1mb to 10mb. I want to transfer these files to the remote server via SFTP key based authentication (as per the requirement). I have written a simple shell script to pick the files from local directory and connect to remote server and then put all the files.

I want to know whether there is a way to check below

  1. File transfer failed in between(out of 10 files, 5 got transferred and 5 didn't).

  2. Transfer of partial files.

  3. Aborting the script when the transfer is happening.

Sample code :

cd local_directory

sftp -i privatekey username@ip_address  << EOF 2>> TMP_LOG

cd /data

pwd

put *

bye

EOF

if [[ $? != 0 ]]

then

echo "Failure"

else

echo "fine"

fi

But this doesn't seem to be working fine:

  1. When script is aborted.
  2. Transfer is partial.
  3. SFTP connection getting lost.

Any suggestion on this please?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Vimruth
  • 3
  • 2
  • Possible duplicate of [How to confirm sftp file delivery?](https://stackoverflow.com/questions/28223890/how-to-confirm-sftp-file-delivery) – brunorey Mar 21 '19 at 18:51

1 Answers1

0

Yeah, sftp isn't working very well with exit status reporting.

You can use diff to find if there is any differences, by default, diff gives exit status 1 if there is a difference, which means that you can use it in script.

To compare local file and remote file with diff:

ssh user_name@remotehost ls -R /remote/dir > remotedirfiles.txt 
ls -R /local/dir > localdirfiles.txt
diff remotedirfiles.txt localdirfiles.txt

And if ssh prompt you for password, that will not work, you will need to redirect both results to text files and compare them with diff.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459