Class: Castled::Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/castled/backup.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

MANIFEST_FILENAME =
".castled_manifest.yml"

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Backup

Returns a new instance of Backup.



14
15
16
# File 'lib/castled/backup.rb', line 14

def initialize(config)
  @config = config
end

Instance Method Details

#list_backupsObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/castled/backup.rb', line 58

def list_backups
  dest = @config.destination_path
  return [] unless dest.directory?

  prefix = "#{@config.backup_name}_"
  dest.children
      .select(&:directory?)
      .select { |d| d.basename.to_s.start_with?(prefix) }
      .sort_by { |d| d.basename.to_s }
      .reverse
end

#restore!(selection: nil, dry_run: false, diff: false, io: $stdout) ⇒ Object

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/castled/backup.rb', line 38

def restore!(selection: nil, dry_run: false, diff: false, io: $stdout)
  backups = list_backups
  raise Error, "No backups found for '#{@config.backup_name}'" if backups.empty?

  chosen = select_backup(backups, selection: selection)
  manifest = load_manifest(chosen)
  plan = restore_plan(chosen, manifest)

  if dry_run
    print_dry_run(chosen, plan, diff: diff, io: io)
    return chosen.to_s
  end

  plan.each do |operation|
    restore_operation(operation)
  end

  chosen.to_s
end

#run!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/castled/backup.rb', line 18

def run!
  timestamp = Time.now.strftime("%Y%m%d_%H%M%S")
  backup_dir = @config.destination_path.join("#{@config.backup_name}_#{timestamp}")
  FileUtils.mkdir_p(backup_dir)

  entries = []
  @config.backup_paths.each do |source_path|
    source = Pathname.new(source_path).expand_path
    raise Error, "Path not found: #{source}" unless source.exist?

    dest_name = source.basename.to_s
    dest = backup_dir.join(dest_name)
    copy_entry(source, dest)
    entries << { "source" => source.to_s, "backup_entry" => dest_name }
  end

  write_manifest(backup_dir, entries)
  backup_dir.to_s
end