Class: KamalBackup::CLI

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

Constant Summary collapse

HELP =
<<~TEXT
  Usage:
    kamal-backup backup
    kamal-backup restore-db [snapshot-or-latest]
    kamal-backup restore-files [snapshot-or-latest] [target-dir]
    kamal-backup list
    kamal-backup check
    kamal-backup evidence
    kamal-backup schedule
    kamal-backup version

  Environment is used for configuration. See README.md for Kamal accessory examples.
TEXT

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV) ⇒ CLI

Returns a new instance of CLI.



40
41
42
43
# File 'lib/kamal_backup/cli.rb', line 40

def initialize(env: ENV)
  @config = Config.new(env: env)
  @redactor = Redactor.new(env: env)
end

Class Method Details

.start(argv = ARGV, env: ENV) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/kamal_backup/cli.rb', line 30

def self.start(argv = ARGV, env: ENV)
  new(env: env).run(argv)
rescue Error => e
  warn("kamal-backup: #{Redactor.new(env: env).redact_string(e.message)}")
  exit(1)
rescue Interrupt
  warn("kamal-backup: interrupted")
  exit(130)
end

Instance Method Details

#backupObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/kamal_backup/cli.rb', line 71

def backup
  @config.validate_for_backup!
  timestamp = Time.now.utc.strftime("%Y%m%dT%H%M%SZ")
  restic.ensure_repository!
  database.backup(restic, timestamp)
  restic.backup_paths(@config.backup_paths, tags: ["type:files", "run:#{timestamp}"])
  restic.forget_after_success! if @config.forget_after_backup?
  restic.check! if @config.check_after_backup?
  true
end

#checkObject



109
110
111
112
113
# File 'lib/kamal_backup/cli.rb', line 109

def check
  @config.validate_for_restic!
  puts restic.check!.stdout
  true
end

#evidenceObject



115
116
117
118
119
# File 'lib/kamal_backup/cli.rb', line 115

def evidence
  @config.validate_for_restic!
  puts Evidence.new(@config, restic: restic, redactor: @redactor).to_json
  true
end

#listObject



103
104
105
106
107
# File 'lib/kamal_backup/cli.rb', line 103

def list
  @config.validate_for_restic!
  puts restic.snapshots.stdout
  true
end

#restore_db(snapshot) ⇒ Object

Raises:



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/kamal_backup/cli.rb', line 82

def restore_db(snapshot)
  @config.validate_for_restic!
  @config.validate_restore_allowed!
  adapter = database
  resolved_snapshot = resolve_snapshot(snapshot, ["type:database", "adapter:#{adapter.adapter_name}"])
  filename = restic.database_file(resolved_snapshot, adapter.adapter_name)
  raise ConfigurationError, "could not find database backup file in snapshot #{resolved_snapshot}" unless filename

  adapter.restore(restic, resolved_snapshot, filename)
  true
end

#restore_files(snapshot, target) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/kamal_backup/cli.rb', line 94

def restore_files(snapshot, target)
  @config.validate_for_restic!
  @config.validate_restore_allowed!
  target = @config.validate_file_restore_target!(target)
  resolved_snapshot = resolve_snapshot(snapshot, ["type:files"])
  restic.restore_snapshot(resolved_snapshot, target)
  true
end

#run(argv) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/kamal_backup/cli.rb', line 45

def run(argv)
  argv = argv.dup
  command = argv.shift
  return puts(HELP) if command.nil? || %w[-h --help help].include?(command)
  return puts(VERSION) if %w[-v --version version].include?(command)

  case command
  when "backup"
    backup
  when "restore-db"
    restore_db(argv[0] || "latest")
  when "restore-files"
    restore_files(argv[0] || "latest", argv[1] || "/restore/files")
  when "list"
    list
  when "check"
    check
  when "evidence"
    evidence
  when "schedule"
    schedule
  else
    raise ConfigurationError, "unknown command: #{command}\n\n#{HELP}"
  end
end

#scheduleObject



121
122
123
124
# File 'lib/kamal_backup/cli.rb', line 121

def schedule
  @config.validate_for_backup!
  Scheduler.new(@config) { backup }.run
end