Class: KamalBackup::App
- Inherits:
-
Object
- Object
- KamalBackup::App
- Defined in:
- lib/kamal_backup/app.rb
Constant Summary collapse
- FRESH_BACKUP_GRACE_SECONDS =
5
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#redactor ⇒ Object
readonly
Returns the value of attribute redactor.
Instance Method Summary collapse
- #backup ⇒ Object
- #check ⇒ Object
- #drill_failed?(result) ⇒ Boolean
- #drill_on_local_machine(snapshot = "latest", check_command: nil) ⇒ Object
- #drill_on_production(snapshot = "latest", database_name: nil, sqlite_path: nil, file_target: "/restore/files", check_command: nil) ⇒ Object
- #evidence ⇒ Object
-
#initialize(env: ENV, config: nil, redactor: nil, restic: nil, database: nil, evidence_class: Evidence, scheduler_class: Scheduler) ⇒ App
constructor
A new instance of App.
- #restore_to_local_machine(snapshot = "latest") ⇒ Object
- #restore_to_production(snapshot = "latest") ⇒ Object
- #schedule ⇒ Object
- #snapshots ⇒ Object
- #validate(check_files: true) ⇒ Object
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.
23 24 25 26 27 28 29 30 |
# File 'lib/kamal_backup/app.rb', line 23 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
#config ⇒ Object (readonly)
Returns the value of attribute config.
21 22 23 |
# File 'lib/kamal_backup/app.rb', line 21 def config @config end |
#redactor ⇒ Object (readonly)
Returns the value of attribute redactor.
21 22 23 |
# File 'lib/kamal_backup/app.rb', line 21 def redactor @redactor end |
Instance Method Details
#backup ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/kamal_backup/app.rb', line 32 def backup started_at = Time.now.utc config.validate_backup require_restic! restic.ensure_repository databases.each { |database| database.backup(restic) } restic.backup_paths(config.backup_paths, tags: ["type:files"]) if config.forget_after_backup? restic.forget_after_success end if config.check_after_backup? restic.check end backup_summary(started_at: started_at, finished_at: Time.now.utc).tap do |summary| validate_fresh_backup_summary!(summary, started_at: started_at) end end |
#check ⇒ Object
124 125 126 127 128 |
# File 'lib/kamal_backup/app.rb', line 124 def check config.validate_restic require_restic! restic.check.stdout end |
#drill_failed?(result) ⇒ Boolean
112 113 114 115 116 |
# File 'lib/kamal_backup/app.rb', line 112 def drill_failed?(result) result.fetch(:status) != "ok" rescue KeyError true end |
#drill_on_local_machine(snapshot = "latest", check_command: nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/kamal_backup/app.rb', line 82 def drill_on_local_machine(snapshot = "latest", check_command: nil) validate_local_machine_restore require_restic! run_drill("local", snapshot, check_command: check_command) do |result| databases.each { |adapter| validate_local_machine_database_target(adapter) } database_results = perform_database_restores_to_current(snapshot) file_results = perform_replacement_file_restores(snapshot, production_source: false) assign_restore_results(result, database_results, file_results) end end |
#drill_on_production(snapshot = "latest", database_name: nil, sqlite_path: nil, file_target: "/restore/files", check_command: nil) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/kamal_backup/app.rb', line 94 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) require_restic! run_drill("production", snapshot, check_command: check_command) do |result| database_results = databases.map do |adapter| perform_database_restore_to_scratch( snapshot, adapter: adapter, database_name: database_name, sqlite_path: sqlite_path ) end file_results = [perform_file_restore(snapshot, target: file_target)] assign_restore_results(result, database_results, file_results) end end |
#evidence ⇒ Object
130 131 132 133 134 |
# File 'lib/kamal_backup/app.rb', line 130 def evidence config.validate_restic require_restic! @evidence_class.new(config, restic: restic, redactor: redactor).to_json end |
#restore_to_local_machine(snapshot = "latest") ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/kamal_backup/app.rb', line 59 def restore_to_local_machine(snapshot = "latest") validate_local_machine_restore require_restic! build_restore_result("local", snapshot) do |result| databases.each { |adapter| validate_local_machine_database_target(adapter) } database_results = perform_database_restores_to_current(snapshot) file_results = perform_replacement_file_restores(snapshot, production_source: false) assign_restore_results(result, database_results, file_results) end end |
#restore_to_production(snapshot = "latest") ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/kamal_backup/app.rb', line 71 def restore_to_production(snapshot = "latest") validate_production_restore require_restic! build_restore_result("production", snapshot) do |result| database_results = perform_database_restores_to_current(snapshot) file_results = perform_replacement_file_restores(snapshot, production_source: true) assign_restore_results(result, database_results, file_results) end end |
#schedule ⇒ Object
136 137 138 139 |
# File 'lib/kamal_backup/app.rb', line 136 def schedule config.validate_backup @scheduler_class.new(config) { backup }.run end |
#snapshots ⇒ Object
118 119 120 121 122 |
# File 'lib/kamal_backup/app.rb', line 118 def snapshots config.validate_restic require_restic! restic.snapshots.stdout end |
#validate(check_files: true) ⇒ Object
54 55 56 57 |
# File 'lib/kamal_backup/app.rb', line 54 def validate(check_files: true) config.validate_backup(check_files: check_files) true end |