Class: EY::Backup::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/ey_backup/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, base_path, keep, backend, environment, name) ⇒ Database

Returns a new instance of Database.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/ey_backup/database.rb', line 4

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

Instance Attribute Details

#backup_sizeObject (readonly)

Returns the value of attribute backup_size.



16
17
18
# File 'lib/ey_backup/database.rb', line 16

def backup_size
  @backup_size
end

#base_pathObject (readonly)

Returns the value of attribute base_path.



16
17
18
# File 'lib/ey_backup/database.rb', line 16

def base_path
  @base_path
end

#engineObject (readonly)

Returns the value of attribute engine.



16
17
18
# File 'lib/ey_backup/database.rb', line 16

def engine
  @engine
end

#environmentObject (readonly)

Returns the value of attribute environment.



16
17
18
# File 'lib/ey_backup/database.rb', line 16

def environment
  @environment
end

#keepObject (readonly)

Returns the value of attribute keep.



16
17
18
# File 'lib/ey_backup/database.rb', line 16

def keep
  @keep
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/ey_backup/database.rb', line 16

def name
  @name
end

Instance Method Details

#backupsObject



22
23
24
25
# File 'lib/ey_backup/database.rb', line 22

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

#bucket_minderObject



18
19
20
# File 'lib/ey_backup/database.rb', line 18

def bucket_minder
  @backend.bucket_minder
end

#dumpObject



27
28
29
30
31
32
33
34
35
# File 'lib/ey_backup/database.rb', line 27

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

#generate_basenameObject



41
42
43
# File 'lib/ey_backup/database.rb', line 41

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

#number_to_human_size(size) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ey_backup/database.rb', line 49

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

#start_upload(filenames) ⇒ Object



37
38
39
# File 'lib/ey_backup/database.rb', line 37

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

#timestampObject



45
46
47
# File 'lib/ey_backup/database.rb', line 45

def timestamp
  Time.now.strftime("%Y-%m-%dT%H-%M-%S")
end