scp: how to find out that copying was finished
Off the top of my head, you could do something like:
touch tinyfilescp bigfile tinyfile user@host:
Then when tinyfile
appears you know that the transfer of bigfile
is complete.
As pointed out in the comments, this assumes that scp
will copy the files one by one, in the order specified. If you don't trust it, you could do them one by one explicitly:
scp bigfile user@host:scp tinyfile user@host:
The disadvantage of this approach is that you would potentially have to authenticate twice. If this were an issue you could use something like ssh-agent.
On sending side (host1) use script like this:
#!/bin/bashecho 'starting transfer'scp FILE USER@DST_SERVER:DST_PATHOUT=$?if [ $OUT = 0 ]; then echo 'transfer successful' touch successful scp successful USER@DST_SERVER:DST_PATHelse echo 'transfer faild'fi
On receiving side (host2) make script like this:
#!/bin/bash SLEEP_TIME=30MAX_CNT=10CNT=0while [[ ! -e successful && $CNT < $MAX_CNT ]]; do ((CNT++)) sleep($SLEEP_TIME);done; if [[ -e successful ]]; then echo 'successful' rm successful # do somethning with FILEfi
With CNT
and MAX_CNT
you disable endless loop (in case file successful
isn't transferred).Product MAX_CNT
and SLEEP_TIME
should be equal or greater expected transfer time. In my example expected transfer time is less than 300 seconds.
A checksum (md5sum
, sha256sum
,sha512sum
) of the local and remote files would tell you if they're identical.
For the situation where you don't have SSH access to the remote system - like an FTP server - you can download the file after it's uploaded and compare the checksums. I do this for files I send from production scripts at work. Below is a snippet from the script in which I do this.
MD5SRC=$(md5sum $LOCALFILE | cut -c 1-32)MD5TESTFILE=$(mktemp -p /ramdisk)curl \ -o $MD5TESTFILE \ -sS \ -u $FTPUSER:$FTPPASS \ ftp://$FTPHOST/$REMOTEFILEMD5DST=$(md5sum $MD5TESTFILE | cut -c 1-32)if [ "$MD5SRC" == "$MD5DST" ]then echo "+Local and Remote files match!"else echo "-Local and Remote files don't match"fi