Sha256: dba6baa50d0d6f8416c66d73993dbedd98573da7f74198e0cf7bc27b328927c4

Contents?: true

Size: 1.66 KB

Versions: 5

Compression:

Stored size: 1.66 KB

Contents

module EY
  module Backup
    class Database
      def initialize(engine, base_path, keep, backend, environment, name)
        if name.nil? || name.empty?
          raise ArgumentError, "database name is blank"
        end
        @engine    = engine
        @base_path = base_path
        @keep      = keep
        @backend = backend
        @environment = environment
        @name = name
        @backup_size = nil
      end
      attr_reader :name, :engine, :environment, :keep, :base_path, :backup_size

      def bucket_minder
        @backend.bucket_minder
      end

      def backups
        s3_objects = bucket_minder.list("#{@environment}.#{@name}/").select { |o| o[:name] =~ @engine.suffix }
        s3_objects.map {|o| BackupSet.new(self, o[:name], nil, o[:keys]) }.sort
      end

      def dump
        FileUtils.mkdir_p(@base_path)

        basename = generate_basename
        file = @engine.dump(@name, basename)
        @backup_size = number_to_human_size(File.size(file))

        BackupSet.from(self, basename, file)
      end

      def start_upload(filenames)
        @backend.start_upload(filenames, @environment, @name)
      end

      def generate_basename
        File.join(@base_path, "#{@name}.#{timestamp}")
      end

      def timestamp
        Time.now.strftime("%Y-%m-%dT%H-%M-%S")
      end
      
      def number_to_human_size(size)
        if size < 1024
          "#{size} bytes"
        elsif size < 1024.0 * 1024.0
          "%.01f KB" % (size / 1024.0)
        elsif size < 1024.0 * 1024.0 * 1024.0
          "%.01f MB" % (size / 1024.0 / 1024.0)
        else
          "%.01f GB" % (size / 1024.0 / 1024.0 / 1024.0)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ey_cloud_server-1.5.0 lib/ey_backup/database.rb
ey_cloud_server-1.4.61 lib/ey_backup/database.rb
ey_cloud_server-1.4.60 lib/ey_backup/database.rb
ey_cloud_server-1.4.58 lib/ey_backup/database.rb
ey_cloud_server-1.4.54 lib/ey_backup/database.rb