Class: Backup::Jar
- Inherits:
-
Object
- Object
- Backup::Jar
- Defined in:
- lib/backup/jar.rb
Class Method Summary collapse
- .all(file_item, root_path) ⇒ Object
- .fetch_index_for(file_item, root_path, hash, timestamp) ⇒ Object
- .hash_to_path(file_item, root_path, hash) ⇒ Object
- .jar_versions(file_item, root_path, jar, hash = false) ⇒ Object
Instance Method Summary collapse
- #hash_local_files ⇒ Object
-
#initialize(file_item, root_path, local_path, key = nil) ⇒ Jar
constructor
A new instance of Jar.
- #jar_hash ⇒ Object
- #save(increment = false, compression = nil, purge = false) ⇒ Object
Constructor Details
#initialize(file_item, root_path, local_path, key = nil) ⇒ Jar
Returns a new instance of Jar.
3 4 5 6 7 8 9 |
# File 'lib/backup/jar.rb', line 3 def initialize(file_item, root_path, local_path, key = nil) @root_path = root_path @local_path = local_path @timestamp = Backup::Timestamp.create @file_item = file_item @key = key end |
Class Method Details
.all(file_item, root_path) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/backup/jar.rb', line 185 def all(file_item, root_path) hashes = file_item.dir("#{root_path}/meta/jars").map do |backup| backup[/[0-9a-z]{32}$/] end.compact.sort result = {} hashes.each do |hash| jar_local_path = Jar.hash_to_path(file_item, root_path, hash) result[jar_local_path] = hash unless jar_local_path.empty? end result end |
.fetch_index_for(file_item, root_path, hash, timestamp) ⇒ Object
212 213 214 215 |
# File 'lib/backup/jar.rb', line 212 def fetch_index_for(file_item, root_path, hash, ) index = file_item.read_file "#{root_path}/meta/#{hash}/#{}.yml" YAML::load(index) unless index.nil? end |
.hash_to_path(file_item, root_path, hash) ⇒ Object
179 180 181 182 183 |
# File 'lib/backup/jar.rb', line 179 def hash_to_path(file_item, root_path, hash) file_item.read_file("#{root_path}/meta/jars/#{hash}") rescue Errno::ENOENT "" end |
.jar_versions(file_item, root_path, jar, hash = false) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/backup/jar.rb', line 200 def jar_versions(file_item, root_path, jar, hash = false) jar = jar.chop if jar =~ /\/$/ jar = Digest::MD5.hexdigest(jar) unless hash = "#{root_path}/meta/#{jar}" file_item.dir(, "*.yml").map do |file| match = file.match(/^\/?([0-9]{12}).yml$/) match[1] if match end.compact.sort end |
Instance Method Details
#hash_local_files ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/backup/jar.rb', line 148 def hash_local_files files = {} if File.directory? @local_path matches = [] Dir.glob(File.join(@local_path, "/**/*"), File::FNM_DOTMATCH) do |file| if File.file?(file) or File.directory?(file) puts_fail "Permission denied: #{file}" unless File.readable?(file) matches << file if file[/\/\.\.$/].nil? and file[/\/\.$/].nil? end end #matches = matches.select do |match| # match[/\/\.\.$/].nil? and match[/\/\.$/].nil? #end matches << @local_path matches.each do |match| files.merge!(@file_item.stat match) end else files = @file_item.stat @local_path end files end |
#jar_hash ⇒ Object
11 12 13 |
# File 'lib/backup/jar.rb', line 11 def jar_hash Digest::MD5.hexdigest(@local_path) end |
#save(increment = false, compression = nil, purge = false) ⇒ Object
15 16 17 18 19 20 21 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/backup/jar.rb', line 15 def save increment = false, compression = nil, purge = false @meta_index = {} @local_files = hash_local_files if increment = Jar.jar_versions(@file_item, @root_path, jar_hash, true).last if .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, ) @local_files.keys.each do |file| current = @local_files[file].dup current.delete(:timestamp) 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]) @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 , , jar_data_path @file_item.create_file_once( "#{}/#{jar_hash}", @file_item.semantic_path(@local_path) ) if @file_item.is_a? Backup::FileItem::Cloud = ProgressBar.new( "Uploading", @local_files.keys.count ) else = ProgressBar.new( "Copying", @local_files.keys.count ) end . = '*' begin files_to_push = @local_files.keys threads = [] 32.times do threads << Thread.new do while file = files_to_push.pop if @meta_index[file].nil? unless File.directory? file data = StringIO.new File.open(file, 'rb').read checksum = Digest::MD5.hexdigest(data.read) 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 ) .inc end @meta_index[file] = @local_files[file] @meta_index[file][:checksum] = checksum @meta_index[file][:timestamp] = @timestamp end end end end threads.each &:join rescue Exception => e @meta_index.merge!({ :jar_path => , :timestamp => @timestamp }) File.open("/var/tmp/encbs.swap", "w") do |f| f.print @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( "#{}/#{@timestamp}.yml", @meta_index.to_yaml ) .finish if purge puts "Removing previous backups..." previous_versions = Jar.jar_versions @file_item, @root_path, jar_hash, true previous_versions.delete @timestamp previous_versions.each do |version| @file_item.delete_file "#{}/#{version}.yml" @file_item.delete_dir "#{@root_path}/#{jar_hash}/#{version}" end end end @timestamp end |