class SimpleBackup::Source::Abstract

Public Instance Methods

backends=(value) click to toggle source
# File lib/simple_backup/source/abstract.rb, line 125
def backends=(value)
  @backends = []
  @backends = @backends + value if value.kind_of?(Array)
  @backends << value unless value.kind_of?(Array)
end
backup_file() click to toggle source
# File lib/simple_backup/source/abstract.rb, line 121
def backup_file
  @backup_file
end
cleanup() click to toggle source
# File lib/simple_backup/source/abstract.rb, line 114
def cleanup
  return nil unless @backup_file

  FileUtils.rm (@backup_file)
  @@logger.debug "Temporary backup file #{@backup_file} was removed"
end
configure(options = {}) click to toggle source
# File lib/simple_backup/source/abstract.rb, line 59
def configure(options = {})
  raise NotImplementedError
end
desc() click to toggle source
# File lib/simple_backup/source/abstract.rb, line 87
def desc
  '%5s :: %s' % [type, @name]
end
get() click to toggle source
# File lib/simple_backup/source/abstract.rb, line 91
def get
  return @backup_file if @backup_file

  @@logger.scope_start :info, "Getting archive for: #{desc}"

  @tmp_dir = ::File.join(get_tmp, "simple_backup-#{SimpleBackup::TIMESTAMP}-#{SecureRandom.uuid}")
  FileUtils.mkdir_p @tmp_dir, mode: 0700

  @@logger.debug "Created tmp directory #{@tmp_dir}"

  data_exists = prepare_data
  archive_data if data_exists

  @@logger.warning "No data for: #{desc}" unless data_exists

  FileUtils.rm_rf(@tmp_dir)
  @@logger.debug "Removed tmp directory #{@tmp_dir}"

  @backup_file
ensure
  @@logger.scope_end
end
keep_last() click to toggle source
# File lib/simple_backup/source/abstract.rb, line 67
def keep_last
  @keep_last
end
keep_last=(value) click to toggle source
# File lib/simple_backup/source/abstract.rb, line 63
def keep_last=(value)
  @keep_last = value
end
name() click to toggle source
# File lib/simple_backup/source/abstract.rb, line 75
def name
  @name
end
name=(value) click to toggle source
# File lib/simple_backup/source/abstract.rb, line 71
def name=(value)
  @name = value.gsub(/[^a-zA-Z0-9\-\_\. ]*/, '').gsub(/\s+/, '_').downcase
end
supports(backend) click to toggle source
# File lib/simple_backup/source/abstract.rb, line 131
def supports(backend)
  return TRUE unless @backends
  return FALSE unless @backends.include?(backend.name)

  TRUE
end
tmp_base_path=(value) click to toggle source
# File lib/simple_backup/source/abstract.rb, line 79
def tmp_base_path=(value)
  @tmp_base_path = value
end
type() click to toggle source
# File lib/simple_backup/source/abstract.rb, line 83
def type
  self.class.name.split('::').last.gsub(/[^a-zA-Z0-9\-\_\. ]*/, '').gsub(/\s+/, '_').downcase
end

Private Instance Methods

archive_data() click to toggle source
# File lib/simple_backup/source/abstract.rb, line 143
def archive_data
  filename = "#{type}-#{name}.#{SimpleBackup::TIMESTAMP}.tar.gz"
  @backup_file = ::File.join(get_tmp, filename)

  targz

  @@logger.debug "Backup saved to temporary file #{backup_file}"
end
get_tmp() click to toggle source
# File lib/simple_backup/source/abstract.rb, line 152
def get_tmp
  tmp = @tmp_base_path
  tmp = ::Dir.tmpdir unless tmp
  tmp
end
prepare_data() click to toggle source
# File lib/simple_backup/source/abstract.rb, line 139
def prepare_data
  raise NotImplementedError
end
targz() click to toggle source
# File lib/simple_backup/source/abstract.rb, line 158
def targz
  path = @tmp_dir

  tempfile = Tempfile.new("#{type}-#{name}", get_tmp)
  @@logger.debug tempfile.path

  Gem::Package::TarWriter.new(tempfile) do |tar|
    @@logger.debug "Opened tar archive #{tar}"
    ::Dir[::File.join(path, '**/*')].each do |file|
      @@logger.debug "Adding file #{file}"
      mode = ::File.stat(file).mode
      relative_file = file.sub(/^#{Regexp::escape path}\/?/, '')

      if ::File.directory?(file)
        tar.mkdir(relative_file, mode)
      else
        tar.add_longname_file relative_file, mode do |tf|
          ::File.open(file, 'rb') do |f|
            while f_content = f.read(1048576)
              tf.write f_content
            end
          end
        end
      end
    end
  end

  gz = ::File.open(@backup_file, 'w')
  Zlib::GzipWriter.open(gz) do |zip|
    tempfile.seek(0)
    while f_content = tempfile.read(1048576)
      zip.write f_content
    end
  end

  tempfile.close
  tempfile.unlink
end