Class: Backup::Jar

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_item, root_path, local_path) ⇒ Jar

Returns a new instance of Jar.



3
4
5
6
7
8
# File 'lib/backup/jar.rb', line 3

def initialize(file_item, root_path, local_path)
  @root_path = root_path
  @local_path = local_path
  @timestamp = Backup::Timestamp.create
  @file_item = file_item
end

Class Method Details

.all(file_item, root_path) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/backup/jar.rb', line 116

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



143
144
145
146
# File 'lib/backup/jar.rb', line 143

def fetch_index_for(file_item, root_path, hash, timestamp)
  index = file_item.read_file "#{root_path}/meta/#{hash}/#{timestamp}.yml"
  YAML::load(index) unless index.nil?
end

.hash_to_path(file_item, root_path, hash) ⇒ Object



110
111
112
113
114
# File 'lib/backup/jar.rb', line 110

def hash_to_path(file_item, root_path, hash)
  file_item.read_file("#{root_path}/meta/jars/#{hash}").chomp
rescue Errno::ENOENT
  ""
end

.jar_versions(file_item, root_path, jar, hash = false) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/backup/jar.rb', line 131

def jar_versions(file_item, root_path, jar, hash = false)
  jar = jar.chop if jar =~ /\/$/
  jar = Digest::MD5.hexdigest(jar) unless hash

  meta_jar_path = "#{root_path}/meta/#{jar}"

  file_item.dir(meta_jar_path, "*.yml").map do |file|
    match = file.match(/^\/?([0-9]{12}).yml$/)
    match[1] if match
  end.compact.sort
end

Instance Method Details

#hash_local_filesObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/backup/jar.rb', line 85

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))
    end
  else
    files = @file_item.stat(@local_path, @timestamp)
  end

  files
end

#jar_hashObject



10
11
12
# File 'lib/backup/jar.rb', line 10

def jar_hash
  Digest::MD5.hexdigest(@local_path)
end

#save(increment = false) ⇒ Object



14
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
# File 'lib/backup/jar.rb', line 14

def save(increment = false)
  unless increment
    @local_files = hash_local_files
  else
    @local_files = {}
    current_files = hash_local_files

    last_timestamp = Jar.jar_versions(@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(@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
      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])

          @local_files[file][:timestamp] = last_index[file][:timestamp]
        end
      end
      # }
    end
  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}",
    @file_item.semantic_path(@local_path)
  )
  @file_item.create_file_once(
  	"#{meta_jar_path}/#{@timestamp}.yml",
    @local_files.to_yaml
			)

  if @file_item.is_a? Backup::FileItem::Cloud
    pbar = ProgressBar.new(
    	"Uploading",
      @local_files.keys.count
    )
  else
    pbar = ProgressBar.new(
    	"Copying",
      @local_files.keys.count
    )
  end

  pbar.bar_mark = '*'

  @local_files.keys.each do |file|
    unless Dir.exists?(file)
      @file_item.create_file_once "#{jar_data_path}/#{@file_item.file_hash file}",
                                  File.open(file)
      pbar.inc
    end
  end

  pbar.finish
end