pFTP README

This is a script which automates the transfer via ftp of files from a mirror site at one location to a site at some other location. A good example to keep in mind is the one for which I wrote the script. I maintain several web sites and keep mirrors of them on my Mac. I create new files and update old ones on my Mac and then ftp the results to the server.

By making this script into a MacPerl Droplet, I can ftp the files just by dropping them onto the Droplet. I usually create/edit the files in BBEdit and using my "Open File With PlugIn" I can ftp the file associated to the front window with a single keystroke.

The Droplet is smart enough to figure out where to put the file: the mechanism is as follows. Somewhere on the Mac is a folder (for this example let us call it 'mirror'). On a server somewhere is a directory (for this example let us call it 'destination'). The files and folders inside 'mirror' have the same names and relative locations as the files and folders inside 'destination'. We say the 'mirror' is linked to 'destination'. In principal, several different folders on the Mac could be linked to the same directory on a server.

This first thing the Droplet does is collect an array of full path names to all the files. It will resolve aliases and recursively list folders to do this, so if you drop a folder onto the Droplet, all the files inside will be ftp'ed. This also means that you can create infinite loops if you wish - just put an alias to a folder inside the folder and drop the folder.

Next the Droplet locates the folder name 'mirror' in the full path name, grabs the part after 'mirror' and adds it to 'destination' to get a full path name to the destination. From this it extracts the destination directory. Then it ftp's the file and repeats the process.

There are several wrinkles in this mechanism which we now explore. First, I really have several web sites and at various times they have resided on different servers, so the script will manage this. This involves two arrays in the script which the user must set '@MAC_path' and '@server_paths'. In the example, '@MAC_path' has only one entry and that is 'mirror:'. Then @server_paths must also have one entry which is 'destination/' (The colon and slash reflect the file system differences between the MacOS where 'mirror' resides and the UnixOS where 'destination' resides.) It would also be a better example if 'destination' had a longer path name since it is rare that directories into which you wish to ftp lie at root level. While 'destination/' must be a full path name, there is no need for 'mirror:' to be. If you have more than one initial site, then add entries to the '@MAC_path' and '@server_paths' arrays until you have entered all of them. The locations in the two arrays are used to determine which Mac folder is linked to which server directory.

A full path name for the 'destination' directory is not enough to specify the location since various directories may reside on different servers. Each server has an ip number and we need to link these ip numbers to the 'destination' directories. This is done by a two array collection. The array '@ip_index' is an array of numbers and the array '@ip_numbers is an array of ip numbers. The numbers in '@ip_index' are linked to 'destination' by their position. The number corresponds to the element in @ip_numbers' which is the ip number for the server. This two part scheme is used because of how ftp works.

A connection must be made to each server to begin the ftp process, but once the connection is made, it can be used for any directories on the server. Hence the script loops through the '@ip_numbers' array, opening each ip number only once. Then every file destined for that server is ftp'ed.

This brings us to the final wrinkle. In order to open a connection to a server a name and a password are needed, hence the arrays '@login_names' and '@passwords'. The entries in these arrays are linked by index to the '@ip_numbers' array and are the corresponding login name and password for an account on the server that has the requisite permissions for the 'destination' directory.

Copy the script from here, fill in the arrays discussed above and off you go.

A UNIX version of this script can be had by locating the colons and replacing them by slashes as needed. Some of the error messages may be a bit weird if not altered. You will also need to adjust the shebang line.