Two Quick Gems to Round Out the Week

21 Jan 2011 – Denver, CO

Good week, lots accomplished. 60 minutes until I knock off for pizza and beer, here’s something I thought up as I started to wind down for the week and knocked up this Friday afternoon. I’ve been thinking about an automated file-storage solution in ruby for a while now. This afternoon I thought “What if I wanted to backup some of my files to an internal (enterprise) file server ?”. The two modules I’m about to detail are the result of that thought process.

The first thing we need to do is gather the details of all the files we need. SQLite3 is probably suitable enough. All we need to do is accept some parameters and build a nice little cache of file information. Source code here (you can tell I was getting hungry by the name). The gem is built here.

To use it is very simple, lets say we want to build a quick cache of all the pdf files in our home directory:

 
  require 'rubygems'
  require 'hungrynoodle'
  h = HungryNoodle.new('/Users/snaggled', '\.pdf')
  h.find_and_insert

We can query that via the following:

   
  h.basenames

Secondly, I picked the internal IBM Cattail
as a file storage destination because I like it – it’s a cool system, its REST based, etc, etc.

A quick gem to facilitate uploading and querying files, and we’re in a position to back up files we care about.

Heres a quick and dirty script to facilitate:

 
  require 'rubygems'
  require 'cattail'
  require 'hungrynoodle'

  if ARGV.length != 4
    p "Usage: cattail <directory, e.g. '.'> 
                      <extension to be stored, e.g 'ppt'> 
                      <cattail login> 
                      <cattail passwd>"
    exit(-1)
  end

  dir = ARGV[0]
  extension = ARGV[1]
  login = ARGV[2]
  passwd = ARGV[3]

  # establish connections 
  hungrynoodle = HungryNoodle.new(
    dir, 
    "\.#{extension}", 
    "#{extension}.db", 
    "#{extension}.txt")
  cattail = Cattail.new(login, passwd)

  hungrynoodle.find_and_insert

  local_files = hungrynoodle.basenames
  remote_files = cattail.list
  diff = local_files - remote_files

  diff.each do |file|
  	file_record = hungrynoodle.file(file)
  	p "Uploading #{file_record[0][3]}"
  	cattail.insert(file_record[0][3])
  end

And that’s it. Back to our earlier example:

 
  ruby cattail.rb /Users/snaggled pdf login passwd

That will grab all the PDFs in our home directory and make sure they are all sent to the backup server. I’ll let that bad boy run and knock off for some pizza and beer, let the weekend begin.

Todays musings in accompaniment to this sweet album:


Apparat - Things to be Frickled