#!/usr/bin/env ruby

class FilePurger
  PURGE_DIRECTORY="/home/pi/backup_directory"
  PURGE_EXTENSION=".tar.bz2"
  MAX_DAYS_BEFORE_PURGE=7
  LOG_FILE="/home/pi/file_purger.log"
  DRY_RUN=false
  
  def self.purge
    files_to_purge = find_files_to_purge(MAX_DAYS_BEFORE_PURGE)
    files_to_purge.each { |filepath| perform_logged_purge(filepath)}
  end

  private
  def self.find_files_to_purge(max_days_before_purge)
    glob_pattern = File.join(PURGE_DIRECTORY, "*" + PURGE_EXTENSION)
    Dir.glob(glob_pattern).select{|path| File.mtime(path) < (Time.now - (60 * 60 *24 * max_days_before_purge))}
  end

  def self.perform_logged_purge(filepath)
    timestamp = Time.now.strftime("%d/%m/%Y %H:%M")
    log_msg = "#{timestamp}: Deleting #{filepath}"
    File.open(LOG_FILE, "a") { |f| f.puts log_msg }
    File.delete(filepath) unless DRY_RUN
  end
end

# Entrypoint
if __FILE__ == $0
  FilePurger.purge
end

#!/usr/bin/env ruby

# Searches a given directory for files of a given extension and purges (deletes) them if they're old than x days.
class FilePurger
  def self.purge(purge_directory, purge_extension, purge_age, purge_log)
    files_to_purge = find_files_to_purge(purge_directory, purge_extension, purge_age)
    files_to_purge.each { |filepath| perform_logged_purge(filepath, purge_log)}
  end

  private
  def self.find_files_to_purge(purge_directory, purge_extension, purge_age)
    glob_pattern = File.join(purge_directory, "*" + purge_extension)
    Dir.glob(glob_pattern).select{|path| File.mtime(path) < (Time.now - (60 * 60 *24 * purge_age))}
  end

  def self.perform_logged_purge(filepath, purge_log)
    timestamp = Time.now.strftime("%d/%m/%Y %H:%M")
    log_msg = "#{timestamp}: Deleting #{filepath}"
    File.open(purge_log, "a") { |f| f.puts log_msg }

    # export PURGE_DRY_RUN=true
    File.delete(filepath) unless ENV['PURGE_DRY_RUN']
  end
end

# Entrypoint
if __FILE__ == $0
  if ARGV.length == 4
    FilePurger.purge(ARGV[0],ARGV[1], ARGV[2].to_i, ARGV[3])
  else
    raise "Usage: file_purger.rb purge_directory purge_extension purge_age purge_log"
  end
end

Refactor FilePurger to take arguments at runtime
Refactor FilePurger to take arguments at runtime

The FilePurger class now accepts arguments from the command-line at
runtime, rather than having the configuration options hard-coded into
the file. This allows the same ruby class to be used by multiple
callers, adhering to DRY principles.
