Deepak K M
Deepak K M

Reputation: 561

What error is returned by sftp (in unix) if the file is partially uploaded?

I am using the sftp command to upload a file to remote server(unix) from local system (linux) in a kshell script. Sometimes the file is partially uploaded(mostly because of empty memory issues in server). How to detect whether a file is fully uploaded or partially uploaded.What is the exact error message that is returned by sftp if the file is partially uploaded? Below is the code used in my shell script.

{
        sftp $Remote_srvr << EOF
        cd $TARGET_DIR
        put $path_files/$dump
        bye
EOF
} > $HOME/Proc1/sftp.log 2>&1
stat=$?
echo "sftp exit status is $stat"

Upvotes: 1

Views: 2147

Answers (2)

geocar
geocar

Reputation: 9305

You can try something like this:

{
    sftp $Remote_srvr << EOF
    cd $TARGET_DIR
    put $path_files/$dump
    bye
EOF
} tee $HOME/Proc1/sftp.log 2>&1 \
| grep -v '^Uploading' \
| grep -v '^sftp>' \
| grep -v '^Connected' > $HOME/Proc1/sftp.filtered.log

to look for any anomalies that get printed out. If the filtered log is non-empty then email it to yourself.

However.

I'd actually recommend using a different tool, such as rsync which verifies the transfer and returns the error code like you'd expect:

rsync -avz $path_files/$dump $Remote_srvr:$TARGET_DIR/./

may give you exactly what you want.

Upvotes: 1

P.P
P.P

Reputation: 121417

You can do it in sftp's batch mode:

cat >/tmp/batch_cmds <<EOF
cd $TARGET_DIR
put $path_files/$dump
bye
EOF

sftp -b /tmp/batch_cmds $Remote_srvr > $HOME/Proc1/sftp.log 2>&1
stat=$?
echo "sftp exit status is $stat"
rm /tmp/batch_cmds

In batch mode, sftp will abort on the first failure in the sequence of commands. But I am not sure if partial upload is considered as failure of put command.

Upvotes: 1

Related Questions