Wednesday, August 4, 2010

how to automatically copy a file through ftp

I had the recent need to move a file on a daily base from a pc to an area of a website.
Here is how:
 
1. On your linux machine where the files are generated create a .netrc_template file containing the commands for the ftp:
machine ftp.xxx login xxx password xxx macdef
init
lcd /localdir
cd /webdir
prompt off
bin
mput ILFILE
quit
<here you need an empty line>

2. Make a small script to generate the file name, the .netrc and to run the task on a daily base 
#!/bin/sh                                                                       
NOME=`/bin/date +"ilFile%Y%m%d.pdf"`
cat /home/alex/.netrc_tmpl |sed -e "s%ILFILE%$NOME%">/home/alex/.netrc
chown alex /home/alex/.netrc
chmod 600  /home/alex/.netrc
ftp ftp.xxx

3. add the script to a crontab
#crontab -l
0 8 * * * /home/alex/my.cron >/dev/null 2>&1

No comments:

Post a Comment