lib/backup/jar.rb in encbs-0.1.3 vs lib/backup/jar.rb in encbs-0.2.0.alpha

- old
+ new

@@ -10,109 +10,137 @@ def jar_hash Digest::MD5.hexdigest(@local_path) end - def save(increment = false) - unless increment - @local_files = hash_local_files - else - @local_files = {} - current_files = hash_local_files + def save increment = false, compression = nil + @meta_index = {} + @local_files = hash_local_files + if increment last_timestamp = Jar.jar_versions(@file_item, @root_path, jar_hash, true).last if last_timestamp.nil? puts_fail "First you must create a full backup for #{@local_path.dark_green}" end - last_index = Jar.fetch_index_for(@file_item, @root_path, jar_hash, last_timestamp) + @last_index = Jar.fetch_index_for(@file_item, @root_path, jar_hash, last_timestamp) - current_files.keys.each do |file| - @local_files[file] = current_files[file] - - #TODO: Cut to a new method { - current = current_files[file].dup + @local_files.keys.each do |file| + current = @local_files[file].dup current.delete(:timestamp) - - unless last_index[file].nil? - backup = last_index[file].dup + + unless @last_index[file].nil? + backup = @last_index[file].dup backup.delete(:timestamp) - + if (current == backup) or (!current[:checksum].nil? and current[:checksum] == backup[:checksum]) - - @local_files[file][:timestamp] = last_index[file][:timestamp] + + @meta_index[file] = @local_files[file] + @meta_index[file][:timestamp] = @last_index[file][:timestamp] end end - # } end end + unless @key.nil? + @meta_index.merge!({ + :checksum => Base64.encode64(@key.encrypt(@timestamp)) + }) + end + + unless compression.nil? + @meta_index.merge!({ + :compression => compression.type.to_s + }) + end + @file_item.create_directory_once meta_jars_path, meta_jar_path, jar_data_path @file_item.create_file_once( - "#{meta_jars_path}/#{jar_hash}", + "#{meta_jars_path}/#{jar_hash}", @file_item.semantic_path(@local_path) ) - @file_item.create_file_once( - "#{meta_jar_path}/#{@timestamp}.yml", - @local_files.to_yaml - ) - @local_files.select! {|k, v| v[:timestamp] == @timestamp} if @file_item.is_a? Backup::FileItem::Cloud pbar = ProgressBar.new( - "Uploading", + "Uploading", @local_files.keys.count ) else pbar = ProgressBar.new( - "Copying", + "Copying", @local_files.keys.count ) end - + pbar.bar_mark = '*' - @local_files.keys.each do |file| - unless Dir.exists?(file) - data = if @key - @key.encrypt_to_stream(File.open(file).read) - else - File.open(file) - end + #@local_files = @meta_index.select {|k, v| v[:timestamp] == @timestamp if v.is_a? Hash} - @file_item.create_file_once( - "#{jar_data_path}/#{@file_item.file_hash file}", - data - ) + begin + @local_files.keys.each do |file| + if @meta_index[file].nil? + unless Dir.exists?(file) + data = StringIO.new File.open(file, 'rb').read + checksum = Digest::MD5.hexdigest(data.read) - pbar.inc + data.seek 0 + data = compression.compress(data.read, 3) unless compression.nil? + + data = @key.encrypt_to_stream(data) if @key + + @file_item.create_file_once( + "#{jar_data_path}/#{@file_item.file_hash file}", + data + ) + + pbar.inc + end + + @meta_index[file] = @local_files[file] + @meta_index[file][:checksum] = checksum + @meta_index[file][:timestamp] = @timestamp + end end + rescue Exception => e + @meta_index.merge!({ + :jar_path => meta_jar_path, + :timestamp => @timestamp + }) + File.open("/var/tmp/encbs.swap", "w") do |f| + f.puts @meta_index.to_yaml + end + + puts + puts_fail "Index file has been saved that to allow upload into cloud in next run." + else + @file_item.create_file_once( + "#{meta_jar_path}/#{@timestamp}.yml", + @meta_index.to_yaml + ) end pbar.finish end def hash_local_files files = {} - puts_verbose "Create index for #{@local_path.dark_green}" - if Dir.exists? @local_path matches = Dir.glob(File.join(@local_path, "/**/*"), File::FNM_DOTMATCH) matches = matches.select do |match| match[/\/..$/].nil? and match[/\/.$/].nil? end matches << @local_path matches.each do |match| - files.merge!(@file_item.stat(match, @timestamp)) + files.merge!(@file_item.stat match) end else - files = @file_item.stat(@local_path, @timestamp) + files = @file_item.stat @local_path end files end