Batch FTP with NcFTP and Ruby

I just experienced the weirdest thing ever on a shared hosting space (avoid One.com). For some reason folders I knew to be there (accessible through http) were not showing up in the FileZilla directory browser. Files I just had to download due to a hosting move.

Funny thing is I could browse to these folders and download their contents if I entered their paths explicitly in the remote address bar in FileZilla. With that in mind and the fact that I knew the “missing” directories were numbered between 1 and 10000 I could get the missing directories if I could setup some kind of command line FTP batch script.

Another issue is that I already had most of the directories due to a backup I had done earlier so I didn’t want to bother with the stuff I already had in the batch script.

After some searching I found NcFTP which seemed to be the perfect tool for the job, especially the NcFTPGet utility.

Simply doing apt-get install ncftp took care of the installation.

It’s time for some shell script generating action with Ruby:

f = File.new("ftpdown.sh",  "w+")
outs = ""
(1..10000).each do |n|
	n = n.to_s
	unless File.directory? n
		outs += "ncftpget -R -u username -p password ftp.domain.com /var/www/domain/images/ads /images/ads/#{n};\n"
	end
end
f.write(outs)

So we create a new file called ftpdown.sh and then we loop through the numbers 1 to 10000 and check if the number in question is a directory. If it is not a directory (ie we don’t have it already) we put the proper command in the shell script.

After that we simply run ftpdown.sh and voila we get all the folders and their contents that we were missing.


Related Posts

Tags: , , ,