Class: KamalBackup::App

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV, config: nil, redactor: nil, restic: nil, database: nil, evidence_class: Evidence, scheduler_class: Scheduler) ⇒ App

Returns a new instance of App.



21
22
23
24
25
26
27
28
# File 'lib/kamal_backup/app.rb', line 21

def initialize(env: ENV, config: nil, redactor: nil, restic: nil, database: nil, evidence_class: Evidence, scheduler_class: Scheduler)
  @config = config || Config.new(env: env)
  @redactor = redactor || Redactor.new(env: @config.env)
  @restic = restic
  @database = database
  @evidence_class = evidence_class
  @scheduler_class = scheduler_class
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



19
20
21
# File 'lib/kamal_backup/app.rb', line 19

def config
  @config
end

#redactorObject (readonly)

Returns the value of attribute redactor.



19
20
21
# File 'lib/kamal_backup/app.rb', line 19

def redactor
  @redactor
end

Instance Method Details

#backupObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kamal_backup/app.rb', line 30

def backup
  config.validate_backup

  timestamp = current_timestamp
  restic.ensure_repository
  database.backup(restic, timestamp)
  restic.backup_paths(config.backup_paths, tags: ["type:files", "run:#{timestamp}"])

  if config.forget_after_backup?
    restic.forget_after_success
  end

  if config.check_after_backup?
    restic.check
  end

  true
end

#checkObject



119
120
121
122
# File 'lib/kamal_backup/app.rb', line 119

def check
  config.validate_restic
  restic.check.stdout
end

#drill_failed?(result) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
# File 'lib/kamal_backup/app.rb', line 108

def drill_failed?(result)
  result.fetch(:status) != "ok"
rescue KeyError
  true
end

#drill_on_local_machine(snapshot = "latest", check_command: nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kamal_backup/app.rb', line 78

def drill_on_local_machine(snapshot = "latest", check_command: nil)
  validate_local_machine_restore

  run_drill("local", snapshot, check_command: check_command) do |result|
    adapter = database
    validate_local_machine_database_target(adapter)
    result[:database] = perform_database_restore_to_current(snapshot, adapter: adapter)
    result[:files] = perform_replacement_file_restore(
      snapshot,
      source_paths: config.local_restore_source_paths,
      target_paths: config.backup_paths
    )
  end
end

#drill_on_production(snapshot = "latest", database_name: nil, sqlite_path: nil, file_target: "/restore/files", check_command: nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/kamal_backup/app.rb', line 93

def drill_on_production(snapshot = "latest", database_name: nil, sqlite_path: nil, file_target: "/restore/files", check_command: nil)
  validate_production_drill(file_target, database_name, sqlite_path)

  run_drill("production", snapshot, check_command: check_command) do |result|
    adapter = database
    result[:database] = perform_database_restore_to_scratch(
      snapshot,
      adapter: adapter,
      database_name: database_name,
      sqlite_path: sqlite_path
    )
    result[:files] = perform_file_restore(snapshot, target: file_target)
  end
end

#evidenceObject



124
125
126
127
# File 'lib/kamal_backup/app.rb', line 124

def evidence
  config.validate_restic
  @evidence_class.new(config, restic: restic, redactor: redactor).to_json
end

#restore_to_local_machine(snapshot = "latest") ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kamal_backup/app.rb', line 49

def restore_to_local_machine(snapshot = "latest")
  validate_local_machine_restore

  build_restore_result("local", snapshot) do |result|
    adapter = database
    validate_local_machine_database_target(adapter)
    result[:database] = perform_database_restore_to_current(snapshot, adapter: adapter)
    result[:files] = perform_replacement_file_restore(
      snapshot,
      source_paths: config.local_restore_source_paths,
      target_paths: config.backup_paths
    )
  end
end

#restore_to_production(snapshot = "latest") ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kamal_backup/app.rb', line 64

def restore_to_production(snapshot = "latest")
  validate_production_restore

  build_restore_result("production", snapshot) do |result|
    adapter = database
    result[:database] = perform_database_restore_to_current(snapshot, adapter: adapter)
    result[:files] = perform_replacement_file_restore(
      snapshot,
      source_paths: config.backup_paths,
      target_paths: config.backup_paths
    )
  end
end

#scheduleObject



129
130
131
132
# File 'lib/kamal_backup/app.rb', line 129

def schedule
  config.validate_backup
  @scheduler_class.new(config) { backup }.run
end

#snapshotsObject



114
115
116
117
# File 'lib/kamal_backup/app.rb', line 114

def snapshots
  config.validate_restic
  restic.snapshots.stdout
end