Class: KamalBackup::Restic
- Inherits:
-
Object
- Object
- KamalBackup::Restic
- Defined in:
- lib/kamal_backup/restic.rb
Constant Summary collapse
- RESTIC_ENV_PATTERN =
/\A(?:RESTIC_|AWS_|B2_|AZURE_|GOOGLE_|RCLONE_|OS_|ST_|HP_|HTTP_|HTTPS_|NO_PROXY)/i
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_file(path, filename:, tags:) ⇒ Object
- #backup_path(path, tags:) ⇒ Object
- #backup_paths(paths, tags:) ⇒ Object
- #backup_stream(command, filename:, tags:) ⇒ Object
- #check ⇒ Object
- #common_tags ⇒ Object
- #database_file(snapshot, adapter, database_name: nil) ⇒ Object
- #ensure_repository ⇒ Object
- #forget_after_success ⇒ Object
-
#initialize(config, redactor:) ⇒ Restic
constructor
A new instance of Restic.
- #latest_snapshot(tags:) ⇒ Object
- #ls_json(snapshot) ⇒ Object
- #pipe_dump_to_command(snapshot, filename, command) ⇒ Object
- #prune ⇒ Object
- #restore_snapshot(snapshot, target) ⇒ Object
- #run(args, log_output: true) ⇒ Object
- #snapshots(tags: common_tags) ⇒ Object
- #snapshots_json(tags: common_tags) ⇒ Object
- #write_dump_to_path(snapshot, filename, target_path) ⇒ Object
Constructor Details
#initialize(config, redactor:) ⇒ Restic
Returns a new instance of Restic.
16 17 18 19 |
# File 'lib/kamal_backup/restic.rb', line 16 def initialize(config, redactor:) @config = config @redactor = redactor end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
14 15 16 |
# File 'lib/kamal_backup/restic.rb', line 14 def config @config end |
#redactor ⇒ Object (readonly)
Returns the value of attribute redactor.
14 15 16 |
# File 'lib/kamal_backup/restic.rb', line 14 def redactor @redactor end |
Instance Method Details
#backup_file(path, filename:, tags:) ⇒ Object
40 41 42 43 44 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 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/kamal_backup/restic.rb', line 40 def backup_file(path, filename:, tags:) command = CommandSpec.new( argv: %w[restic backup] + host_args + ['--stdin', '--stdin-filename', filename] + tag_args( + ), env: restic_env ) log("backing up file content as #{filename}") File.open(path, 'rb') do |file| output = Command.output context = output&.command_start(command, redactor: redactor) Open3.popen3(command.env, *command.argv) do |stdin, stdout, stderr, wait_thread| stdout_reader = Thread.new do Command.collect_stream(stdout, command_output: output, context: context, stream: :stdout, redactor: redactor) end stderr_reader = Thread.new do Command.collect_stream(stderr, command_output: output, context: context, stream: :stderr, redactor: redactor) end copy_error = nil begin IO.copy_stream(file, stdin) rescue Errno::EPIPE => e copy_error = e ensure stdin.close unless stdin.closed? end out = stdout_reader.value err = stderr_reader.value status = wait_thread.value output&.command_exit(context, status.exitstatus) raise_command_error(command, status, out, err) unless status.success? raise_stream_error(command, copy_error, out, err) if copy_error CommandResult.new(stdout: out, stderr: err, status: status.exitstatus) end end rescue Errno::ENOENT => e raise CommandError.new("command not found: #{command.argv.first}", command: command, status: 127, stderr: e.) end |
#backup_path(path, tags:) ⇒ Object
94 95 96 |
# File 'lib/kamal_backup/restic.rb', line 94 def backup_path(path, tags:) backup_paths([path], tags: ) end |
#backup_paths(paths, tags:) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/kamal_backup/restic.rb', line 83 def backup_paths(paths, tags:) paths = Array(paths).compact.map(&:to_s).reject(&:empty?) return unless paths.any? = paths.map { |path| "path:#{config.backup_path_label(path)}" } excludes = config.backup_path_excludes(paths) log("backing up #{paths.size} file path(s): #{paths.join(', ')}") run(['backup'] + host_args + exclude_args(excludes) + paths + tag_args( + + )) end |
#backup_stream(command, filename:, tags:) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/kamal_backup/restic.rb', line 30 def backup_stream(command, filename:, tags:) restic_command = CommandSpec.new( argv: %w[restic backup] + host_args + ['--stdin', '--stdin-filename', filename] + tag_args( + ), env: restic_env ) log("backing up stream as #{filename}") pipe_commands(command, restic_command, producer_label: 'dump', consumer_label: 'restic backup') end |
#check ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/kamal_backup/restic.rb', line 110 def check args = %w[check] args.concat(['--read-data-subset', config.check_read_data_subset]) if config.check_read_data_subset started_at = Time.now.utc result = run(args) write_last_check(status: 'ok', started_at: started_at, finished_at: Time.now.utc, output: result.stdout) result rescue CommandError => e write_last_check(status: 'failed', started_at: started_at || Time.now.utc, finished_at: Time.now.utc, error: e.) raise end |
#common_tags ⇒ Object
219 220 221 |
# File 'lib/kamal_backup/restic.rb', line 219 def ['kamal-backup', "app:#{config.app_name}"] end |
#database_file(snapshot, adapter, database_name: nil) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/kamal_backup/restic.rb', line 153 def database_file(snapshot, adapter, database_name: nil) legacy_prefix = "databases/#{config.app_name}/#{adapter}/" app = config.app_name.gsub(/[^A-Za-z0-9_.-]+/, '-') database = database_name.to_s.gsub(/[^A-Za-z0-9_.-]+/, '-') stable_prefix = database.empty? ? nil : "databases/#{app}/#{database}/#{adapter}." flat_prefix = "databases-#{app}-#{adapter}-" named_flat_prefix = database.empty? ? nil : "databases-#{app}-#{database}-#{adapter}-" ls_json(snapshot).find do |entry| next false unless entry['type'] == 'file' normalized = entry['path'].to_s.sub(%r{\A/+}, '') (stable_prefix && normalized.start_with?(stable_prefix)) || normalized.start_with?(legacy_prefix) || File.basename(normalized).start_with?(flat_prefix) || (named_flat_prefix && File.basename(normalized).start_with?(named_flat_prefix)) end&.fetch('path') end |
#ensure_repository ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/kamal_backup/restic.rb', line 21 def ensure_repository run(%w[snapshots --json], log_output: false) rescue CommandError => e raise e unless config.restic_init_if_missing? log('restic repository not ready, running restic init') run(%w[init]) end |
#forget_after_success ⇒ Object
98 99 100 |
# File 'lib/kamal_backup/restic.rb', line 98 def forget_after_success prune end |
#latest_snapshot(tags:) ⇒ Object
137 138 139 140 141 142 |
# File 'lib/kamal_backup/restic.rb', line 137 def latest_snapshot(tags:) snapshots = snapshots_json(tags: + ) snapshots.max_by { |snapshot| Time.parse(snapshot.fetch('time')) } rescue JSON::ParserError nil end |
#ls_json(snapshot) ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/kamal_backup/restic.rb', line 144 def ls_json(snapshot) output = run(['ls', '--json', snapshot], log_output: false).stdout output.lines.filter_map do |line| JSON.parse(line) rescue JSON::ParserError nil end end |
#pipe_dump_to_command(snapshot, filename, command) ⇒ Object
171 172 173 174 |
# File 'lib/kamal_backup/restic.rb', line 171 def pipe_dump_to_command(snapshot, filename, command) restic_command = CommandSpec.new(argv: ['restic', 'dump', snapshot, filename], env: restic_env) pipe_commands(restic_command, command, producer_label: 'restic dump', consumer_label: command.argv.first) end |
#prune ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/kamal_backup/restic.rb', line 102 def prune retention_tag_sets.map do || args = ['forget', '--prune', '--group-by', 'host'] + config.retention_args + filter_tag_args() log("running restic forget/prune with retention policy for #{retention_scope()}") run(args) end end |
#restore_snapshot(snapshot, target) ⇒ Object
206 207 208 209 |
# File 'lib/kamal_backup/restic.rb', line 206 def restore_snapshot(snapshot, target) log("restoring file snapshot #{snapshot} to #{target}") run(['restore', snapshot, '--target', target]) end |
#run(args, log_output: true) ⇒ Object
211 212 213 214 215 216 217 |
# File 'lib/kamal_backup/restic.rb', line 211 def run(args, log_output: true) Command.capture( CommandSpec.new(argv: ['restic'] + args, env: restic_env), redactor: redactor, log_output: log_output ) end |
#snapshots(tags: common_tags) ⇒ Object
123 124 125 |
# File 'lib/kamal_backup/restic.rb', line 123 def snapshots(tags: ) run(['snapshots'] + filter_tag_args()) end |
#snapshots_json(tags: common_tags) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/kamal_backup/restic.rb', line 127 def snapshots_json(tags: ) output = run(['snapshots', '--json'] + filter_tag_args(), log_output: false).stdout snapshots = JSON.parse(output) = .compact snapshots.select do |snapshot| = Array(snapshot['tags']) .all? { |tag| .include?(tag) } end end |
#write_dump_to_path(snapshot, filename, target_path) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/kamal_backup/restic.rb', line 176 def write_dump_to_path(snapshot, filename, target_path) command = CommandSpec.new(argv: ['restic', 'dump', snapshot, filename], env: restic_env) target_path = File.(target_path) FileUtils.mkdir_p(File.dirname(target_path)) temp_path = "#{target_path}.kamal-backup-#{$PROCESS_ID}.tmp" output = Command.output context = output&.command_start(command, redactor: redactor) Open3.popen3(command.env, *command.argv) do |stdin, stdout, stderr, wait_thread| stdin.close stderr_reader = Thread.new do Command.collect_stream(stderr, command_output: output, context: context, stream: :stderr, redactor: redactor) end File.open(temp_path, 'wb') { |file| IO.copy_stream(stdout, file) } err = stderr_reader.value status = wait_thread.value output&.command_exit(context, status.exitstatus) raise_command_error(command, status, '', err) unless status.success? end File.rename(temp_path, target_path) target_path rescue Errno::ENOENT => e FileUtils.rm_f(temp_path) if temp_path raise CommandError.new("command not found: #{command.argv.first}", command: command, status: 127, stderr: e.) rescue StandardError FileUtils.rm_f(temp_path) if temp_path raise end |