FTP script to get latest file from server

Hello people,

I have to download, with a scheduled script, the latest file from an FTP server.

In the remote DIR, named .../TEKNONET/60468/, every night a CDR file like this gets uploaded into it: 000006046820151122N001.CDR, so my script will have to download every day the previous day file.

Could you please help me to build this script?
Thanks

Any attempts/ideas/thoughts from your side?

Is that the only file in the remote directory? Do you have interactive access to that server?

Do it in two passes. This is not complete.

ftp host >new_file_list <<EOF
ls 
quit
EOF
diff old_file_list new_file_list >files_to_get
list=`cat files_to_get`
ftp host <<EOF 
get $list
quit
EOF
ls >old_file_list  # lists only files received.

I have to login in this remote FTP. I suppose that it is interactive as I can do ls and other commands.
Could you please post a bash to login ftp, cd to remote directory /TEKNONET/60468/ and then get the latest file in this format: 0000060468yyyymmddN00. Zip?
I stress the fact that this "automate script" must download every day the regarding zip file.
Ex. Today's zip file belongs to yesterday, yesterday's file belonged to the day before yesterday, ecc...

Thanks

.Zip ? .CDR ? ...N00 ? ...N001 ? Does your system offer date -d ? What version of bash ?

Yes I can use all of those options

I'm using Ubuntu so I have all the functions you required.
I tried seeing wget but not able to develop the script :frowning:

I was pointing out that your posts' specs are not consistent. What are the extensions? What are the file names ending on?

Try (untested)

ftp host <<< get .../TEKNONET/60468/0000060468$(date -d-1day +%Y%m%d)N001.CDR

Inside remote dir... /TEKNONET/60468/ there are all .Zip files containing a CDR file. These zip files have the timestamp on them in the format 0000060468yyyymmddN001.Zip

Are both machines in the same time zone.
Will there be a file on weekends and statutory holidays.
How much time is there between the time the file is available and the attempt to retrieve it.
Will a checksum be available.

The time zone is the same.
The only thing is that in the remote dir the Zip file regarding every monday is not given, so whatever script you are going to write, it must have an option which tells that Monday's files must not be downloaded as they dont exists.
The scheduling of this script must be at least from 04.50 am to 08.30am you choose.

It doesn't work quite like that. We help you write your script.

Ok guys I managed to do all in this way and It is all working with a crontab schedule.

#!/bin/bash

START=`echo $1 | tr -d _`;
TABLE_NAME=M"`date --date="$START" +%Y%m%d`";

echo "Tabella del mese: "$TABLE_NAME;

if [ -f "/var/lib/mysql/database_name/"$TABLE_NAME".frm" ]
then echo "Tabella esistente: OK"

else
echo "Tabella non esistente, la creo";
SQL="create table "$TABLE_NAME"
(WHSSID int not null,
Seriale int not null,
DataOra datetime,
Chiamante text,
Chiamato text,
Prefisso_num_chiamato int,
Descr_prefisso_chiamato varchar(50),
Durata_secondi int,
Costo_conversazione text,
Chiamata_urb_extraurb int,
Chiamata_voce_dati int,
Chiamata_intratwt int,
Prefisso_carrier_destinazione int);";
echo $SQL > /tmp/sql;
mysql -D database_name -u database_name -p_database_password < /tmp/sql;
fi

FILE="ftp://ftp_user:ftp_pass@host/go/directory/0000060468$(date -d-0day +%Y%m%d)N001.Zip";
wget -qq $FILE -P /tmp;
unzip -qq -o -u /tmp/0000060468$(date -d-0day +%Y%m%d)N001.Zip -d /tmp;
SQL="load data local infile '/tmp/0000060468$(date -d-0day +%Y%m%d)N001.CDR' INTO TABLE $TABLE_NAME
FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n'";
echo $SQL > /tmp/sql;
mysql -u database_name -pdatabase_password --local-infile database_name < /tmp/sql;
rm -rf /tmp/*

exit