LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   AIX (http://www.linuxquestions.org/questions/aix-43/)
-   -   Setting up a cron for transferring a file through FTP at 5 PM daily (http://www.linuxquestions.org/questions/aix-43/setting-up-a-cron-for-transferring-a-file-through-ftp-at-5-pm-daily-934622/)

arjundey 03-15-2012 01:07 PM

Setting up a cron for transferring a file through FTP at 5 PM daily
 
Hi Friends,

I am fairly new to Unix/Shell Scripting & I need your help badly & urgently.

I want to setup a cron that will transfer a file from one server to another through FTP everyday at 5 PM (system time).

Can anyone help please ?

Thanks in advance.

evilputty 03-15-2012 01:09 PM

is this another linux server? rsync or sftp?

arjundey 03-15-2012 01:10 PM

It is another unix server.
The transfer is from a production to a development box.

evilputty 03-15-2012 02:08 PM

check out http://troy.jdmz.net/rsync/index.html for rsync.

but if you want just to ftp something

Code:

#!/bin/bash

filename="/home/<username>/thefile"
hostname="ftp.myhost.com"
username="username"
password="password"
ftp -un $hostname <<EOF
quote USER $username
quote PASS $password

binary
put $filename
quit
EOF

Code:

$ crontab -e

1 17 * * * /path/to/script


Sydney 03-15-2012 02:18 PM

The script is from a someone else's page I can not yet cite the url due to forum restrictions.
#!/bin/sh
HOST='ftp.users.qwest.net'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
exit 0



Once you have your script made and executable you must schedule it to run with cron. Every user has their own crontab please do not use the system crontab. Log in to the user who has access to the file and will be running your script. Make sure they can execute it.

Backup their crontab:
user@localhost:~$ crontab -l > cron.bak
Open the file you just made:
user@localhost:~$ nano cron.bak
Add the lines
# FTP file to other server at 17:01 daily.
01 17 * * * /Path/to/my/shell/script
Save the file.
Load it into your crontab with:
user@localhost:~$ crontab cron.bak

lithos 03-15-2012 03:35 PM

Hi,

just a reminder on FTP transfer, to be sure of successful operation use LFTP, which will "automagically" resume or reconnect broken connections until transfer succeed.

good luck

arjundey 03-16-2012 03:06 AM

Thanks everyone for your valuable inputs. Really helpful !

Just wanted to know if I can have a cron setup in a single line itself rather than calling a script from cron.

something like :

$ crontab -e
1 17 * * * "ftp -n $hostname /username /$password /$current_dir get file_name

Is anything like this possible ? If yes, please give me an example with a proper syntax.

lithos 03-16-2012 05:19 AM

Hi arjundey

actually I know only for LFTP that it is possible, like:
Code:

# crontab -e
1 17 * * * "/usr/bin/lftp -c "open $hostname && user $username $password && cd $current_dir && get -c $file_name"

The marked && are used for executing the command in order when one "exits" (let's say cd $curr_dir) then another executes (get -c $filename)


good luck

arjundey 03-16-2012 05:46 AM

I have written this script for a FTP transfer, but I am not able to get through to FTP connection without it prompting for username & pwd.

Code:

#!/bin/ksh

sdir="/var/mqm/tmp/pso/"
filename="pso_whse_orders_inout"
hostname="n0aif01"
username="evaxd4"
password="march2012"

ftp -n $hostname <<EOF
quote USER $username
quote PASS $password

binary
cd $sdir
get $filename
quit
EOF

mv -f $filename $filename.`date '+%Y%m%d'`


druuna 03-16-2012 05:56 AM

Hi,

Try changing this part:
Code:

quote USER $username
quote PASS $password

to:
Code:

user $username $password

You might also want to change this:
Code:

ftp -n $hostname <<EOF
to:
Code:

ftp -in $hostname <<EOF
Hope this helps.

EDIT: DO NOT edit your post(s) after an answer has been given!!!

arjundey 03-16-2012 06:21 AM

Thanks Druuna. FTP connectivity works now, but have 2 more issues to resolve with my script.

I have written this script for a FTP transfer, but I am not able to get the files in the required directory even after the cd command before connecting to FTP. I'm running this script from a different directory & I want the file to come into a specific directory. I assumed to change the working directory before connecting to FTP, but its not working. The file is coming to the same directory where I am running the script from. Please help ASAP.

Also, the script doesn't purge the files which are more than 3 days old. Pls help on this as well.

Note : Both are highlighted in BOLD below.

Code:

#!/bin/ksh

sdir="/var/mqm/tmp/pso/"
filename="pso_whse_orders_inout"
hostname="n0aif01"
username="evaxd4"
password="march2012"

`cd $sdir`
ftp -in $hostname <<EOF
quote USER $username
quote PASS $password

binary
cd $sdir
get $filename
quit
EOF

mv -f $filename $filename.`date '+%Y%m%d'`

find /home/evaxd4/FTP_CRON/ -name "pso_whse_orders_inout.*" -mtime +3 -exec rm -f {} \;

In the last command. I want to purge older than 3 days files.

Like for example, there are the following files in the directory :
pso_whse_orders_inout.20120312
pso_whse_orders_inout.20120313
pso_whse_orders_inout.20120314
pso_whse_orders_inout.20120315
pso_whse_orders_inout.20120316

Then the directory will be left over with the last 3 days' files :
pso_whse_orders_inout.20120314
pso_whse_orders_inout.20120315
pso_whse_orders_inout.20120316

Any ideas ?

druuna 03-16-2012 06:41 AM

Hi arjundey,

You can change local directory from within an ftp session with lcd directoryname:
Code:

ftp -in $hostname <<EOF
quote USER $username
quote PASS $password

binary
cd $sdir  # this changes directory on the remote server
get $filename
lcd $somedir  # this changes directory on local server
get $otherfilename
quit
EOF

Have a look at the ftp man page for other options.

About the find command: Any errors? Are you searching the correct directory? The command shown is basically correct.
Use the stat command to check if the mtime is what you expect it to be (the Modify line):
Code:

stat pso_whse_orders_inout.20120312
BTW: About you editing a previous post and trying to restore it: Use the Last edited by arjundey link. It shows your edits and also gives you the possibility to change back to a previous version.

Hope this helps.

arjundey 03-16-2012 07:30 AM

Hi Druuna,

Thanks for your suggestion on editing a post. It will take some time to learn the features of this forum.

The lcd in the middle of a FTP session is only changing the directory in the server where I have connected to.
I want the script to get a file into a particular directory after the FTP transfer.

For example, there are 2 directories : a & b.
I am running this script from directory "a", but I want the file to come to directory "b" on the same machine after the FTP transfer (from a different unix machine). How do I do this ?

Regarding the find command, yes the directory listed there is correct & also I have created the files in such a way that there is the same filename with just the date different.
Code:

/home/evaxd4/FTP_CRON$ ls -ltr

-rwxrwxrwx    1 evaxd4  mqm            379 Mar 16 04:04 auto_script.sh
-rw-r--r--    1 evaxd4  mqm              0 Mar 16 05:52 pso_whse_orders_inout.20120315
-rw-r--r--    1 evaxd4  mqm              0 Mar 16 05:53 pso_whse_orders_inout.20120314
-rw-r--r--    1 evaxd4  mqm              0 Mar 16 05:53 pso_whse_orders_inout.20120313
-rw-r--r--    1 evaxd4  mqm              0 Mar 16 05:53 pso_whse_orders_inout.20120312
-rw-r--r--    1 evaxd4  mqm        2110264 Mar 16 06:04 pso_whse_orders_inout.20120316

The command is not working :
Code:

/home/evaxd4/FTP_CRON$ find /home/evaxd4/FTP_CRON/ -name "pso_whse_orders_inout.*" -mtime +3 -exec rm -f {} \;

/home/evaxd4/FTP_CRON$ ls
auto_script.sh                  pso_whse_orders_inout.20120313  pso_whse_orders_inout.20120315
pso_whse_orders_inout.20120312  pso_whse_orders_inout.20120314  pso_whse_orders_inout.20120316

stat command that you gave above is not working.
Code:

stat pso_whse_orders_inout.20120312
ksh: stat:  not found.


evilputty 03-16-2012 09:57 AM

Quote:

Originally Posted by FTP Manpage
get remote-file [local-file]
Retrieve the remote-file and store it on the local machine.
If the local file name is not specified, it is given the same
name it has on the remote machine, subject to alteration by
the current case, ntrans, and nmap settings. The current
settings for type, form, mode, and structure are used while
transferring the file.

change this line:
Code:

get $filename

to

get $filename /location/to/localfile


arjundey 03-16-2012 11:49 AM

Thanks a ton evilputty. It worked great. :-)

Any idea on the command for removal of files older than 3 days ?
The command I used above hasn't worked. The files are not getting purged. Pls help.


All times are GMT -5. The time now is 10:10 PM.