Wednesday, March 13, 2013

Automatic/periodic FTP download using cron jobs

I came across a situation where I had to download files from an FTP server every week. Initially I was doing it manually, but due to human errors I missed some data. I then realized it must be possible to automate the download.

I initially created a shell script to enable FTP download[2]. The script looks like:

#!/bin/bash
HOST='ftp.server.com'   # change the ipaddress accordingly
USER='username'   # username also change
PASSWD='password'    # password also change
ftp -inv $HOST<<EOF
quote USER $USER
quote PASS $PASSWD
bin
cd /move/to/remote/directory        
lcd "/local/directory/" 
mget filename*
cd /move/to/remote/directory2
lcd "/local/directory2/"
mget filename*     
bye
EOF

Using [1], I setup a cron job using the command:
crontab -e

The job entry format is pretty self-explanatory in the reference [1], and there are some commonly used job examples too.
I had to launch a job at the beginning of every week, so my entry in the file looks like:
0 10 * * 1 ~/ftp_download_script.sh
This line states that the script should be launched at 10am every monday.

References:
1. http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
2. https://blogs.oracle.com/SanthoshK/entry/automate_ftp_download_using_sh

No comments:

Post a Comment