Sha256: 81406d78edf72434e2138df7f15ed8296e23f8a8322f12e4fba7d94798a28d81

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

require 'yolo_backup/helper/log'
require 'yolo_backup/backup_runner/backend/rsync'

module YOLOBackup
  class BackupRunner
    class Job
      include Helper::Log

      OPTIONS = %w{server verbose}

      OPTIONS.each do |option|
        attr_accessor option
      end

      alias_method :verbose?, :verbose

      def initialize(options)
        OPTIONS.each do |option|
          send("#{option}=", options[option]) if options[option]
        end
      end

      def start
        if backup_required?
          log "Latest backup (#{server.latest_backup}) is older than maximum backup age (#{maximum_backup_age})" if verbose?
          log "Starting backup of #{server}"
          backend.start_backup
          log "Backup completed" if verbose?
          log "Cleaning up old backups" if verbose?
          log "Deleted #{server.cleanup_backups.length} backups" if verbose?
        else
          log "Backup not required (latest backup = #{server.latest_backup}, maximum backup age = #{maximum_backup_age})" if verbose?
        end
      end

      def backup_required?
        latest_backup = server.latest_backup
        return latest_backup.nil? || latest_backup < maximum_backup_age
      end

      def maximum_backup_age
        case server.rotation.minimum_unit
        when 'hourly'
          1.hour.ago
        when 'daily'
          1.day.ago
        when 'weekly'
          1.week.ago
        when 'monthly'
          1.month.ago
        when 'yearly'
          1.year.ago
        end
      end

      def backend
        @backend ||= YOLOBackup::BackupRunner::Backend::Rsync.new(server)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yolo_backup-0.0.0 lib/yolo_backup/backup_runner/job.rb