On Thu, Apr 17, 2008 at 11:19 AM, Clement Ow
<clement.ow / asia.bnpparibas.com> wrote:
> Jesù¸ Gabriel y GaláÏ wrote:
>
>  >
>  > If you want to check the actual modification date of the file,
>  > I think File.stat can help on that, or I don't know if File.find has
>  > an option to search
>  > for files based on date.
>  >
>  > Hope this helps.
>  >
>  > Jesus.
>
>  Are you using a hash to contain the file paths that you want to delete?
>  It pretty much works about the same as the array that I have created eh?
>  btw, is it possible to attach your script so that I can use it for
>  reference? Thanks!

I'm using the "main" gem to process input parameters. The folders
variable is an array, I think.
The full script (it uses an erb template to compose the email report,
which I don't think it's interesting):

require 'date'
require 'find'
require 'simplemail'
require 'main'
require 'erb'
# the next two are just for the number_to_human_size method
require 'action_controller'
require 'action_view'
include ActionView::Helpers::NumberHelper


TEMPLATE_FILE = File.join(File.dirname(__FILE__), "deletelogs_template.erb")

main {
  description <<-DESC
    Deletes or gzips jhub log files older than the specified dates,
searching the specified directories
    recursively. The files should match this regexp
/^(\d\d\d\d-\d\d-\d\d).*\.log(\.gz)?$/
DESC

  option("zip", "z") {
    argument :required
    description "Zip files older than the specified number of days"
    cast :int
  }
  option("delete", "d") {
    argument :required
    defaults 7
    description "Delete files older than the specified number of days.
If --zip option is specified, only delete the files that are in
between
both dates"
    cast :int
  }
  argument("directories") {
    arity -2
  }

 def disk_usage
    `df -h`
  end

  def run
    folders = params[:directories].values
    have_to_zip = params[:zip].given?
    zip = params[:zip].value
    if have_to_zip
      zip_date = DateTime.now - zip
    end
    delete = params[:delete].value
    delete_date = DateTime.now - delete

    usage_before = disk_usage
    regexp = Regexp.compile(/^(\d\d\d\d-\d\d-\d\d).*\.log(\.gz)?$/)

    fileData = Struct.new(:name, :size)
    deleted_files = []
    zipped_files = []

    folders.each do |folder|
      Find.find(folder + "/") do |file|
        match = regexp.match(File.basename(file));
        if match
          file_date = DateTime.parse(match[1])
          size = File.stat(file).size
          if delete_date > file_date
            deleted_files << fileData.new(file,size)
            File.delete(file)
          elsif have_to_zip && zip_date > file_date && !match[2]
            zipped_files << fileData.new(file,size)
            `gzip -f #{file}`
          end
        end
      end
    end
    usage_after = disk_usage
    report = ERB.new(File.read(TEMPLATE_FILE), nil, "%<>")
    report = report.result(binding)
    SimpleMail.deliver_simple('email address', 'email address',
'Delete old log files', report)
  end
}

SimpleMail is just a class that inherits ActionMailer and has the smtp
info and a simple method to compose the email.
Any comment on how to make this more efficient or better is appreciated...

Jesus.