module Backup::Storage::Cycler

Private Instance Methods

cycle!() click to toggle source

Adds the current package being stored to the YAML cycle data file and will remove any old package file(s) when the storage limit set by keep is exceeded.

# File lib/backup/storage/cycler.rb, line 12
def cycle!
  Logger.info 'Cycling Started...'

  packages = yaml_load.unshift(package)
  cycled_packages = []

  if keep.is_a?(Date) || keep.is_a?(Time)
    cycled_packages = packages.select do |p|
      p.time_as_object < keep.to_time
    end
  else
    excess = packages.count - keep.to_i
    cycled_packages = packages.last(excess) if excess > 0
  end

  saved_packages = packages - cycled_packages
  cycled_packages.each { |package| delete_package package }

  yaml_save(saved_packages)
end
delete_package(package) click to toggle source
# File lib/backup/storage/cycler.rb, line 33
      def delete_package(package)
        begin
          remove!(package) unless package.no_cycle
        rescue => err
          Logger.warn Error.wrap(err, "            There was a problem removing the following package:
            Trigger: #{package.trigger} :: Dated: #{package.time}
            Package included the following #{ package.filenames.count } file(s):
            #{ package.filenames.join("\n") }
")
        end
      end
yaml_file() click to toggle source

Returns path to the YAML data file.

# File lib/backup/storage/cycler.rb, line 47
def yaml_file
  @yaml_file ||= begin
    filename = self.class.to_s.split('::').last
    filename << "-#{ storage_id }" if storage_id
    File.join(Config.data_path, package.trigger, "#{ filename }.yml")
  end
end
yaml_load() click to toggle source

Returns stored Package objects, sorted by time descending (oldest last).

# File lib/backup/storage/cycler.rb, line 56
def yaml_load
  if File.exist?(yaml_file) && !File.zero?(yaml_file)
    YAML.load_file(yaml_file).sort_by!(&:time).reverse!
  else
    []
  end
end
yaml_save(packages) click to toggle source

Stores the given package objects to the YAML data file.

# File lib/backup/storage/cycler.rb, line 65
def yaml_save(packages)
  FileUtils.mkdir_p(File.dirname(yaml_file))
  File.open(yaml_file, 'w') do |file|
    file.write(packages.to_yaml)
  end
end