Class: Raz::Restorer

Inherits:
Object
  • Object
show all
Defined in:
lib/raz/restorer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_path) ⇒ Restorer

Returns a new instance of Restorer.

Parameters:

  • source_path (String)


18
19
20
# File 'lib/raz/restorer.rb', line 18

def initialize(source_path)
  @source_path = source_path
end

Instance Attribute Details

#source_pathString (readonly)

Returns:

  • (String)


14
15
16
# File 'lib/raz/restorer.rb', line 14

def source_path
  @source_path
end

Instance Method Details

#restoreObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/raz/restorer.rb', line 22

def restore
  # load saved information from YAML
  @info = YAML.load(File.read(File.join(@source_path, BACKUP_INFO_BASE_PATH)))
  @orig_home_path = @info[:env]['HOME']

  # load config file
  config = ConfigFile.new(backup_config_file_path(@source_path))

  # run before procs
  (config.procs[:before_restore] || []).each do |proc|
    instance_eval &proc
  end

  # restore all files
  @info[:copied_paths].each do |src|
    dest = destination_path_from_original(src)
    src = File.join(@source_path, BACKUP_DATA_BASE_PATH, src)

    if File.directory?(dest)
      FileOperations.dir_entries(src).each do |item|
        puts "Restoring file to #{File.join(dest, item)}".green
        FileOperations.copy_item(File.join(src, item), dest)
      end
    else
      FileUtils.rmtree(dest) if File.exist?(dest)
      FileUtils.mkdir_p(File.dirname(dest))

      puts "Restoring item to #{dest}".green
      FileOperations.copy_item(src, dest)
    end
  end

  # restore config file to previous location
  orig_config_path = destination_path_from_original(@info[:orig_config_path])
  FileUtils.mkdir_p(File.dirname(orig_config_path))
  FileOperations.copy_item(config.path, orig_config_path)

  # run after procs
  (config.procs[:after_restore] || []).each do |proc|
    instance_eval &proc
  end
end