Class: KamalBackup::App

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

Constant Summary collapse

FRESH_BACKUP_GRACE_SECONDS =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV, config: nil, redactor: nil, restic: nil, database: nil) ⇒ App

Returns a new instance of App.



25
26
27
28
29
30
# File 'lib/kamal_backup/app.rb', line 25

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



23
24
25
# File 'lib/kamal_backup/app.rb', line 23

def config
  @config
end

#redactorObject (readonly)

Returns the value of attribute redactor.



23
24
25
# File 'lib/kamal_backup/app.rb', line 23

def redactor
  @redactor
end

Instance Method Details

#backup(force: false) ⇒ Object



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

def backup(force: false)
  started_at = Time.now.utc
  config.validate_backup
  return skipped_backup_result(started_at) unless force || backup_due?(started_at)

  restic.ensure_repository
  databases.each { |database| database.backup(restic) }
  restic.backup_paths(config.backup_paths, tags: ['type:files'])

  restic.prune if config.forget_after_backup?

  restic.check if config.check_after_backup?

  backup_summary(started_at: started_at, finished_at: Time.now.utc).tap do |summary|
    validate_fresh_backup_summary!(summary, started_at: started_at)
    write_last_backup(summary)
  end
end

#checkObject



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

def check
  config.validate_restic
  restic.check.stdout
end

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



75
76
77
78
79
80
81
82
83
# File 'lib/kamal_backup/app.rb', line 75

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

  run_drill('local', snapshot, check_command: check_command) do |result|
    databases.each { |adapter| validate_local_machine_database_target(adapter) }
    result[:databases] = perform_database_restores_to_current(snapshot)
    result[:files] = perform_replacement_file_restore(snapshot, production_source: false)
  end
end

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/kamal_backup/app.rb', line 85

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|
    result[:databases] = databases.map do |adapter|
      perform_database_restore_to_scratch(
        snapshot,
        adapter: adapter,
        database_name: database_name,
        sqlite_path: sqlite_path
      )
    end
    result[:files] = perform_file_restore(snapshot, target: file_target)
  end
end

#evidenceObject



117
118
119
120
# File 'lib/kamal_backup/app.rb', line 117

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

#pruneObject



112
113
114
115
# File 'lib/kamal_backup/app.rb', line 112

def prune
  config.validate_backup(check_files: false)
  restic.prune
end

#restore_to_local_machine(snapshot = 'latest') ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/kamal_backup/app.rb', line 56

def restore_to_local_machine(snapshot = 'latest')
  validate_local_machine_restore

  build_restore_result('local', snapshot) do |result|
    databases.each { |adapter| validate_local_machine_database_target(adapter) }
    result[:databases] = perform_database_restores_to_current(snapshot)
    result[:files] = perform_replacement_file_restore(snapshot, production_source: false)
  end
end

#restore_to_production(snapshot = 'latest') ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/kamal_backup/app.rb', line 66

def restore_to_production(snapshot = 'latest')
  validate_production_restore

  build_restore_result('production', snapshot) do |result|
    result[:databases] = perform_database_restores_to_current(snapshot)
    result[:files] = perform_replacement_file_restore(snapshot, production_source: true)
  end
end

#scheduleObject



122
123
124
125
# File 'lib/kamal_backup/app.rb', line 122

def schedule
  config.validate_backup
  Scheduler.new(config) { backup(force: true) }.run
end

#snapshotsObject



102
103
104
105
# File 'lib/kamal_backup/app.rb', line 102

def snapshots
  config.validate_restic
  restic.snapshots.stdout
end

#validate(check_files: true) ⇒ Object



51
52
53
54
# File 'lib/kamal_backup/app.rb', line 51

def validate(check_files: true)
  config.validate_backup(check_files: check_files)
  true
end