Class: BackupRestore
- Inherits:
-
Object
- Object
- BackupRestore
- 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
- .change_directory(target) ⇒ Object
- .compare(compared_path, unpack_root) ⇒ Object
- .compare_paths(path_to_backuped, backup_location) ⇒ Object
- .debug(message, priority = :medium) ⇒ Object
- .discard_unimportant(text, unimportant_paths_array, possible_prefix = []) ⇒ Object
- .everything_is_fine_message ⇒ Object
- .everything_is_fine_or_unimportant_message ⇒ Object
- .execute_command(command, unstandard_error_free_exit_codes = []) ⇒ Object
- .extract_archive(archive_storage_root, archive_name, unpack_root) ⇒ Object
- .extract_tar_file(file, target_folder = nil) ⇒ Object
- .get_storage_folder(archive_storage_root, archive_name) ⇒ Object
- .get_the_only_expected_file(filter = '*') ⇒ Object
- .is_unsplitting_necessary(archive_storage_root, archive_name) ⇒ Object
- .process_given_archive(archive_storage_root, archive_name, unpack_root, password) ⇒ Object
- .uncrypt_archive(archive_storage_root, archive_name, password) ⇒ Object
- .unsplit_archive(archive_storage_root, archive_name) ⇒ Object
- .validate_folder_parameters(archive_storage_root, unpack_root) ⇒ Object
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
170 171 172 173 |
# File 'lib/backup_restore.rb', line 170 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
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/backup_restore.rb', line 225 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 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(, priority = :medium) return if priority == :low return if priority == :medium puts end |
.discard_unimportant(text, unimportant_paths_array, possible_prefix = []) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/backup_restore.rb', line 181 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 == .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_message ⇒ Object
242 243 244 |
# File 'lib/backup_restore.rb', line 242 def self. return "everything is fine!" + "\n" end |
.everything_is_fine_or_unimportant_message ⇒ Object
221 222 223 |
# File 'lib/backup_restore.rb', line 221 def self. "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 files.each do |file| 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
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/backup_restore.rb', line 142 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) # archive may be in form of # (1) file(s) $NAME.tar.enc-aaa, $NAME.tar.enc-aab, $NAME.tar.enc-aac ... # (2) single $NAME.tar.enc if is_unsplitting_necessary(archive_storage_root, archive_name) unsplit_archive(archive_storage_root, archive_name) end # now $NAME.tar.enc exists with the entire archive uncrypt_archive(archive_storage_root, archive_name, password) # now $NAME.tar exists with the entire archive unencrypted # now $NAME.tar.enc can be now deleted if it was created # it MUST NOT be deleted if archive was not split - in that case # it is the original archive file! # it is deleted at this step to reduce peak memory consumption on disk # for unpacking large archives if is_unsplitting_necessary(archive_storage_root, archive_name) FileUtils.rm_rf(storage + archive_name + ".tar.enc") end extract_archive(archive_storage_root, archive_name, unpack_root) storage = get_storage_folder(archive_storage_root, archive_name) 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 -md md5 -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 131 132 133 |
# 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) if archive_name.include?(" ") raise "archive name was assumed to not have spaces but it is #{archive_name}" end execute_command("cat #{archive_name}.tar.enc-* > '#{archive_name}.tar.enc'") end |
.validate_folder_parameters(archive_storage_root, unpack_root) ⇒ Object
135 136 137 138 139 140 |
# File 'lib/backup_restore.rb', line 135 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 |