View Full Version : Automate FTP Tasks

18th November 2012, 01:47 AM
Automate repeative FTP tasks

Everyone with a homepage, specialy rpm packagers, will face the fact the he's uploading the files over and over again to the same place so others can review them.
The most work is the mouse clicking, moving and make sure you havent changed folders in the GUI FTP in between for other files, etc...
To ease this task, as its pretty annoying, i wanted to write a script. :)

In this scenario we're uploading all files from a local path to a new created remote path.
As a psudo code: upload $(ls $HOME/Projects/) to myftp.com/files/20121118

One of the major issues that usualy stops one from doing taks automaticly, is when one needs to login.
For that reason we're using ~/.netrc which simplyfies this task alot!

The main trick is to (i dont know the tecnical term for this) counter-pipe commands.
I had seen that trick in the use with fdisk, so i assumed it'll work with ftp just alike.

This script needs to be edited at least once before it works for you.
Simply have a closer look at the Variables section.

To customize your FTP tasks, simply change the lines between the REMOTE_SCRIPT's.

# Description: Uploads files in local dir to a sub
# folder ($today) in the remote dir
# Date created: 2012.11.17
# Date changed: 2012.11.18
# Written by: Simon A. Erat, erat . simon gmail . com
# Variables
today=$(date +'%Y%m%d') # eg: 20121117
plocal=$HOME/Projects # Local Path
premote=/files # Remote Path
user="user" # Your FTP loginname
password="password" # Your FTP password
# Prepare auto-connect
[ "" = "$(grep $myurl $HOME/.netrc)" ] && \
echo "machine $myurl login $user password $password" >> $HOME/.netrc && \
chmod 600 $HOME/.netrc
# Connect & Execute
echo -e "\n\tUploading $plocal/* to\n\t$myurl$premote/$today\n"
ftp $myurl $port <<REMOTE_SCRIPT
lcd $plocal
mkdir $premote/$today
cd $premote/$today
mput *
exit 0

Hope this helps.

Note: Updates (if any) will be on my homepage (http://sea.hostingsociety.com/?p=Scripts&stc=sea-auto-ftp).