Class: Raz::Backuper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, destination_path) ⇒ Backuper

Returns a new instance of Backuper.

Parameters:

  • config (Config)


20
21
22
23
# File 'lib/raz/backuper.rb', line 20

def initialize(config, destination_path)
  @config = config
  @destination_path = File.expand_path(destination_path)
end

Instance Attribute Details

#destination_pathString (readonly)

Returns:

  • (String)


12
13
14
# File 'lib/raz/backuper.rb', line 12

def destination_path
  @destination_path
end

#parsed_entriesArray<DirEntry | FileEntry> (readonly)

Returns:



16
17
18
# File 'lib/raz/backuper.rb', line 16

def parsed_entries
  @parsed_entries
end

Instance Method Details

#backupObject

Main method to back all files from configuration



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
64
65
66
67
68
69
70
# File 'lib/raz/backuper.rb', line 37

def backup
  FileUtils.mkdir_p(@destination_path)

  dest_contents = FileOperations.dir_entries(@destination_path)

  unless dest_contents.empty?
    raise "Can only operate on empty or non-existing directory! Directory #{@destination_path} contains: #{dest_contents}"
  end

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

  investigate if @parsed_entries.nil?

  @copied_paths = []

  @parsed_entries.each do |entry|
    copy_entry_to_dest(entry)
  end

  # save info for restorer
  save_info

  # backup config folder
  FileOperations.copy_item(File.dirname(@config.path), @destination_path)
  FileUtils.mv(File.join(@destination_path, CONFIG_FOLDER_BASE_PATH), File.join(@destination_path, BACKUP_CONFIG_FOLDER_BASE_PATH))

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

#investigateObject



25
26
27
28
29
30
31
32
33
# File 'lib/raz/backuper.rb', line 25

def investigate
  @file_system = FileSystem.new
  @parsed_entries = []

  # process all items from configuration file
  @config.items.each do |item|
    process_item(item)
  end
end