Ftp files using perl scripts
So there are probably a lot of "no really beautiful" code-fragments in my script. So if you have any constructive comments regarding my coding style or the way I use Perl, please tell me! Sign in Email. Forgot your password? Search within: Articles Quick Answers Messages. Tagged as Win2K. Stats Uwe Keim Rate me:. Please Sign up or sign in to vote. Download source files - Theoretically, a file needs to be uploaded only if: the local modification date is newer than the remote modification date, or the remote file doesn't exist at all In practice, the problem is that the FTP-Server has another timebase e.
Upload Algorithm The solution presented in this article handles this by storing the modification dates of the files to upload in a local database and does the comparison with these timestamps. In a nutshell, the "algorithm" works, for each file to upload, like this: Does an entry for the file exist in the database?
If no , upload it, create new recordset for this file, storing its path and modification date. If yes , compare the actual modification date of the file with the stored modification date in the database. If actual modification date is newer than the stored one, upload the file, store its modification date in the database. If actual modification date is older than the stored one, do nothing.
Starting from the second time, the script is executed, it works as described above. The following extra modules need to be installed: libnet libnet Script Configuration When Perl and all modules are installed, you need to configure the script. Each given directory is recursively processed i.
For each found file i. You can use here any valid expression, e. Usually this field is empty. Epilog If you have any suggestions, found errors or want to comment on that article or script, please write a comment to at the end of this article and I will answer it.
Uwe Keim. NET and C extensively, too. He has also teached programming to students at the local university. In his free time, he does climbing, running and mountain biking. Recently he became a father of a cute boy. Download now! First Prev Next My vote of 5 arif Jan Maverick Jun Jon Hulatt Oct Something wrong with this article?
Help us out by opening an issue or pull request on GitHub. To get in touch, send an email to perl. The information published on this website may not be suitable for every situation.
All work on this website is provided with the understanding that Perl. Neither Perl. A tour with Net::FTP Jul 13, by Thibault Duponchelle When we want to have a way to exchange files between machines, we often think about rsync, scp, git or even something slow and complex looking at you Artifactory and S3 , but the answer is often right in front of your eyes: FTP! In addition, FTP proved later to also require zero support.
I mean really zero maintenance! Download and install ftpd I decided to use pure-ftpd but there are some other good alternatives if you want. Server port: Using binary mode to transfer files. Thibault Duponchelle Thibault Duponchelle is a software developer.
Browse their articles. Please consult the documentation of Pod::usage for complete information. Lines 25 through 30 define some sensible default values for the command-line options not specified by the user.
This operator evaluates its left-hand side or lvalue. A false value causes its right-hand value or rvalue to be assigned to the lvalue. You can learn more about this operator by looking at the documentation for perlop. In order to execute the synchronization, the script collects information from two filesystem trees, one local and one remote.
I chose to collect all the information from both trees and perform the synchronization later. In my experience, this translates to cleaner and more maintainable code than if I tried to do everything in one pass. I'll store the data about the remote and local trees in two hashes, which I declare and initialize at lines 36 and Once the information is safely captured in the corresponding hashes, the code can focus on the differences and take the appropriate actions.
When matching the contents of the remote FTP site with those on the local copy, it is important to compare apples to apples. Because the filesystem layout is not always straightforward, I chose to compare relative pathnames. Therefore, before looking for the local files, I do a chdir to the path the user specified with the -l option, as seen on line After this step, I use find , as provided by File::Find, to traverse the local tree. Below is the code from line 46 to I will explain this code bit by bit, I promise.
I want to be absolutely sure that I am seeing path names that are consistent and relative to whatever the user specified with the -l option. At line 49, I prevent following symbolic links with the follow argument.
I do not want to deal with them mainly because of the infinite loops they can introduce when pointing upwards in the filesystem.
I do not use symlinks in my web site, so I see no problem with this. At line 50, finally, there's some interesting code. I specify it using the wanted argument, although there are various ways to invoke this function.
Please see perldoc File::Find for more information. The wanted argument requires a reference to a sub, which I define in lines 50 through Line 52 makes sure that I do not include the current directory. The find function, when used as shown, produces relative path names, such as. Both are legal forms of relative paths. To accomplish this, line 53 removes the leading. After this is done, a check is performed on lines 54 through 58 to see if the current pathname matches the ignored regular expression specified with -i.
By line 59 all tests have passed, so we should collect the information from this file. I'll collect three elements of information: the modification time or mdtm, the file size and the file type. The first two are collected with the stat call, which returns a list of values. It turns out that stat is a somewhat expensive operation, so it is better to do as few of them as possible. Therefore, the above construct causes only a single stat call, even when Perl's stat function is used more than once.
The same applies to the -x file operators that I use in lines 63 and 64 to assign a type to this file. I chose to connect to the FTP site after collecting the local file information for various reasons.
The first and most important is that, as a rule of thumb, whenever you write network-client code, keep your impact on the server to a minimum.
In this way, I spare the connection of the time it takes to collect the local information. Lines 74 through 77 handle the connection to the server, which in case of failure trigger the error message from line At line 80, authentication with the credentials passed in the command line is attempted, again producing a fatal error in case of failure.
A number of parameters for the FTP connection can be controlled by passing arguments to the new method. The definitive information is in the module's documentation. At line 81 we change the remote directory to whatever the user supplied.
0コメント