0

This script will send a file via FTP and then delete it. But, sometimes, the file is deleted before the transmission ends, and then an empty file is received.

#!/bin/bash

tar czf <sourcefile> --directory=<directory> log
ftp -v -n $1 <<END_OF_SESSION
user <user> <password>
put <sourcefile> <targetfile>
bye
END_OF_SESSION

rm <sourcefile>

What would be a good way to synchronize the processes, such that the deletion occurs after the sending has completed?

As shown in the update below, the connection sometimes cannot be establish.

Notes:

Running on Lubuntu 16.04.

Updated with the tar line.

log information for a failed session:

Connected to IP
220 (vsFTPd 3.0.2)
331 Please specify the password.
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
200 Switching to Binary mode.
local: /home/user01/tmp/log.tgz remote: E1/180418090056
200 PORT command successful. Consider using PASV.
425 Failed to establish connection.
221 Goodbye.

and a successful one:

Connected to IP
220 (vsFTPd 3.0.2)
331 Please specify the password.
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
200 Switching to Binary mode.
local: /home/user01/tmp/log.tgz remote: E1/180418090344
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
6901 bytes sent in 0.00 secs (43.5848 MB/s)
221 Goodbye.
  • is your ftp server working on windows ? – Kiwy Apr 17 '18 at 10:28
  • 1
    The script you've shown us will finish the ftp session before the rm is executed. – Mark Plotnick Apr 17 '18 at 10:39
  • @Kiwy updated: Linux. – nightcod3r Apr 17 '18 at 10:44
  • 1
    Don't you want to change into binary mode before transfer? – Ralph Rönnquist Apr 17 '18 at 10:53
  • 1
    In general: in your scripts check the exit code of commands and include error handling. You currently don't know if the ftp command was completed successfully or not before you delete the source file. - Second calculate a checksum, store that in a second file and upload both to determine if what has been uploaded differs from the original – HBruijn Apr 17 '18 at 11:14
1

The ftp command has no functionality to allow you to check for successful transfer. If you must continue using this implementation of FTP transfer, two alternatives are:

  1. Download the transmitted file to a local temporary and compare it byte for byte against the source.
  2. Run ls within the FTP client and check that the file length matches expectations. Bear in mind that ls is server dependent, and can vary from server implementation to implementation.

The best solution (other than replacing FTP entirely with rsync or scp) is to use a different FTP client that provides a reliable transfer status.

#!/bin/bash
tar czf <sourcefile> --directory=<directory> log
lftp -u '<user>,<password>' -e 'put -E <source> -o <target>; quit' "$1"

The lftp command should be available in most Linux distributions. The -E flag configures the put command to act more like mv rather than cp: it deletes the source file after a successful transfer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.