1753779 Members
7898 Online
108799 Solutions
New Discussion юеВ

Re: FTP Scripts

 
Ishmael
New Member

FTP Scripts

I wrote the following Script to ftp files from ServerA to ServerB. How do I modify the script to only transfer new files which I haven't copied.
#!/usr/bin/sh
{ echo "open ServerB
user user pass
hash
cd /directory/
mget *
close"
} | ftp -i -n -v 2>&1 | tee /tmp/ftplog
11 REPLIES 11
Patrick Chim
Trusted Contributor

Re: FTP Scripts

Hi,

Here is a script for you.

#!/bin/ksh

if [ ! -f filename.txt ]
then
cp /dev/null filename.txt
fi

mv filename.txt filename.prev

ftp -n < filename.curr
user
cd
nlist
quit
EOF

comm -23 filename.curr filename.prev > filename.diff

for i in `cat filename.diff`
do
ftp -n <user
cd
get $i
quit
EOF
done

This is just a simple script but it works (I have try it). You can further enhance it to fulfill your requirement !

Regards,
Patrick
Paula J Frazer-Campbell
Honored Contributor

Re: FTP Scripts

Hi

Before you start the ftp you will have to extend your script to do an ls -l on the dir, sending the output to a file.

E.G.

ls -l | awk @{print $9}' > /tmp/ftplist

Each time you do an ftp compare it with this file and if there are new files then ftp them, The script should then update it if there are new files, so that at next run time these files do not get sent again.


HTH

Paula


If you can spell SysAdmin then you is one - anon
Patrick Chim
Trusted Contributor

Re: FTP Scripts

Hi,

Sorry about that ! I've forgotten to rename the file after the testing.

By the way, your have to replace the 'filename.txt' with 'filename.curr'

Regards,
Patrick

Re: FTP Scripts

Hello,
everything looks nice especially the latest reply. But there is little weakness. Use -lL. The "L" looks for links. And I have a question:
why don't u use PERL?? If u do the last reply, you will run into trouble with awk. Awk (and sed) is restricted to 3000 values to be processed and about this number it will go unpredictible. I experienced this very fact and have been using PERL exclusively since.
The algorithm could look alike.
(:

per aspera ad astra
Ishmael
New Member

Re: FTP Scripts

Thanks,

Patrick, the comm command after the first ftp doesn't seem to be executed. Any idea why?
Patrick Chim
Trusted Contributor

Re: FTP Scripts

Hi,

Sorry for the late reply as I cannot login to the forum this morning as it show 'services unavailable'.

BTW, what error did you get ?

Did you submit the job by cron ? If so, you have to specific the full path of comm, i.e. /usr/bin/comm

Regards,
Patrick
Ishmael
New Member

Re: FTP Scripts

Hi Patrick,

Thanks for the response.
I ran the complete script from the command line and it gave the folliwng error :-
cat: Cannot open tempfile.diff: No such file or directory

tempfile.diff doesn't seem to be created by comm. But if I run the full comm command manully from cmd line it works fine.
I forced it run as /usr/bin/ksh and /usr/bin/sh but nothing works.
Patrick Chim
Trusted Contributor

Re: FTP Scripts

Hi,

Can you attach the script so that I can have a look ??

Does the file tempfile.diff create by you or other ? Do you have write permission on it ? How about if you specific all file with absolute path ?

Regards,
Patrick
Ishmael
New Member

Re: FTP Scripts

Script as requested.
#!/usr/bin/ksh
#set -x
cd /testdata/temp_data/test
if [ ! -f tempfile.curr ]
then
cp /dev/null tempfile.curr
fi

mv tempfile.curr tempfile.prev

ftp -n -v -i machineB << EOF > tempfile.curr
user login password
cd /IN/TEMP/
nlist c*
close
quit
EOF

/usr/bin/ksh comm -23 tempfile.curr tempfile.prev > tempfile.diff

for i in `cat tempfile.diff`
do
ftp -n machineB << EOF
user login password
cd /IN/TEMP/
get $i
quit
EOF
done