Class: KamalBackup::Databases::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/kamal_backup/databases/base.rb

Direct Known Subclasses

Mysql, Postgres, Sqlite

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, redactor:) ⇒ Base

Returns a new instance of Base.



22
23
24
25
# File 'lib/kamal_backup/databases/base.rb', line 22

def initialize(config, redactor:)
  @config = config
  @redactor = redactor
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



20
21
22
# File 'lib/kamal_backup/databases/base.rb', line 20

def config
  @config
end

#redactorObject (readonly)

Returns the value of attribute redactor.



20
21
22
# File 'lib/kamal_backup/databases/base.rb', line 20

def redactor
  @redactor
end

Class Method Details

.build(config, redactor:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/kamal_backup/databases/base.rb', line 7

def self.build(config, redactor:)
  case config.database_adapter
  when "postgres"
    Postgres.new(config, redactor: redactor)
  when "mysql"
    Mysql.new(config, redactor: redactor)
  when "sqlite"
    Sqlite.new(config, redactor: redactor)
  else
    raise ConfigurationError, "unsupported DATABASE_ADAPTER: #{config.database_adapter.inspect}"
  end
end

Instance Method Details

#adapter_nameObject

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/kamal_backup/databases/base.rb', line 53

def adapter_name
  raise NotImplementedError
end

#backup(restic, timestamp) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/kamal_backup/databases/base.rb', line 27

def backup(restic, timestamp)
  restic.backup_stream(
    dump_command,
    filename: database_filename(timestamp),
    tags: backup_tags(timestamp)
  )
end

#backup_tags(timestamp) ⇒ Object



49
50
51
# File 'lib/kamal_backup/databases/base.rb', line 49

def backup_tags(timestamp)
  ["type:database", "adapter:#{adapter_name}", "run:#{timestamp}"]
end

#current_restore_commandObject

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/kamal_backup/databases/base.rb', line 65

def current_restore_command
  raise NotImplementedError
end

#current_target_identifierObject

Raises:

  • (NotImplementedError)


77
78
79
# File 'lib/kamal_backup/databases/base.rb', line 77

def current_target_identifier
  raise NotImplementedError
end

#database_filename(timestamp) ⇒ Object



44
45
46
47
# File 'lib/kamal_backup/databases/base.rb', line 44

def database_filename(timestamp)
  app = config.app_name.gsub(/[^A-Za-z0-9_.-]+/, "-")
  "databases-#{app}-#{adapter_name}-#{timestamp}.#{dump_extension}"
end

#dump_commandObject

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/kamal_backup/databases/base.rb', line 61

def dump_command
  raise NotImplementedError
end

#dump_extensionObject

Raises:

  • (NotImplementedError)


57
58
59
# File 'lib/kamal_backup/databases/base.rb', line 57

def dump_extension
  raise NotImplementedError
end

#restore_to_current(restic, snapshot, filename) ⇒ Object



35
36
37
# File 'lib/kamal_backup/databases/base.rb', line 35

def restore_to_current(restic, snapshot, filename)
  restic.pipe_dump_to_command(snapshot, filename, current_restore_command)
end

#restore_to_scratch(restic, snapshot, filename, target:) ⇒ Object



39
40
41
42
# File 'lib/kamal_backup/databases/base.rb', line 39

def restore_to_scratch(restic, snapshot, filename, target:)
  validate_scratch_restore_target(target)
  restic.pipe_dump_to_command(snapshot, filename, scratch_restore_command(target))
end

#scratch_restore_command(target) ⇒ Object

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/kamal_backup/databases/base.rb', line 69

def scratch_restore_command(target)
  raise NotImplementedError
end

#scratch_target_identifier(target) ⇒ Object

Raises:

  • (NotImplementedError)


81
82
83
# File 'lib/kamal_backup/databases/base.rb', line 81

def scratch_target_identifier(target)
  raise NotImplementedError
end

#validate_scratch_restore_target(target) ⇒ Object



73
74
75
# File 'lib/kamal_backup/databases/base.rb', line 73

def validate_scratch_restore_target(target)
  config.validate_database_restore_target(scratch_target_identifier(target))
end