Class: BackupRestore

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

Overview

some assumptions: this tool runs on Ubuntu archive is valid archive generated with backup gem files are encrypted and archived with tar archives may be split or not

Defined Under Namespace

Classes: PreconditionFailed, UnexpectedData

Class Method Summary collapse

Class Method Details

.change_directory(target) ⇒ Object



21
22
23
24
25
# File 'lib/backup_restore.rb', line 21

def self.change_directory(target)
  Dir.chdir(target)
  return if File.identical?(target, Dir.getwd)
  raise "failed to change working directory to #{target} (it is #{Dir.getwd})"
end

.compare(compared_path, unpack_root) ⇒ Object



154
155
156
157
# File 'lib/backup_restore.rb', line 154

def self.compare(compared_path, unpack_root)
  text = compare_paths(compared_path, unpack_root)
  return text
end

.compare_paths(path_to_backuped, backup_location) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/backup_restore.rb', line 209

def self.compare_paths(path_to_backuped, backup_location)
  original = path_to_backuped
  restored = backup_location + path_to_backuped
  raise "missing folder for comparison: #{original}" unless Dir.exist?(original)
  raise "missing folder for comparison: #{restored}" unless Dir.exist?(restored)
  command = "diff --brief -r --no-dereference '#{original}' '#{restored}'"
  puts
  puts command
  puts
  returned = execute_command(command, [1])
  if returned == ""
    return everything_is_fine_message
  else
    return returned
  end
end

.debug(message, priority = :medium) ⇒ Object



15
16
17
18
19
# File 'lib/backup_restore.rb', line 15

def self.debug(message, priority = :medium)
  return if priority == :low
  return if priority == :medium
  puts message
end

.discard_unimportant(text, unimportant_paths_array, possible_prefix = []) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
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
# File 'lib/backup_restore.rb', line 165

def self.discard_unimportant(text, unimportant_paths_array, possible_prefix = [])
  possible_prefix << ""
  output = ""
  text.split("\n").each do |line|
    line.strip!
    unimportant = false
    unimportant_paths_array.each do |filter|
      possible_prefix.each do |prefix|
        r_filter = (Regexp.escape filter).gsub('/', '\/')
        r_prefix = (Regexp.escape prefix).gsub('/', '\/')
        if line =~ /\AOnly in (.+): (.+)\z/
          filepath_without_file, file = /\AOnly in (.+): (.+)\z/.match(line).captures
          filepath_without_file += '/' if filepath_without_file[-1] != '/'
          filepath = filepath_without_file + file
          unimportant = true if filepath =~ /\A#{r_prefix}#{r_filter}.*\z/
        elsif line =~ /\AFiles (.+) and (.+) differ\z/
          filepath_a, filepath_b = /\AFiles (.+) and (.+) differ\z/.match(line).captures
          unimportant = true if filepath_a =~ /\A#{r_prefix}#{r_filter}.*\z/
          unimportant = true if filepath_b =~ /\A#{r_prefix}#{r_filter}.*\z/
        elsif line =~ /\AFile (.+) is a fifo while file (.+) is a fifo\z/
          unimportant = true
        elsif line =~ /\AFile (.+) is a character special file while file (.+) is a character special file\z/
          unimportant = true
        elsif line == everything_is_fine_message.strip
          next
        elsif line == ""
          next
        else
          raise UnexpectedData, "unexpected line <#{line}>"
        end
      end
    end
    next if unimportant
    output += line + "\n"
  end
  puts
  return nil if output == ""
  return output.to_s
end

.everything_is_fine_messageObject



226
227
228
# File 'lib/backup_restore.rb', line 226

def self.everything_is_fine_message
  return "everything is fine!" + "\n"
end

.everything_is_fine_or_unimportant_messageObject



205
206
207
# File 'lib/backup_restore.rb', line 205

def self.everything_is_fine_or_unimportant_message
  "no important differences"
end

.execute_command(command, unstandard_error_free_exit_codes = []) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/backup_restore.rb', line 47

def self.execute_command(command, unstandard_error_free_exit_codes = [])
  output = `#{command}`
  if $?.success? || unstandard_error_free_exit_codes.include?($?.exitstatus)
    debug('all done', :low)
  else
    raise "<#{command}> command had problem (<#{$?}> with output <#{output}>). Working directory path was <#{Dir.getwd}>"
  end
  return output
end

.extract_archive(archive_storage_root, archive_name, unpack_root) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/backup_restore.rb', line 99

def self.extract_archive(archive_storage_root, archive_name, unpack_root)
  debug("unpacking <#{archive_name}>", :high)

  storage = get_storage_folder(archive_storage_root, archive_name)
  change_directory(storage)
  debug("archive is stored at <#{storage}>")

  file = get_the_only_expected_file('*.tar')
  debug("extracting #{file}")
  extract_tar_file(file)
  folder_with_unpacked_archive = storage + archive_name
  debug("unpacked archive with second layer of archive is stored at <#{folder_with_unpacked_archive}>")

  change_directory(folder_with_unpacked_archive + '/archives/')
  file = get_the_only_expected_file('*.tar.gz')
  debug("extracting #{file}")
  extract_tar_file(file, unpack_root)

  change_directory(storage)
  FileUtils.rm_rf(folder_with_unpacked_archive)
end

.extract_tar_file(file, target_folder = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/backup_restore.rb', line 57

def self.extract_tar_file(file, target_folder = nil)
  command = "tar --extract --file=#{file}"
  unless target_folder.nil?
    command += " --preserve-permissions -C #{target_folder}"
    # -C
    # tar will change its current directory to dir before performing any operations
    # https://www.gnu.org/software/tar/manual/html_node/Option-Summary.html
  end
  # base test:
  # echo "tar: Removing leading \`/' from member names" | grep -v "tar: Removing leading \`/' from member names"
  # shell should get:
  # grep -v "tar: Removing leading \`/' from member names"
  # command += ' | grep -v "tar: Removing leading \`/\' from member names"'
  # the code above is not proper way to solve this, it will mess up errorcode (hide errors, grep will return error on lack of match)
  execute_command(command)
end

.get_storage_folder(archive_storage_root, archive_name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/backup_restore.rb', line 27

def self.get_storage_folder(archive_storage_root, archive_name)
  debug("joining <#{archive_storage_root}> and <#{archive_name}>", :low)
  target = archive_storage_root + '/' + archive_name
  debug("looking for date folder in <#{target}>", :low)
  change_directory(target)
  directory = Dir.glob('*').select { |f| File.directory? f }
  if directory.length != 1
    puts "unexpected multiple backups at once in #{target}, or backup not found"
    puts "not supposed to happen in my workflow"
    puts "listing #{directory.length} directories, expected exactly 1:"
    directory.each do |file|
      puts file
    end
    raise "unhandled workflow"
  end
  target += '/' + directory[0] + '/'
  return target
end

.get_the_only_expected_file(filter = '*') ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/backup_restore.rb', line 74

def self.get_the_only_expected_file(filter = '*')
  files = Dir.glob(filter)
  puts files
  if files.length != 1
    if files.empty?
      puts 'no files found'
    else
      puts "files:"
    end
    for file in files
      puts file
    end
    raise "expected exactly one file, not handled!"
  end
  return files[0]
end

.is_unsplitting_necessary(archive_storage_root, archive_name) ⇒ Object



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

def self.is_unsplitting_necessary(archive_storage_root, archive_name)
  storage = get_storage_folder(archive_storage_root, archive_name)
  return File.exist?(storage + "#{archive_name}.tar.enc-aaa")
end

.process_given_archive(archive_storage_root, archive_name, unpack_root, password) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/backup_restore.rb', line 139

def self.process_given_archive(archive_storage_root, archive_name, unpack_root, password)
  debug("processsing #{archive_name} in #{archive_storage_root} - extracting to #{unpack_root}", :high)
  validate_folder_parameters(archive_storage_root, unpack_root)
  if is_unsplitting_necessary(archive_storage_root, archive_name)
    unsplit_archive(archive_storage_root, archive_name)
  end
  uncrypt_archive(archive_storage_root, archive_name, password)
  extract_archive(archive_storage_root, archive_name, unpack_root)
  storage = get_storage_folder(archive_storage_root, archive_name)
  if is_unsplitting_necessary(archive_storage_root, archive_name)
    FileUtils.rm_rf(storage + archive_name + ".tar.enc")
  end
  FileUtils.rm_rf(storage + archive_name + ".tar")
end

.uncrypt_archive(archive_storage_root, archive_name, password) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/backup_restore.rb', line 91

def self.uncrypt_archive(archive_storage_root, archive_name, password)
  storage = get_storage_folder(archive_storage_root, archive_name)
  output_archive = archive_name + '.tar'
  change_directory(storage)
  command = "openssl aes-256-cbc -d -in #{archive_name}.tar.enc -k #{password} -out #{output_archive}"
  execute_command(command)
end

.unsplit_archive(archive_storage_root, archive_name) ⇒ Object



126
127
128
129
130
# File 'lib/backup_restore.rb', line 126

def self.unsplit_archive(archive_storage_root, archive_name)
  storage = get_storage_folder(archive_storage_root, archive_name)
  change_directory(storage)
  execute_command("cat #{archive_name}.tar.enc-* > #{archive_name}.tar.enc")
end

.validate_folder_parameters(archive_storage_root, unpack_root) ⇒ Object

Raises:



132
133
134
135
136
137
# File 'lib/backup_restore.rb', line 132

def self.validate_folder_parameters(archive_storage_root, unpack_root)
  raise PreconditionFailed.new("archive_storage_root (<#{archive_storage_root}>) does not exists") if !File.exist?(archive_storage_root)
  raise PreconditionFailed.new("unpack_root (<#{unpack_root}>) does not exists") if !File.exist?(unpack_root)
  raise PreconditionFailed.new("archive_storage_root (<#{archive_storage_root}>) is a file, not directory") if !Dir.exist?(archive_storage_root)
  raise PreconditionFailed.new("unpack_root (<#{unpack_root}>) is a file, not directory") if !Dir.exist?(unpack_root)
end