FTP a file on Hourly basis

Hi,

I have to upload a file test_201105281100.txt to a ftp location.

The files will be created on hourly basis like test_201105281100.txt, test_201105281200.txt & so on.

After a file is uploaded successfully, I need to rename the file as test_201105281100.success & if it is not uploaded successfully then rename it as test_201105281100.failure.

Also I need to delete the older files ( I may delete all the files or only .success files). After 12 AM on 30th May, I want to delete all the files that were created on 28th.

I want the script to run as a cron job.

Please help.

Note: I will be able to implement & test the solution that you suggest on Monday morning (more than 36 Hours from now).

Thanks,
Sunil

I didnt test this. Hope this will work. I have given just a skeleton you need some work to make this work.

upload()
{
  ftp $hostname
  user $user_name
  password $password
  put $file_name 
  bye
}
checkupload()
{
  ftp $hostname
  user $user_name
  password $password
  get ${file_name} ${file_name}.remote
  bye

loc=`wc -c ${file_name}`
rem=`wc -c ${file_name}.remote`

if [ $loc -eq $rem ]
then
  echo "file uploaded successfully.."
  mv $file_name ${file_name}.success
else 
 echo "file uploaded failed.."
   mv $file_name ${file_name}.failure
fi
}

  
# assign values for below variables
file_name=
user_name=
password=
hostname=

upload
checkupload
1 Like

Hi Friends,

I need some more solutions.

Kindly help at the earliest.

Thanks,
Sunil

Hi,

I have set the variables as follows.

bash-2.03# echo $hostname
97.253.98.14
bash-2.03# echo $password
postgres
bash-2.03# echo $file_name
ONLINE_WAIVER_2011052816.txt
bash-2.03# echo $user_name
postgres
bash-2.03#

I have created the 2 scripts as follows.

bash-2.03# cat f.sh
#!/bin/bash
source upload.sh
upload
bash-2.03#

and

bash-2.03# cat upload.sh
#!/bin/bash
function upload(){
  ftp $hostname
  user $user_name
  password $password
  put $file_name
  bye
}
bash-2.03#

But on running the script, I am not able to login to FTP automatically.

bash-2.03# ./f.sh
ftp> ls
Not connected.
ftp>

Thanks,
Sunil

you have to use

ftp -inv $hostname
user $user_name $pass_word
1 Like

Hi,

Still not able to login to FTP from the script.

bash-2.03#
bash-2.03# echo $hostname
97.253.98.14
bash-2.03# echo $user_name
postgres
bash-2.03# echo $pass_word
postgres
bash-2.03# echo $file_name
ONLINE_WAIVER_2011052816.txt
bash-2.03#
bash-2.03#
bash-2.03#
bash-2.03# cat f.sh
#!/bin/bash
source upload.sh
upload
bash-2.03#
bash-2.03#
bash-2.03#
bash-2.03# cat upload.sh
#!/bin/bash
function upload(){
ftp -inv $hostname
user $user_name $pass_word
#Password $password
put $file_name
  bye
}
bash-2.03#
bash-2.03#
bash-2.03# ./f.sh
ftp> ls
Not connected.
ftp>

Thanks,
Sunil

function upload(){
  ftp -inv $hostname <<EOF
  user $user_name $pass_word
  put $file_name
  bye
EOF
}

This will work. You wrote the function wrong.

1 Like

Hi,

I made the changes. but now it gives Not connected message 2 times while connecting to FTP. The User name & password are correct as I am able to connect to FTP manually.

bash-2.03# echo $hostname
97.253.98.14
bash-2.03# echo $user_name
postgres
bash-2.03# echo $pass_word
postgres
bash-2.03# echo $file_name
ONLINE_WAIVER_2011052816.txt
bash-2.03#
bash-2.03#
bash-2.03# cat f.sh
#!/bin/bash
source upload.sh
upload
bash-2.03#
bash-2.03#
bash-2.03# cat upload.sh
#!/bin/bash
function upload(){
  ftp -inv $hostname <<EOF
  user $user_name $pass_word
  put $file_name
  bye
EOF
}
bash-2.03#
bash-2.03# ./f.sh
Not connected.
Not connected.
bash-2.03#

Logging to FTP manually.

bash-2.03# ftp 97.253.98.14
Connected to 97.253.98.14.
220 L028XRAPDN FTP server ready.
331 Password required for postgres.
230 User postgres logged in.
ftp>
 
The Username & password are saved in .netrc file.
 

bash-2.03# cat /RAPhome/postgres/.netrc
machine 97.253.98.14 login postgres password postgres
[/code]

Thanks,
Sunil

It is working fine for me.Please check whether your IP is correct and try to FTP manually for that IP.

user@host2> (/home/user) $ cat test.sh
#!/bin/bash
function upload(){
  ftp -inv 100.155.34.123 <<EOF
  user user pass2000
  bin
  bye
EOF
}


user@host2> (/home/user) $ cat f.sh
#!/bin/bash

source test.sh

upload

user@host2> (/home/user) $ ./f.sh
Connected to 100.155.34.123.
220 tonga FTP server ready.
334 Using AUTH type GSSAPI; ADAT must follow
GSSAPI accepted as authentication type
GSSAPI error major: Unspecified GSS failure.  Minor code may provide more information
GSSAPI error minor: No credentials cache found
GSSAPI error: initializing context
GSSAPI authentication failed
504 AUTH KERBEROS_V4 not supported.
KERBEROS_V4 rejected as an authentication type
331 Password required for user.
230 User user logged in.
200 Type set to I.
221-You have transferred 0 bytes in 0 files.
221-Total traffic for this session was 332 bytes in 0 transfers.
221-Thank you for using the FTP service on tonga.
221 Goodbye.
user@host2> (/home/user) $

1 Like

Hi,

I am able to FTP the file if I hard code the filename. But if I use $file_name instead of filename, the file is not uploaded on FTP.

bash-2.03# cat upload.sh
#!/bin/bash
function upload(){
  ftp -inv 97.253.98.14 <<EOF
  user postgres postgres
  bin
  put ONLINE_WAIVER_2011052816.txt
  bye
EOF
}
bash-2.03#
bash-2.03#
bash-2.03# ./f.sh
Connected to 97.253.98.14.
220 L028XRAPDN FTP server ready.
331 Password required for postgres.
230 User postgres logged in.
200 Type set to I.
200 PORT command successful.
150 Opening BINARY mode data connection for ONLINE_WAIVER_2011052816.txt.
226 Transfer complete.
local: ONLINE_WAIVER_2011052816.txt remote: ONLINE_WAIVER_2011052816.txt
17 bytes sent in 0.00017 seconds (100.62 Kbytes/s)
221-You have transferred 17 bytes in 1 files.
221-Total traffic for this session was 404 bytes in 1 transfers.
221-Thank you for using the FTP service on L028XRAPDN.
221 Goodbye.
bash-2.03#
bash-2.03# echo $file_name
ONLINE_WAIVER_2011052816.txt
bash-2.03#
bash-2.03#
bash-2.03# cat upload.sh
#!/bin/bash
function upload(){
  ftp -inv 97.253.98.14 <<EOF
  user postgres postgres
  bin
  put $file_name
  bye
EOF
}
bash-2.03#
bash-2.03# ./f.sh
Connected to 97.253.98.14.
220 L028XRAPDN FTP server ready.
331 Password required for postgres.
230 User postgres logged in.
200 Type set to I.
(local-file) (remote-file) 221-You have transferred 0 bytes in 0 files.
221-Total traffic for this session was 201 bytes in 0 transfers.
221-Thank you for using the FTP service on L028XRAPDN.
221 Goodbye.
bash-2.03#

Thanks,
Sunil

user@host> (/home/user) $ a=10
user@host> (/home/user) $ vi test.sh
user@host> (/home/user) $
user@host> (/home/user) $
user@host> (/home/user) $ ./test.sh

user@host> (/home/user) $ echo $a
10
user@host> (/home/user) $

Oh man you made mistake accessing evn variable inside your script.

First when you set a variable at your prompt, like i did a=10, then the same won't be available inside the script you are running.

The prompt shell is a different process and the script you are running is a different process.
If you are planning to share it, you have it to make it as environment variable by using export.

Check the output i have posted, i was not able to access a inside test.sh, but in the prompt i am getting it

1 Like

Hi,

Thanks, I will try this solution tomorrow & then revert to you.

Regards,
Sunil

---------- Post updated 05-31-11 at 10:11 AM ---------- Previous update was 05-30-11 at 08:13 PM ----------

Hi,

Thanks.

I can now access the exported $file_name variable from inside the script.

Thanks,
Sunil