Sha256: ce1b805ac513d22c9771578b5e2d71968d7311589cab629afa4688d8a7b821c3

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

require 'fileutils'

require 'yolo_backup/storage_pool/file'

class YOLOBackup::StoragePool::File::Cleaner
  attr_reader :storage_pool, :server

  def initialize(storage_pool, server)
    @storage_pool = storage_pool
    @server = server
  end

  def cleanup
    unused_backups = all_backups - required_backups
    unused_backups.each do |unused_backup|
      path = "#{server_path}/#{unused_backup.iso8601}"
      p path
      FileUtils.rm_r path, :force => true
    end
    unused_backups
  end

  private
  def required_backups
    required_backups = {}
    all_backups.each do |date|
      {
        :year => :yearly,
        :month => :monthly,
        :week => :weekly,
        :day => :daily,
        :hour => :hourly
      }.each do |accuracy, schedule_option_name|
        keep_count = server.rotation.send("#{schedule_option_name}").to_i
        next if keep_count.nil? || keep_count.zero?
        if required_backups[accuracy].nil?
          required_backups[accuracy] = [date]
        elsif !same_date(date, required_backups[accuracy].last, accuracy)
          required_backups[accuracy] << date
        end
        if required_backups[accuracy].length > keep_count
          required_backups[accuracy].shift
        end
      end
    end
    required_backups = required_backups.values.flatten
    required_backups << server.latest_backup
    required_backups.uniq
  end

  def all_backups
    backups = []
    Dir.chdir(server_path) do
      Dir.entries('.').sort.each do |file|
        next if ['.', '..'].include?(file)
        next unless ::File.directory?(file) && !::File.symlink?(file)
        backups << Time.parse(file)
      end
    end
    backups
  end

  def server_path
    storage_pool.path.interpolate(:hostname => server)
  end

  def same_date(time1, time2, accuracy)
    end_of = "end_of_#{accuracy}"
    time1.send(end_of) == time2.send(end_of)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yolo_backup-0.0.0 lib/yolo_backup/storage_pool/file/cleaner.rb