Delete File Only If Successful Transfer..

Hi.

I have this shell script for ftp..

ftp -n 12.34.5.67 << EOF
user username password
cd LOCAL/inbox
bin
get JAN_Total.gz
# del JAN_Total.gz
EOF

how do i modify the commented part i.e. del JAN_Total.gz only if that JAN_Total.gz has been successfully transfered to the local server.

Thanks a lot

Perfered solution is to use sftp:
sftp might a better choice, with the -b batchfile flag it will terminate the batch script if a command fails unless the command starts with a '-' character. sftp protocol also includes validation of blocks as there transfered so if file arrives you know it's intacked.

Downside is you need a server that supports it and for batch mode you will also need to setup public key authentication.

If you don't have the option of using sftp you could try ncftpget it supports the -DD option (Delete remote file after successfully downloading it.).

Thanks for your response.

But that would not be possible :smiley:

Coz this is only 0.1% of my total task contribution.. :o

Do you have (or can you get) ncftp/ncftpget installed?

try delete <filename>

Nope.. I dont think it's possible.

Coz I'm just on the programming user, not the server admin.

So, is there any other way? Couldnt this be achieved thru the unix programming script?

Thanks.

I've been developing something similar for downloading backups from my server. But I am using SSH and SFTP. What you should do is to first run the following command on the server to create an md5 checksum file:

md5sum foofile>foofile.md5

Then make sure that you download foofile.md5 first and then foofile. Before deleting remote foofile check that the md5 sum matches on the local computer:

md5sum foofile.md5

If it does you know the transfer succeeded and you can delete the file on the remote server. If not you try again.

If we can run the get command in background (im not sure though), then you could get the PID of bg job and loop it until it exists..Something like below..

ftp -n 12.34.5.67 << EOF
user username password
cd LOCAL/inbox
bin
get JAN_Total.gz &
PD=$!

PID_CNT=`ps -p $PD | wc -l`
# loop until the bg pid exists
while [[ $PID_CNT -gt 1 ]]
do 
	echo "Backgournd process still running"			
	sleep 5
	PID_CNT=`ps -p $PD | wc -l`
done
del JAN_Total.gz
EOF

We used the following solution for this problem
For the first pass, create a file (input.txt) that looks like:

user=
pass=
cd ???
get jan_totals.z
quit

Then

ftp 12.34.5.6 <input.txt

Then after some process on the receiving machine verifies, or processes the data, the process creates a new input.txt file for the next run that looks like:

user=
pass=
cd ???
delete jan_totals.z
get Feb_totals.z (or mget *)
quit

Then use the new file as input to the next run.